in reply to Decomposing sum to unique sets of summands
It's not fast, but that's because of the enormous amount of ways to decompose numbers.sub perms { my ($n, $min) = @_; $min //= 2; return [] if $n <= 0; return [$n] if $n <= $min; my @r; foreach my $i ($min .. $n) { my $left = $n - $i; next if $left < $min; my @p = perms $left, $i; foreach my $p (@p) { push @r, [@$p, $i]; } } @r; } say "[@$_"] for perms 7; __END__ [2 3 2] [3 4] [2 5]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Decomposing sum to unique sets of summands
by dHarry (Abbot) on Oct 28, 2008 at 14:48 UTC | |
by JavaFan (Canon) on Oct 28, 2008 at 15:00 UTC | |
by JadeNB (Chaplain) on Oct 28, 2008 at 17:54 UTC |