English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2 answers

Here's a real basic script to generate prime numbers.

var aryPrimes = new Array();
var bolIsPrime;

for( var intValue = 2; intValue <= 100; intValue++) {

bolIsPrime = true;
for( var intIndex = 0; intIndex < aryPrimes.length; intIndex++) {
if( intValue % aryPrimes[ intIndex] == 0) {
bolIsPrime = false;
}
}
if( bolIsPrime) {
aryPrimes[ aryPrimes.length] = intValue;
}
}

document.write( aryPrimes.join( ","));


There are much betters ways of finding prime numbers, however, this should be simple enough for you to understand what's going on. There are several ways to optimize this script such as alternately incrementing intValue by 2 and 4. Give this a try starting with the number 5 and adding 2 to it, then adding 4 to the result, then adding 2, and so on. Try this again starting with the number 3 and compare the results. You'll notice that you can skip a lot of numbers which aren't prime anyways, 2/3rds to be exact.

Here's a hint on how to do that.
intValue = (intIncrement = 6 - intIncrement)

I'll leave it up to you on how to actually implement that. :)

2006-11-20 10:49:41 · answer #1 · answered by Kookiemon 6 · 0 0

There are 25 prime numbers between 1 and 100. They are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 and 97. The number 1 is NOT prime, and neither are 57 87 91 or 93.

2016-03-19 11:43:55 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers