Skip to content

Instantly share code, notes, and snippets.

@justinmoon
Created August 14, 2014 22:31
Show Gist options
  • Save justinmoon/7b2089bcc71653a691be to your computer and use it in GitHub Desktop.
Save justinmoon/7b2089bcc71653a691be to your computer and use it in GitHub Desktop.
cool way to define factorials
class F:
def __init__(self):
self.cache = {}
def __call__(self, n):
if n in self.cache.keys():
return self.cache[n]
else:
if n == 1:
return 1
else:
return n*self.__call__(n-1)
factorial = F()
factorial(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment