#!/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"); } }