Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  23] [ 2]  / answers: 1 / hits: 13433  / 10 Years ago, wed, march 26, 2014, 12:00:00

I have just created a prime number finder with a for loop that runs over time. Right now its at 167899 and growing every second.



What is the max number of loops a for loop can go through??? If its an ungodly big number I don't want to be waiting all night to see what is the max prime number I can generate.



Is it infinite?



Here is my code:



        var isNotPrime = false;
var currentNumber = 3;
var primeArray = [2];

function prime(){
isNotPrime = false;
for(a=0; a<primeArray.length; a++){
if(currentNumber%primeArray[a] === 0){
isNotPrime = true;
}
if(a===(primeArray.length-1) && isNotPrime ===false){
document.write(currentNumber+<BR>);
primeArray.push(currentNumber);
}
if(a===(primeArray.length-1)){
currentNumber++;
}
}
}

var main = setInterval(prime, 1);

window.alert(Starting search for Prime Numbers!);

More From » for-loop

 Answers
1

There's no maximum number of times a loop can execute since you can have an infinite loop:



for (var i = 0; i < 1;) { console.log('this will keep running forever' }


(Note that the increment step after i < 1; of the for loop is empty, instead of being i++)



However, there is a max integer in Javascript, which is probably what you were after.


[#46546] Tuesday, March 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iselauniquef

Total Points: 443
Total Questions: 98
Total Answers: 102

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;