Last active
August 29, 2015 14:03
-
-
Save muayyad-alsadi/255c1da7509b0d8ebbda to your computer and use it in GitHub Desktop.
primes with php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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