In [ ]:
import math
In [ ]:
def fact(n):
'''(int) -> int
Precondition: n >= 0
Return n!
Examples:
>>> fact(5)
5
>>> fact(10)
3628800
'''
accum = 1
for i in range( 1, n + 1):
accum *= i
return accum
In [ ]:
help(fact)
In [ ]:
def fact_recursive (n):
'''(int) -> int
Precondition: n >= 0
Return n!
Examples:
>>> fact(5)
5
>>> fact(10)
3628800
'''
return 1 if n == 1 else n * fact_recursive(n - 1)
In [ ]:
def reduce_factorial (n):
'''(int) -> int
Precondition: n >= 0
Return n!
Examples:
>>> fact(5)
5
>>> fact(10)
3628800
'''
from functools import reduce
return reduce((lambda x, y : x * y), range( 1, n + 1 ) )
In [ ]:
j = 5
In [ ]:
print('{}! = {}'.format( j, fact ( j ) ) )
In [ ]:
print('{}'.format('Agree' if fact(j) == fact_recursive(j) else 'Ups' ) )
In [ ]:
print('{}'.format('Really Agree' if fact(j) == math.factorial(j) else 'Ups' ) )
In [ ]:
print('{}'.format('Really Really Agree'
if fact(j) == reduce_factorial(j) else 'Ups' ) )