Project Euler Problem 10 (PHP)

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Script

Execution Time: 88.6965 seconds

function isPrime($n)
{
    for ($x=2; $x<=sqrt($n); $x++) {
        if ($n%$x == 0) {
            return false;
        }
    }

    return true;
}

$maxNum = 2000000;
$sum = 2+3+5+7;

for ($x = 11; $x < $maxNum; $x=$x+2) {
    if ($x % 1000 == 1) echo $x.PHP_EOL;
    if ($x&5 > 0 && isPrime($x)) {
        $sum += $x;
    }
}

echo $sum.PHP_EOL;

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
This entry was posted in PHP, Programming, Project Euler and tagged , , , . Bookmark the permalink.

Comments are closed.