#!/usr/bin/perl use strict; use warnings; my $cards = 5; my $holes = 3; my @arrangements = arrange(0, $cards, $holes); print "@$_\n" for @arrangements; sub arrange { my ($usedHoles, $remainingCount, $totalHoles) = @_; my @arrangements; return [$remainingCount] if ++$usedHoles == $totalHoles; for my $thisCount (1 .. $remainingCount - ($totalHoles - $usedHoles)) { my @subArrangements = arrange($usedHoles, $remainingCount - $thisCount, $totalHoles); push @arrangements, map {[$thisCount, @$_]} @subArrangements; } return @arrangements; } #### 1 1 3 1 2 2 1 3 1 2 1 2 2 2 1 3 1 1 #### #!/usr/bin/perl use strict; use warnings; my $cards = 5; my $holes = 3; arrange(0, $cards, $holes, ''); sub arrange { my ($usedHoles, $remainingCount, $totalHoles, $prefix) = @_; if (++$usedHoles == $totalHoles) { print "$prefix $remainingCount\n"; return; } for my $thisCount (1 .. $remainingCount - ($totalHoles - $usedHoles)) { arrange($usedHoles, $remainingCount - $thisCount, $totalHoles, "$prefix $thisCount"); } }