Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Solving 76, thanks @akrito for the suggestion to memoize

  • Loading branch information...
commit d352114a28cd6241f83f8587f61ce1d66c34598c 1 parent d10c0af
Charles Leifer authored May 08, 2012

Showing 1 changed file with 21 additions and 0 deletions. Show diff stats Hide diff stats

  1. 21  problem76.py
21  problem76.py
... ...
@@ -0,0 +1,21 @@
  1
+#!/usr/bin/env python
  2
+
  3
+def memo(fn):
  4
+    _memo = {}
  5
+    def inner(k, n):
  6
+        tup = (k, n)
  7
+        if tup not in _memo:
  8
+            _memo[tup] = fn(k, n)
  9
+        return _memo[tup]
  10
+    return inner
  11
+
  12
+@memo
  13
+def p(k, n):
  14
+    if k > n:
  15
+        return 0
  16
+    if k == n:
  17
+        return 1
  18
+    return p(k+1, n) + p(k, n-k)
  19
+
  20
+# subtract 1 since we don't count n + 0
  21
+print p(1, 100) - 1

0 notes on commit d352114

Please sign in to comment.
Something went wrong with that request. Please try again.