$| = 1; sub hammingweight { my $n = shift; my $h = 0; while ($n) { if ($n & 1) { $h++; $n--; } $n /= 2; } return $h; } my $S = ""; foreach my $n (1..2**14) { $S .= hammingweight($n-1) % 2; foreach my $s (0..$n) { my $p = substr($S, $s); if ($p eq scalar reverse($p)) { # $p is a palindromic suffix my $pos = index($S, $p); if ($pos>=0 && $pos<$s) { print "$n 0\n"; } else { my $w = length($p); print "$n $w\n"; } last; } } }