@
anubiskong 差远了
求素数同样的算法 nodejs
function sushu (n ){
var result=[];
for (var i=2;i<=n;i++){
var f=true;
var limit=parseInt ( Math.pow (i,0.5 )+1 );
for (var j=2;j<=limit;j++){
if (i%j==0 ){
f=false;
break;
}
}
if (f ){
result.push (i );
}
}
return result;//#print_r ($result );
}
console.log ("time is\t");
var start=(new Date ()).getTime ();
var b=sushu (10000 );
console.log ( ((new Date ()).getTime ()-start )/1000 );
0.005
php
<?php
function sushu ($n ){
$result=[];
for ($i=2;$i<=$n;$i++ ){
$f=true;
$limit=intval (pow ($i,0.5 )+1 );
#printf ("\n".' %s -> %s ',$i,$limit );
for ( $j=2;$j<= $limit; $j++){
if ($i%$j==0 ){
$f=false;
break;
}
}
if ($f ){
$result[]=$i;
}
}
return $result;#print_r ($result );
}
echo "time is\t";
$start=microtime (true );
$result=sushu (10000 );
echo microtime (true )-$start;
time is 0.035001020431519