Created
August 14, 2015 16:01
-
-
Save lucemia/29e187f6ee64dcdda955 to your computer and use it in GitHub Desktop.
combination.py
This file contains hidden or 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
def c(n, r): | |
print n, r | |
if r == 1: return n | |
if n == r: return 1 | |
return c(n-1, r) + c(n-1, r-1) | |
# can compute it on time | |
print c(5, 2) | |
# cannot finish it ontime | |
# or stack will overflow | |
c(999, 33) | |
# use cache to speed up | |
cache = {} | |
def c1(n, r): | |
if r == 1: return n | |
if n == r: return 1 | |
if (n,r) in cache: | |
return cache[(n,r)] | |
v = c1(n-1, r) + c1(n-1, r-1) | |
cache[(n,r)] = v | |
return v | |
print c1(999, 33) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment