ProjectEuler.net
 |   |   |   |   |   |   |   |   |   | 

Project Euler.net Answers

Problem 10

Solved on 2/26/2008

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

Find the sum of all the primes below two million.

Note: This problem has been changed recently, please check that you are using the right parameters.



0

$number = (int)$_POST['number'];
$sum = 5;
if($number <= 2000000 && $number >= 3){
for($ic = 6; $ic <= $number+1; $ic+=6){
if($this->isPrime($ic-1)) $sum+= $ic-1;
if($ic <= $number && $this->isPrime($ic+1)) $sum+= $ic+1;
}
}


public function isPrime($val){
if(($val % 2 ) == 0) return FALSE;
for($c = 3; $c <= sqrt($val); $c+=2) {
if(($val % $c ) == 0) return FALSE;
}
return TRUE;
}