for my $n (1..20) { print "$n:\t"; for my $M (2..5) { my $remaining = ($n-1) % $M; # subtract one from the remainder my $next_multiple = $n-1 + $M - $remaining; print "$next_multiple\t"; }; print "\n"; };