mayordc
09-19-2003, 04:24 AM
I know that prime numbers are numbers that are divisible only by itself and 1 except 1 itself.
How can I generate the list of prime numbers between 0 and 1000?
please help me!
Gollum
09-19-2003, 05:17 AM
Well, you just need an algorithm to work out what those numbers are.
Something like this will do it...
var primes = new Array;
primes.push(2);// special - only even prime
for (var i = 3; i < 1000; i += 2)
{
var iSqrt = Math.floor(Math.sqrt(i));
var bPrime = true;
for ( j = 0; primes[j] <= iSqrt; j++ )
{
if ( i % primes[j] == 0 )
{
bPrime = false;
break;
}
}
if ( bPrime ) primes.push(i);
}
document.write(primes.join());
Of course you could just calculate them ahead of time (they don't change :p)