Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Created February 20, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfreixes/e7f6f89f7ef60234b5dc to your computer and use it in GitHub Desktop.
Save pfreixes/e7f6f89f7ef60234b5dc to your computer and use it in GitHub Desktop.
Use memoization to speedup the python version
#!/usr/bin/env python
UPPER_ITERATIONS = 10000000
def fac(n):
if n == 0:
return 1
return n * fac(n - 1)
def optimized():
t = 0
facs = [fac(x) for x in range(8)]
for j in xrange(UPPER_ITERATIONS):
for i in range(8):
t += facs[i]
return t
if __name__ == "__main__":
t = optimized()
print("total: {0}".format(t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment