Pythogorean triplets

May 31, 2007

We all know of the Pythagorean theorem (Well if you don't know that one, you probably stumbled to the wrong place, there wouldn't be anything of interest to you here).

A Pythogorean triplet is the set of three integers that figure in the equation. At Project Euler, the 9th problem is to find a triplet so that a+b+c = 1000

It's not the most difficult problem listed there.
    for($a=0 ; $a < 1000 ; $a++)
    {
        for($b = 0 ; $b < 1000 ; $b++)
        {
            $c2 = pow($a,2) + pow($b,2);
            $c = sqrt($c2);
            //echo "$a, $b, $c \n";
            if(ceil($c) == floor($c))
            {
                // int
                if(($a+$b+$c)== 1000)
                {
                    echo "MATCH for $a, $b, $c | product = " . ($a*$b*$c) ."\n";
                    
                }
            }
        }
    }
Posted by raditha at May 31, 2007 4:13 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