简体   繁体   English

Javascript查找素数

[英]Javascript Finding Prime Numbers

I am writing a little script to find and print out all the prime numbers from X thru Y. Here is what I have written: 我正在编写一个小脚本,以查找并打印出X到Y的所有素数。这是我写的:

var numX = prompt('Enter a number greater than 0:','');
var numY = prompt('Enter a number greater than ' + numX + ':','');

while (numX <= numY) {
    if (numX == 1 || numX == 2 || numX == 3) {
    document.write(numX + '</br>');
    } else if (numX % 2 === 0 || numX % 3 === 0 || numX % 5 === 0 || numX % 7 === 0){
    document.write();
    } else {
    document.write(numX + '</br>');
    }
    numX++;
};

Now, this works just fine so long as the first number is 1. If, however, the first number is anything greater than 1 it does not print out anything. 现在,只要第一个数字为1,就可以正常工作。但是,如果第一个数字大于1,则不会打印任何内容。 I am not sure if this is the right forum for this question (perhaps a math forum?), but I thought I would ask here on the off chance someone could help me out. 我不确定这是否是解决此问题的合适论坛(也许是数学论坛?),但我想我会在偶然的机会下问到有人可以帮助我的问题。 I also know that a sieve is the better way to go about this, but I wanted to try and figure this out as a while loop first. 我也知道筛子是解决这个问题的更好方法,但是我想尝试先将其弄清楚。 Any and all help is appreciated! 任何和所有帮助表示赞赏!

While I understand what you are trying to do, I highly recommend taking a look at the Sieve of Eratosthenes . 当我了解您要做什么时,强烈建议您看一下Eratosthenes筛网 You really want to get the hang of knowing different algorithms to compute these things in case you decide to deal with really large numbers. 如果您决定处理非常大的数字,您确实想掌握各种算法来计算这些东西的窍门。 While the way you go about it now might work in smaller ranges, bigger ranges are going to go crazy. 尽管您现在采取的方式可能会在较小的范围内起作用,但较大的范围将变得疯狂。

Also I believe this Stackoverflow question is very similar to this one and the answer for it is very well made: 我也相信这个Stackoverflow问题与这个问题非常相似,并且答案也很正确:

finding sum of prime numbers under 250 查找250以下的素数之和

您可以在这里尝试任何一种选择: http : //www.javascripter.net/faq/numberisprime.htm

Hi i have added bit change to ur code (added condition for 5 and 7 prime numbers) and its working... 嗨,我为您的代码添加了位更改(添加了5和7个质数的条件)及其工作...

var numX = prompt('Enter a number greater than 0:','');
var numY = prompt('Enter a number greater than ' + numX + ':','');

while (numX <= numY) {
    if (numX == 1 || numX == 2 || numX == 3 || numX == 5 || numX == 7) {
    document.write(numX + '</br>');
    } else if (numX % 2 === 0 || numX % 3 === 0 || numX % 5 === 0 || numX % 7 === 0){
    document.write();
    } else {
    document.write(numX + '</br>');
    }
    numX++;
};

Check the demo here 此处查看演示

OK, turns out that I jumped the gun on asking this question. 好的,原来我问了这个问题。 I was more concerned with getting the else if statement working that I failed to even note that my formula was seriously flawed! 我更关心使else if语句起作用,我什至没有注意到我的公式存在严重缺陷!

The issue possibly could be with the second variable. 问题可能与第二个变量有关。 If the first variable is 1 then the second variable can be any number. 如果第一个变量为1,则第二个变量可以为任何数字。 However, if the first variable is greater than 1 then the second variable has to be less than 100 or it will not work. 但是,如果第一个变量大于1,则第二个变量必须小于100,否则它将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM