Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def memoize(f):
- memory = {}
- def wrapper(n, k):
- if (n, k) not in memory:
- memory[(n, k)] = f(n, k)
- return memory[(n, k)]
- return wrapper
- @memoize
- def combination(n, k):
- if k < 0 or n < 0:
- raise ValueError
- if k == 0 or n == k:
- return 1
- return combination(n - 1, k - 1) + combination(n - 1, k)
- for n in range(0, 100):
- print ' '.join(['{}C{} = {}'.format(n, k, combination(n, k))
- for k in range(0, n + 1)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement