use v5.10; use strict; use List::Util qw{sum}; my $product = shift || 72; local our %u; sub f { my @a = @_; @a = sort { $b <=> $a } (length($a[1]), length($a[0])/length($a[1]), $product/length($a[0]) ); local $" = ", "; say "(@a)\t ".sum(@a) unless exists($u{"@a"}); $u{"@a"} = undef; } say "SOL\t\tNUMBER"; my @a = ('1'x$product) =~ /^((1+)\2+)(\1+)$ (?{ f($1, $2, $3) }) (*FAIL) /x; #### $ ./oldestplayspiano.pl SOL NUMBER (18, 2, 2) 22 (12, 3, 2) 17 (9, 4, 2) 15 (8, 3, 3) 14 (6, 6, 2) 14 (6, 4, 3) 13 (36, 2, 1) 39 (24, 3, 1) 28 (18, 4, 1) 23 (12, 6, 1) 19 (9, 8, 1) 18 #### x*y*z = 72 x+y+z = number on that building over there... #### (8, 3, 3) (6, 6, 2)