Project Euler in PHP

Project Euler is a series of challenging mathematical/computer programming problems, their website is http://www.projecteuler.net

  1. Add all the natural numbers below one thousand that are multiples of 3 or 5.
    PHP Code:
    $y = array();
    for($i=1;$i<1000;$i++){
        if (!($i%3)||!($i%5)){
            $y[] = $i;
        }
    }
    echo array_sum($y);


    Answer: 233,168

  2. Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
    PHP Code:
    $total=0;
    $a=1;
    $b=1;
    for ($c=$a+$b;$c<4000000;$c){
        $total=$total+$c;
        $a=$b+$c;
        $b=$c+$a;
        $c=$a+$b;
    }
    echo $total;


    Answer: 4,613,732

  3. Find the largest prime factor of a composite number. (600851475143 in this question)
    PHP Code:
    $n = 600851475143;<br />
    $d = 2;
    while ($n > 1) {
        if (($n % $d) == 0) {
            $n /= $d;
            $d--;
        }
        $d++;
    }
    echo $d;


    Answer: 6,857

  4. Find the largest palindrome made from the product of two 3-digit numbers.
    PHP Code:
    $answer = NULL;
    for($a = 100; $a <= 999 ; $a++){
        for($b=100;$b<=999;$b++){
            $c = $a*$b;
            if ($c == strrev($c)){
                if($c > $answer){
                    $answer = $c;
                }
            }
        }
    }
    echo $answer;


    Answer: 906,609

  5. What is the smallest number divisible by each of the numbers 1 to 20?
    PHP Code:
    $i = 1;
    $x = 1;
    while ($x <= 20) {
        if ($i%$x == 0) {
            $x++;
        } else {
            $i++;
            $x = 1;
        }
    }
    echo $i;


    Answer: 232,792,560

  6. What is the difference between the sum of the squares and the square of the sums?
    PHP Code:
    $b = 0;
    for($a=1;$a<=100;$a++){
        $b += $a*$a;
    }
    $c = 0;
    for($a=1;$a<=100;$a++){
        $c += $a;
    }
    $c = $c*$c;
    $answer = $b-$c;
    echo $answer;


    Answer: 25164150

  7. Find the 10001st prime.
    PHP Code:
    function prime($n){
        if($n%2 == 0){
            return 0;
        }
        $temp = ceil(sqrt($n)) +1;
        while($temp > 1){
            if($n % $temp == 0){
                return 0;
            }
            $temp -= 1;
        }
        return 1;
    }
    $t = 7;
    $count = 3;
    while($count < 10001){
        $bool = prime($t);
        $t += 1;
        if($bool == 1){
            $count += 1;
        }
    }
    print $t-1;


    Answer: 104,743

  8. Discover the largest product of five consecutive digits in the 1000-digit number (shown below).
    PHP Code:
    $n = '7316717653 1330624919 2251196744 2657474235 5349194934 9698352031 2774506326 2395783180 1698480186 9478851843 8586156078 9112949495 4595017379 5833195285 3208805511 1254069874 7158523863 0507156932 9096329522 7443043557 6689664895 0445244523 1617318564 0309871112 1722383113 6222989342 3380308135 3362766142 8280644448 6645238749 3035890729 6290491560 4407723907 1381051585 9307960866 7017242712 1883998797 9087922749 2190169972 0888093776 6572733300 1053367881 2202354218 0975125454 0594752243 5258490771 1670556013 6048395864 4670632441 5722155397 5369781797 7846174064 9551492908 6256932197 8468622482 8397224137 5657056057 4902614079 7296865241 4535100474 8216637048 4403199890 0088952434 5065854122 7588666881 1642717147 9924442928 2308634656 7481391912 3162824586 1786645835 9124566529 4765456828 4891288314 2607690042 2421902267 1055626321 1111093705 4421750694 1658960408 0719840385 0962455444 3629812309 8787992724 4284909188 8458015616 6097919133 8754992005 2406368991 2560717606 0588611646 7109405077 5410022569 8315520005 5935729725 7163626956 1882670428 2524836008 2325753042 0752963450';
    $answer = NULL;
    for($i = 0; $i <= 994; $i++){
        $a = substr($n, $i, 5);
        $b = $a[0] * $a[1] * $a[2] * $a[3] * $a[4];
        if ($b > $answer){
            $answer = $b;
        }
    }
    echo $answer;


    Answer: 40824

  9. Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
    PHP Code:
    $productanswer = 'Find Me Please';
    for($a = 1; $a <= 998; $a++){
        for($b = $a; ($b+$a)<=999 ;$b++){
            $c = 1000 - $a - $b;
            if (($a*$a)+($b*$b) == $c*$c){
                $productanswer = $a*$b*$c;
                $found = 1;
                break 2;
            }
        }
    }
    echo $productanswer;


    Answer: 31875000

  10. Calculate the sum of all the primes below two million.
    PHP Code:
    $n = 2000000;
    $p = 2;
    $list = range($p, $n);
    $i = 0;
    while($p*$p < $n){
        for($j=$i+1; $j < $n; $j++){
            if($list[$j] % $p == 0){
                unset($list[$j]);
            }
        }
        sort ($list);
        $i = $i + 1;
        $p = $list[$i];
    }
    echo array_sum($list);


    Answer: 142,913,828,922


  11. Coming Soon

  12. What is the value of the first triangle number to have over five hundred divisors?
    PHP Code:
    $n = 0;
    $i = 1;
    do{
        $n += $i;
        $d = 0;
        $sqrt = round(sqrt($n));
        for($j = 1; $j<= $sqrt; $j++){
            if($n % $j == 0){
                $d += 2;
            }
        }
        if ($sqrt * $sqrt == $n) {
            $d--;
        }
        $i++;
    }while($d < 500);
    echo $n;


    Answer: 76,576,500


  13. Coming Soon


  14. Coming Soon

  15. Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.
    How many routes are there through a 20×20 grid?

    PHP Code:
    $gridSize = 20;
    for ($i = 0; $i < $gridSize; $i++) {
        $grid[$i][$gridSize] = 1;
        $grid[$gridSize][$i] = 1;
    }
    for ($i = $gridSize - 1; $i >= 0; $i--) {
        for ($j = $gridSize - 1; $j >= 0; $j--) {
            $grid[$i][$j] = $grid[$i+1][$j] + $grid[$i][$j+1];
        }
    }
    echo $grid[0][0];


    Answer: 137,846,528,820


  16. Coming Soon


  17. Coming Soon


  18. Coming Soon


  19. Coming Soon


  20. Coming Soon


  21. Coming Soon


  22. Coming Soon


  23. Coming Soon


  24. Coming Soon


  25. Coming Soon


  26. Coming Soon


  27. Coming Soon


  28. Coming Soon


  29. Coming Soon

  30. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
    PHP Code:
    $result = 0;
    for ($i = 2; $i < 355000; $i++) {
        $sumOfPowers = 0;
        $number = $i;
        while ($number > 0) {
            $d = $number % 10;
            $number /= 10;
            $temp = $d;
            $temp = pow($d, 5);
            $sumOfPowers += $temp;
        }
        if ($sumOfPowers == $i) {
            $result += $i;
        }
    }
    echo $result;


    Answer: 443,839

  31. In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
    1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
    How many different ways can £2 be made using any number of coins?

    PHP Code:
    $target = 200;
    $coins = array( 1, 2, 5, 10, 20, 50, 100, 200 );
    $ways[0] = 1;
    for ($i = 0; $i < count($coins); $i++) {
        for ($j = $coins[$i]; $j <= $target; $j++) {
            $ways[$j] += $ways[$j - $coins[$i]];
        }
    }
    echo $ways[count($ways)-1].' ways to make &pound;2';


    Answer: 73,682

  32. Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
    PHP Code:
     

    Answer:

  33. The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
    We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
    There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
    If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

    PHP Code:
    $denproduct = 1;
    $nomproduct = 1;
    function gcd($a, $b) {
        $y = 0;
        $x = 0;
        if ($a > $b) {
            $x = $a;
            $y = $b;
        } else {
            $x = $b;
            $y = $a;
        }
        while ($x % $y != 0) {
            $temp = $x;
            $x = $y;
            $y = $temp % $x;
        }
        return $y;
    }
    for ($i = 1; $i < 10; $i++) {
        for ($den = 1; $den < $i; $den++) {
            for ($nom = 1; $nom < $den; $nom++) {
                if (($nom * 10 + $i) * $den == $nom * ($i * 10 + $den)) {
                    $denproduct *= $den;
                    $nomproduct *= $nom;
                }
            }
        }
    }
    $denproduct /= gcd($nomproduct, $denproduct);
    echo $denproduct;


    Answer: 100


  34. Coming Soon


  35. Coming Soon


  36. Coming Soon


  37. Coming Soon


  38. Coming Soon


  39. Coming Soon


  40. Coming Soon


  41. Coming Soon


  42. Coming Soon


  43. Coming Soon


  44. Coming Soon


  45. Coming Soon


  46. Coming Soon


  47. Coming Soon


  48. Coming Soon


  49. Coming Soon

  50. Which prime, below one-million, can be written as the sum of the most consecutive primes?
    Coming Soon

There will be more solved here soon.