The factorial is a basic operation in mathematics, and it’s also used frequently in Computer Science for many reasons. In this article, I’ll show you how to create a simple Python function that calculates the factorial of any given number (but only positive numbers).

What is the Factorial of a Number?

The factorial of the non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example:

10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800

4! = 4 x 3 x 2 x 1 = 24

1! = 1

The value of 0! is 1, according to the convention for an empty product.

Factorial Function (Recursive Algorithm)

Just follow these steps to implement a Python function that calculates the factorial of any given number recursively (Note: I’ve commented all lines of code):

def factorial(num):

”’

This is a recursive function to find the factorial of a given number

”’

if num == 1:

return 1

else:

return (num * factorial(num-1))

num = int(input(“Enter a Number: “))

print(“The factorial of”, num, “is”, factorial(num))

from math import factorial

print(factorial(8))

def fact(n):

“””Calculate n! iteratively”””

result = 1

if n > 1:

for f in range(2, n + 1):

result *= f

return result

def factorial(x):

if x == 1:

return 1

else:

return (x * factorial(x-1))

Note that the above example contain a recursive function to calculate the factorial of a number. The base case is n = 1. This is written as x == 1 in this example. So, when we call the function with the value of 1, it will return 1 immediately. And when the function called with any other values (except for 1), it will execute the else part first before return its value.

This is an example for recursion. But this is not efficient because it makes multiple copies of the same function on memory. To avoid this, you can use a loop instead.

Tomy

Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.

You May Also Like

Artistic representation for Programming vs Alternatives: Complete Comparison

Programming vs Alternatives: Complete Comparison

Programming vs Alternatives: A Sustainable Developer's Dilemma In an era where digital solutions are shaping our planet's future, programmers face...

A Case Study of a Large-Scale JavaScript Maintainability Project

A Case Study of a Large-Scale JavaScript Maintainability Project

I recently had to refactor the jslint codebase. It's an interesting case study in large-scale JavaScript maintainability, and also an...

Automator is an Automation app in Mac OS

Automator is an Automation app in Mac OS

If you want to know how to make your life easier, take a look at the Automator app in Mac...

7 Cool Markdown Extensions in vscode

7 Cool Markdown Extensions in vscode

Markdown is a plain text formatting syntax aimed at making writing for the internet easier. The philosophy behind Markdown is...

Leave a Reply

About | Contact | Privacy Policy | Terms of Service | Disclaimer | Cookie Policy
© 2026 AskMeCode. All rights reserved.