Skip to content

Instantly share code, notes, and snippets.

@muayyad-alsadi
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muayyad-alsadi/255c1da7509b0d8ebbda to your computer and use it in GitHub Desktop.
Save muayyad-alsadi/255c1da7509b0d8ebbda to your computer and use it in GitHub Desktop.
primes with php
<?php
function get_primes($n) {
$primes=array();
$compo=array();
for($i=2;$i<$n;++$i) $compo[$i]=false;
for($i=2;$i<$n;++$i) {
if ($compo[$i]) continue;
$primes[]=$i;
for($j=$i*2;$j<$n;$j+=$i) $compo[$j]=true;
}
return $primes;
}
function benchmark($c, $n) {
$t0=microtime(1);
for($i=0;$i<$c;++$i) get_primes($n);
echo microtime(1)-$t0;
}
benchmark(500, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment