Created
October 29, 2021 13:53
-
-
Save 1f0/883cc03b7aa8f418130b5b3ea8b04c72 to your computer and use it in GitHub Desktop.
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
def partition(n): | |
# x: current coin, y: remain value | |
def helper(x, y): | |
if y <= 0: | |
return int(y == 0) | |
# use x and not use x | |
z = helper(x, y-x) | |
if x < y: | |
z += helper(x+1, y) | |
return z | |
return helper(1, n) | |
if __name__ == '__main__': | |
ans = [1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, \ | |
56, 77, 101, 135, 176, 231, 297, 385, 490] | |
for i, p in enumerate(ans): | |
assert partition(i) == p | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only prime
Result: