双进制回文数

作者:xenu

https://projecteuler.net/problem=36

十进制数 585 = 1001001001_2(二进制)在两种进制下都是回文数。

求所有小于一百万的十进制和二进制都是回文数的数字之和。

(请注意,任何进制下的回文数都不能包含前导零。)

源代码:prob036-xenu.pl

use v6;

#
# DON'T TRY  EXECUTING THIS SCRIPT WITH RAKUDO 2010.08 OR OLDER!
# Because of memory leak in these versions, this code eats tens of gigabytes of RAM
#

sub MAIN(Bool :$verbose = False) {
    my $palindromNumbersSum = 0;
    loop (my $i = 1; $i <= 999999; $i+=2) {
        if ( ($i.flip == $i) && (sprintf('%b',$i).flip == sprintf('%b',$i)) ) {
            $palindromNumbersSum += $i;
        }
        if $verbose {
            say "Checked $i of 999999 numbers" unless $i % 99999;
        }

    }
    if $verbose {
        say "Number of double-base palindromes: $palindromNumbersSum";
    }
    else {
        say $palindromNumbersSum;
    }
}