Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 077ab14

Browse filesBrowse files
authored
Merge pull request AllAlgorithms#15 from pablotrinidad/add-factorial
Add factorial
2 parents 63ffc0f + 8b05c20 commit 077ab14
Copy full SHA for 077ab14

File tree

Expand file treeCollapse file tree

2 files changed

+28
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+28
-0
lines changed

‎math/factorial_iterative.py

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Factorial of n (iterative implementation).
2+
3+
@author: Pablo Trinidad <github.com/pablotrinidad>
4+
"""
5+
6+
7+
def factorial(n):
8+
"""Algorithm implementation."""
9+
r = 1
10+
while n > 0:
11+
r = r * n
12+
n = n - 1
13+
return r
14+
15+
n = int(input("Enter an integer n to compute its factorial: "))
16+
print(str(n) + "! = " + str(factorial(n)))

‎math/factorial_recursive.py

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Factorial of n (recursive implementation).
2+
3+
@author: Pablo Trinidad <github.com/pablotrinidad>
4+
"""
5+
6+
7+
def factorial(n):
8+
"""Algorithm implementation."""
9+
return 1 if n <= 0 else n * factorial(n - 1)
10+
11+
n = int(input("Enter an integer n to compute its factorial: "))
12+
print(str(n) + "! = " + str(factorial(n)))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.