0

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!");
Zachooz
  • 535
  • 1
  • 12
  • 25
  • possible duplicate of [How to calculate that to what maximum number of times for loop can run in Javascript?](http://stackoverflow.com/questions/8256785/how-to-calculate-that-to-what-maximum-number-of-times-for-loop-can-run-in-javasc) – bjb568 Mar 26 '14 at 00:44
  • you can also limit the loop to the max int of javascript 2^52. For very large primes I recommend using a library that supports very large numbers, C and Python use: https://gmpy2.readthedocs.org/en/latest/mpz.html#mpz-methods – bitoiu Mar 26 '14 at 00:47

4 Answers4

3

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.

Community
  • 1
  • 1
mralexlau
  • 1,823
  • 1
  • 16
  • 23
2

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.

Cyassin
  • 1,437
  • 1
  • 15
  • 31
  • So that means if I can change the loop every 9 007 199 254 740 991 and it will continue again for another 9 007 199 254 740 991? throwing memory error should run fine right? – Shiz Dec 03 '20 at 08:15
1

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).

Femi
  • 1,332
  • 9
  • 20
0

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 
}
Mile Mijatović
  • 2,948
  • 2
  • 22
  • 41