use v6; #make a BFS over all possible addition chains sub chain(@c, $l, $n) { for @c Z 1..* {for (@c) Z (($^a) xx $^b) -> $x, $y { my $s = $x + $y; if ($l) { last if $s <= @c[*-1]; my $c = chain(@($s,@c), $l-1, $n) and return $c; } else { last if $s < $n; $s == $n and return (@($s, @c)); } }} return Bool::False; } sub MAIN(Int $n) { $n > 0 or say "Argument must be positive." and return; $n == 1 and say "(1)" and return; my $min = (log($n)/log(2)).ceiling - 1; my $solution = (); until ($solution = chain((1,), $min++, $n)) {}; say "(" ~$solution.reverse.join(", ") ~ ")"; }