Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- # Add a memoization method?
- def factorial(n):
- if n == 0:
- return 1
- else:
- return n * factorial(n - 1)
- def factorial_iterative(n):
- product = 1
- for i in range(1, n + 1):
- product *= i
- return product
- if __name__ == '__main__':
- print('factorial():')
- print(timeit.timeit('factorial(15)', number=10000,
- setup='from __main__ import factorial'))
- print('factorial_iterative():')
- print(timeit.timeit('factorial_iterative(15)', number=10000,
- setup='from __main__ import factorial_iterative'))
Add Comment
Please, Sign In to add comment