Skip to content

Instantly share code, notes, and snippets.

@zelark
Created January 16, 2014 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelark/8451557 to your computer and use it in GitHub Desktop.
Save zelark/8451557 to your computer and use it in GitHub Desktop.
class Factorial:
def __init__(self):
self.cache = {}
def __call__(self, n):
if n not in self.cache:
if n == 0:
self.cache[n] = 1
else:
self.cache[n] = n * self.__call__(n - 1)
return self.cache[n]
fact = Factorial()
for i in xrange(10):
print '%d! = %d' % (i, fact(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment