Created
March 29, 2016 22:53
-
-
Save Scinawa/1350a46b7ec7132368cdc6dc4fd871f9 to your computer and use it in GitHub Desktop.
Integer Partition
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
# http://stackoverflow.com/questions/10244180/python-generating-integer-partitions (checked) | |
def accelAsc(n): | |
a = [0 for i in range(n + 1)] | |
k = 1 | |
a[0] = 0 | |
y = n - 1 | |
while k != 0: | |
x = a[k - 1] + 1 | |
k -= 1 | |
while 2*x <= y: | |
a[k] = x | |
y -= x | |
k += 1 | |
l = k + 1 | |
while x <= y: | |
a[k] = x | |
a[l] = y | |
yield a[:k + 2] | |
x += 1 | |
y -= 1 | |
a[k] = x + y | |
y = x + y - 1 | |
yield a[:k + 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment