Skip to main content
deleted 122 characters in body
Source Link
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 the same idea implemented in python, additionally usingmy (probably clumsy) attempt to translate this to Mathematica (with memoization):

fromdistinctPartitions[0] functools:= importdistinctPartitions[0] lru_cache @lru_cache(maxsize=None) def= distinctPartitions(n):{{}};  distinctPartitions[n_] := if   ndistinctPartitions[n] === 0:   Flatten[Table[  Prepend[xs, returnx], [[]] {x, 1, n}, {xs, return   [[x] + xs for xSelect[distinctPartitions[n in- range(1x], n+1) for   xs in distinctPartitions (n-x) if xsLength@# == []0 or|| x < xs[0]] First@# &]}], 1] 

My Mathematica skills are rather rusty, so I'll leave it to you to translate in case you want to try this approach - should be straightforward, hopefully.

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 the same idea implemented in python, additionally using memoization:

from functools import lru_cache @lru_cache(maxsize=None) def distinctPartitions(n):   if n == 0:   return [[]]  return [[x] + xs for x in range(1, n+1) for xs in distinctPartitions (n-x) if xs == [] or x < xs[0]]  

My Mathematica skills are rather rusty, so I'll leave it to you to translate in case you want to try this approach - should be straightforward, hopefully.

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] 
Source Link
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 the same idea implemented in python, additionally using memoization:

from functools import lru_cache @lru_cache(maxsize=None) def distinctPartitions(n): if n == 0: return [[]] return [[x] + xs for x in range(1, n+1) for xs in distinctPartitions (n-x) if xs == [] or x < xs[0]] 

My Mathematica skills are rather rusty, so I'll leave it to you to translate in case you want to try this approach - should be straightforward, hopefully.