Skip to content

Instantly share code, notes, and snippets.

@gayalkuruppu
Created November 2, 2019 15:33
Show Gist options
  • Save gayalkuruppu/e54bd306cea630d3c41d80cb5387c054 to your computer and use it in GitHub Desktop.
Save gayalkuruppu/e54bd306cea630d3c41d80cb5387c054 to your computer and use it in GitHub Desktop.
Number partition in Python
def partition(n, c=[], k=1):
if n == 0:
yield c
for i in range(k, n + 1):
for p in partition(n - i, c + [i], i):
yield p
# Example: Partition 10 as a sum of integers
for j in partition(10):
print ' + '.join(map(str, j))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment