Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. import timeit
  2. # Add a memoization method?
  3.  
  4. def factorial(n):
  5. if n == 0:
  6. return 1
  7. else:
  8. return n * factorial(n - 1)
  9.  
  10.  
  11. def factorial_iterative(n):
  12. product = 1
  13. for i in range(1, n + 1):
  14. product *= i
  15. return product
  16.  
  17.  
  18. if __name__ == '__main__':
  19. print('factorial():')
  20. print(timeit.timeit('factorial(15)', number=10000,
  21. setup='from __main__ import factorial'))
  22. print('factorial_iterative():')
  23. print(timeit.timeit('factorial_iterative(15)', number=10000,
  24. setup='from __main__ import factorial_iterative'))
Add Comment
Please, Sign In to add comment