admin管理员组文章数量:1316304
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!");
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!");
Share
Improve this question
asked Mar 26, 2014 at 0:41
zallzall
5851 gold badge12 silver badges25 bronze badges
2
- possible duplicate of How to calculate that to what maximum number of times for loop can run in Javascript? – bjb568 Commented Mar 26, 2014 at 0:44
- you can also limit the loop to the max int of javascript 2^52. For very large primes I remend using a library that supports very large numbers, C and Python use: gmpy2.readthedocs/en/latest/mpz.html#mpz-methods – bitoiu Commented Mar 26, 2014 at 0:47
4 Answers
Reset to default 3There'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.
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.
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 puter 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./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).
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); <-- Unment this line and wait, but why? I don't remend, because your browser will crash probably
}
本文标签: For Loop Max JavaScriptStack Overflow
版权声明:本文标题:For Loop Max JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002486a2411295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论