Home > php教程 > php手册 > 10000之内求素数

10000之内求素数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:39:25
Original
1441 people have browsed it

无详细内容 nodejs v0.12.4function 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;//#pr

nodejs v0.12.4

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 \n");
var start=(new Date()).getTime();
var b=sushu(10000);
console.log( ((new Date()).getTime()-start)/1000);

 

 
 php v7
<?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 \n";
$start=microtime(true);
$result=sushu(10000);

echo microtime(true)-$start;
 
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template