largest

Project Euler - Problem 10

Question

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

Find the sum of all the primes below two million.

Solution

$sum = 2;
for ($i = 2; $i < 2000000; $i++) {
  if ($i % 2 != 1) {
    continue;
  }
  $d = 3;
  $x = sqrt($i);
  while (($i % $d != 0) && $d < $x) {
    $d += 2;
  }
  if ((($i % $d == 0 && $i != $d) * 1) == 0) {
    $sum += $i;
  }
}
echo $sum;

Time taken to execute: 30.5757 second.