Initial commit

This commit is contained in:
froge 2025-07-31 17:26:21 +10:00
commit 4a346e5dd9
Signed by: froge
GPG key ID: A825E09930271BFA
3 changed files with 58 additions and 0 deletions

20
factorial.py Normal file
View file

@ -0,0 +1,20 @@
def factorial(n: int) -> int:
if n < 2:
return n
else:
return n * factorial(n-1)
def factorial_iter(n: int) -> int:
fact = 1
# Grab absolute value to ensure n is a positive integer
n = abs(n)
for i in range(2, n+1):
fact *= i
return fact
user_input = int(input("Insert number to calculate factorial for: "))
# Avoid recursion limit
if user_input < 1000:
print(f"Recursive factorial {user_input}: {factorial(user_input)}")
print(f"Iterative factorial {user_input}: {factorial_iter(user_input)}")