素数:
<script type="text/javascript">var i,j,m,n=o
for (i=0i<=1000i++)
{
for(m=1,j=2j<=(int)sqrt(i)j++)
if(i%j==0) {m=0break}
if(m==1)
{
document.write(i)
n++
if(n%6==0) document.writeln()
}
}
质数:
var a = [2,3,5]for (var i=7 i<1000 i+=2) {
if (check(i)) a.push(i)
}
alert(a)
function check(n){
for (var i=0 i<a.length i++) {
if (a[i] * a[i] > n) return true
if (n % a[i] === 0) return false
}
}
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)