方法1:alert("2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97")
方法2:
function prime(n){var primeArr = [2]
var isPrime = function(num){
for(var i=1 i<=numi++){
for(var j=2j<ij++){
if(i%j==0){
return false
break
}
}
}
return true
}
if(isNaN(n) || n < 1){
return []
}
for(var i = 3, i < n i++){
if(isPrime(i)){
primeArr.push(i)
}
}
return primeArr
}
prime(100)
参考下面代码function isprime(x)
{
for(var i = 2i <sqrt(x)i++)
if(isdiv(x, i) == 0) return false
return true
}
function getNum(MaxNum ){var count = 1
var PrimeArys = [2]
var PrimeTemp = []
var oNum = Math.ceil( Math.sqrt( MaxNum ) )
for(i=3 i<oNum i+=2){
if( PrimeTemp[i]==null){
for(j=i i*j<=MaxNum j+=2){
PrimeTemp[ i*j ] = 0
}
}
}
for(i=3 i<=MaxNum i+=2){
if( PrimeTemp[i]==null)
PrimeArys[ count++ ] = i
}
return PrimeArys
}
想要1000以内的素数(质数)就调用这个方法getNum(1000)