Skip to content

Commit aa136ea

Browse files
authored
Fixes #276 (#1982)
* factorial using recrusion * Update Factorial.py
1 parent 3a6218f commit aa136ea

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Code/Python/Factorial.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Factorial function instruct to multiply all whole number from input number down to 1.
3+
The formula for n factorial can be defined as n! = n×(n−1)!
4+
Factorial zero is defined as equal to 1
5+
'''
6+
7+
#This is a recursive function to find the factorial of an integer
8+
def factorial(num):
9+
if num == 0:
10+
return 1
11+
else:
12+
temp = factorial(num-1)
13+
return num * temp
14+
15+
num = int(input("Input: "))
16+
17+
print('Output:',factorial(num))
18+
19+
'''
20+
Test cases:
21+
Input: 7
22+
Output: 5040
23+
24+
Input: 0
25+
Output: 1
26+
27+
Time complexity: O(n)
28+
Space Complexity: O(1)
29+
30+
'''
31+

0 commit comments

Comments
 (0)