Discuss / JavaScript / 引入Math.sqrt(x)减少不必要的计算量

引入Math.sqrt(x)减少不必要的计算量

Topic source

敏二不二

#1 Created at ... [Delete] [Delete and Lock User]
return arr.filter(function(x){
    if(x===1){
        return false;
    }else{
        for(var i=2;i<=Math.sqrt(x);i++){
            if(x%i===0){
                return false;
            }            
        }
       return true;
    }
});

  • 1

Reply