//筛选 0-num的素数
function primes(num) {
var s = [];
//初始化0-num的值
for (var i = 0; i <= num; i++) {
s.push(i);
}
//筛掉不合适的值
delete s[0];
delete s[1];
for (var j = 2; j <= num; j++) {
if (!s[j]) continue;//如果 j 是合数
for (var k = 2; j * k <= num; k++) {
delete s[j * k];//筛掉合数
}
}
//去掉被删掉的值
return s.filter(function(x) {
return x;
})
}
var res = primes(10)
console.log(res);
网友评论