Skip to main content
2 of 2
deleted 122 characters in body
Aky
  • 2.7k
  • 15
  • 20

I randomly came across this question today, and remembered I'd written a function to do this in Haskell not too long ago.

 distinctPartitions 0 = [[]] distinctPartitions n = [x:xs | x <- [1..n], xs <- distinctPartitions (n - x), null xs || x < head xs] 

Here's my (probably clumsy) attempt to translate this to Mathematica (with memoization):

distinctPartitions[0] := distinctPartitions[0] = {{}}; distinctPartitions[n_] := distinctPartitions[n] = Flatten[Table[ Prepend[xs, x], {x, 1, n}, {xs, Select[distinctPartitions[n - x], Length@# == 0 || x < First@# &]}], 1] 
Aky
  • 2.7k
  • 15
  • 20