// you’re reading...

Project Euler Solutions

Project Euler Problem 36 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

Analysis

Since leading zeros are not allowed, the top and bottom bits for binary numbers have to be one in order to be palindromic. This eliminates even numbers from consideration.

Solution

Runs < 2 second in Perl.

$s=0; 
$limit = 1e6;
 
for (my $n=1; $n<$limit; $n+=2) { 
  $s += $n if palindromic($n) && palindromic(dec2bin($n));
} 
print "Answer to PE36 = $s"; 
 
sub dec2bin { sprintf("%b",shift) }
sub palindromic { @_[0] == reverse @_[0] }

Comments

Results < one trillion:
{ 1, 3, 5, 7, 9, 33, 99, 313, 585, 717, 7447, 9009, 15351, 32223, 39993, 53235, 53835,
73737, 585585, 1758571, 1934391, 1979791, 3129213, 5071705, 5259525,
5841485, 13500531, 719848917, 910373019, 939474939, 1290880921, 7451111547,
10050905001, 18462126481, 32479297423, 75015151057, 110948849011, 136525525631 }

Discussion

No comments for “Project Euler Problem 36 Solution”

Post a comment