Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"pure" recursive version solution in Clear category for Number With Exclamation by mu_py
def factorial(n: int) -> int:
""" return the factorial of a non-negative integer """
return 1 if n == 0 else n * factorial(n-1)
# this is the "pure" recursive version: always step down to the most simple point, that is: 0! = 1
# it would be a bit faster to check "if n < 2", because 1! also equals 1
print("Example:")
print(factorial(4))
# These "asserts" are used for self-checking
assert factorial(0) == 1
assert factorial(1) == 1
assert factorial(5) == 120
assert factorial(10) == 3628800
assert factorial(15) == 1307674368000
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 15, 2024