Permalink
Browse files

Solving 76, thanks @akrito for the suggestion to memoize

  • Loading branch information...
1 parent d10c0af commit d352114a28cd6241f83f8587f61ce1d66c34598c @coleifer committed May 8, 2012
Showing with 21 additions and 0 deletions.
  1. +21 −0 problem76.py
View
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+def memo(fn):
+ _memo = {}
+ def inner(k, n):
+ tup = (k, n)
+ if tup not in _memo:
+ _memo[tup] = fn(k, n)
+ return _memo[tup]
+ return inner
+
+@memo
+def p(k, n):
+ if k > n:
+ return 0
+ if k == n:
+ return 1
+ return p(k+1, n) + p(k, n-k)
+
+# subtract 1 since we don't count n + 0
+print p(1, 100) - 1

0 comments on commit d352114

Please sign in to comment.