partition_entier.py

Created by nicolas-patrois

Created on February 12, 2019

348 Bytes

Script from this page: http://jeromekelleher.net/category/combinatorics.html


def partent(n):
  a=[0 for _ in range(n+1)]
  k=1
  y=n-1
  liste=[]
  while k:
    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
      liste.append(a[:k+2])
      x+=1
      y-=1
    a[k]=x+y
    y=x+y-1
    liste.append(a[:k+1])
  return liste