Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- cache = {}
- def factorial(x):
- if x in cache:
- return cache[x]
- if x > 1:
- value = x * factorial(x-1)
- elif x > -1:
- value = 1
- else:
- print('Wrong number')
- exit(1)
- cache[x] = value
- return value
- start = time.time()
- result = factorial(980)
- end = time.time()
- print(end - start) # 0.002 second
- start = time.time()
- result = factorial(980)
- end = time.time()
- print(end - start) # 0.000002 second (2.0e-06)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement