use v6; sub merge(@cc) { gather { my @a := @cc.shift; take @a.shift; my @b := merge(@cc); loop { @a[0].key == @b[0].key and @b[0].value.push(@a.shift.value[]); take @a[0].key < @b[0].key ?? @a.shift !! @b.shift; } } } sub MAIN(Int $count) { #build matrix of cubic sums with their string representation my @csums := (1..*).map: -> $o {$(($o..*).map: { ($_**3+$o**3 => ["$_ ** 3 + $o ** 3"]) }) }; #merge the matrix into a sorted list and filter out only the multiple hits my @results := merge(@csums).grep:{.value.elems > 1}; #make pretty strings out of the results my @pretty := @results.map({"$_.key() = " ~ .value.join(" = ")}); #print the results. This is where all the work is done. @pretty.shift.say for ^$count; }