Created
August 14, 2014 22:31
-
-
Save justinmoon/7b2089bcc71653a691be to your computer and use it in GitHub Desktop.
cool way to define factorials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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