# fastest my @a = (1); for (1..10) { my $s = @a; while ($s-- and my $e = shift @a) { push @a, 1, $e; $a[-2]++, $s--, shift @a while $a[0] == $e and $s; } } # shortest my @a = (1); for (1..10) { my $i; @a = map $i++ % 2 : length : $_, join("", @a) =~ /((\d)\2*)/g; }