3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function get_primes7($n) { if ($n < 2) return array(); if ($n == 2) return array(2); $s = array(); for($i = 3; $i <= $n; $i += 2) $s[] = $i; $mroot = sqrt($n); $half = count($s); $i = 0; $m = 3; while ($m <= $mroot) { if ($s[$i]) { $j = (int)(($m*$m - 3) / 2); $s[$j] = 0; while ($j < $half) { $s[$j] = 0; $j += $m; } } $i = $i + 1; $m = 2*$i + 3; } $res = array(2); foreach ($s as $v) { if ($v) { $res[] = $v; } } return $res; } $res = array(); for ($i = 1; $i <= 10; ++$i) { $res = get_primes7(50000); print "Found ".count($res)." prime numbers.\n"; }
based on dt3fl
Output for 4.3.0 - 5.1.6, 5.2.1 - 7.1.0
Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers.
Output for 5.2.0
Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers. Found 5133 prime numbers.
Process exited with code 137.