Palindromes

May 3, 2007

A palindrome is a number or a text that reads the same forwards and backwards. An object that has an axis of symmetry.

Problem #36 on project Eueler is to find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. it's one of the easier ones on that site. Here is the solution in PHP.

    $sum=0;
    for($i=1 ; $i < 1000001 ; $i++)
    {
            $prod = $i;
            $rev = strrev($i);
            if($prod == $rev)
            {
                $binp  = sprintf("%b",$prod);
                
                $rev = strrev($binp);
                //echo "$binp - > $rev \n";
                if($rev == $binp)
                {
                    printf("Match for %d    -   %b\n",$prod,$prod);
                    $sum = $sum + $prod;
                }
            }
    }
    
    echo "\nTHE SUM = $sum \n";
Posted by raditha at May 3, 2007 7:10 AM
Your Ad Here

 

Jabber  |  Linux  |  mySQL  |  PHP  |  Java  |  Site Map  |  Wiki

Downloads  |  About  |  Links  |  Contact  |  Home

 

Copyright © Raditha Dissanayake 2003 - 2007

Terms of Use  |  Privacy

 

 

May 2007
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31