zall
zall

Reputation: 585

For Loop Max JavaScript

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!");

Upvotes: 0

Views: 15111

Answers (4)

Mile Mijatović
Mile Mijatović

Reputation: 3177

Maximum safe integer in JavaScript is 9007199254740991 , you can check it in console of your browser

console.log('Max safe integer' , Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log('Min safe integer' , Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log('Max value' , Number.MAX_VALUE); // 1.7976931348623157e+308
console.log('Min value' , Number.MIN_VALUE); // 5e-324

So you can run

for (let i = 1; i <= Number.MAX_SAFE_INTEGER; i ++) {
  // console.log('Number ', i); <-- Uncomment this line and wait, but why? I don't recommend, because your browser will crash probably 
}

Upvotes: 0

Femi
Femi

Reputation: 1322

You will probably need to use a web worker to prevent the browser from interrupting the process. With a web worker, it is possible for the loop to run indefinitely (well, until the computer ceases to function).

As noted by others, you will run into issues dealing with the large numbers. There may be ways to work around the integer precision limitation in JavaScript: http://www.2ality.com/2012/07/large-integers.html

So, the answer to your first question is no, JavaScript doesn't have a well-defined maximum number of loops.

The answer to your second question depends on where the process is running. The number of loops cannot be infinite if the process is running on a real machine which will eventually suffer failure due to entropy. If the process is running in a virtual machine, you can keep the process running indefinitely (until the heat death of the universe).

Upvotes: 1

Cyassin
Cyassin

Reputation: 1490

As long as the length is an integer, it can and will continue the loop. The max number is believe for an int is 9 007 199 254 740 992. So loop has the potential to run that many times.

Upvotes: 2

mralexlau
mralexlau

Reputation: 2003

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.

Upvotes: 3

Related Questions