//PHP取出素数并且求和
1 function primes($n,$s = false) { 2 $primes = array_fill(2,$n - 1,‘prime‘); 3 for ($i = 2,$range = intval(sqrt($n)); $i < $range ;$i++) { 4 if ( !empty($primes[$i]) ) { 5 foreach ($primes as $key => $item) { 6 for ($j = 2 * $i; $j <= $n; $j += $i) { 7 unset($primes[$j]); 8 } 9 } 10 } 11 } 12 13 $primes = array_keys($primes); 14 if ($s) { 15 $sum = 0; 16 foreach ($primes as $k => $v) { 17 $sum += $v; 18 } 19 $primes[‘sum‘] = $sum; 20 } 21 print_r($primes); 22 } 23 24 primes(100,true);