Java 计算第 10001 个素数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19143200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:28:44  来源:igfitidea点击:

Calculating the 10001st prime number

java

提问by erfan mehraban

I want to calculate the 10001st prime number as in problem 7 euler project. This is what I have done:

我想像问题7euler project 一样计算第 10001 个素数。这就是我所做的:

int i=0;
int counter=2;
while (i<=10001){
    counter++;
    if (Helper.isPrime(counter))
        i++;
}
Helper.println(counter);

It is returning 104033but the correct answer is 104743. Where is my problem?

它正在返回,104033但正确答案是104743。我的问题在哪里?

采纳答案by Ted Hopp

You have three problems with your code:

您的代码存在三个问题:

  1. Your code does not test whether 2 is a prime, so you are missing that in your count. You should initialize counterto 0 or 1, not 2.
  2. By using while (i<=10001), you are counting until you find 10002 primes. Since you are also not counting 2, you are going two steps past where you need to go. The loop test should be while (i<10001).
  3. Since your answer is lowerthan the correct answer (even though your loop went two primes past where it should have stopped), your Helper.isPrimemethod is clearly identifying too many numbers as prime. It will need to be fixed, but since you have not posted the code for it, it's impossible to say what is needed to fix it.
  1. 您的代码不会测试 2 是否为素数,因此您在计数中遗漏了它。您应该初始化counter为 0 或 1,而不是 2。
  2. 通过使用while (i<=10001),您一直在数数直到找到 10002 个素数。由于您也不计算 2,因此您将越过您需要去的地方两步。循环测试应该是while (i<10001).
  3. 由于您的答案低于正确答案(即使您的循环经过了两个素数,它应该停止),您的Helper.isPrime方法显然将太多数字识别为素数。它需要修复,但由于您尚未发布它的代码,因此无法说明需要修复它的内容。

回答by coder

correct version

正确的版本

int i=0;
int counter=1; // chnage initial value of counter
while (i<10001){ // change terminating condition
    counter++;
    if (Helper.isPrime(counter))
        i++;
}
Helper.println(counter);

Explanation/Issues with your version

您的版本的说明/问题

First solve it for smaller subset, suppose you want to print 3rd prime number. As per your code

首先解决较小的子集,假设您要打印第三个素数。根据您的代码

i=0, counter  =3, is 3 prime yes i=1
i=1  counter = 4 is 4 prime No i =1
i=1 counter = 5 is 5 prime Yes i=2
i=2 counter =6 is 6 prime No i=2
i=2 counter =7 is 7 prime yes i=3
i=3 counter =8 is 8 prime no i=3
i=3 counter = 9 is 9 prime no i=3
i=3 counter =10 is 10 prime no i=3
i=3 counter =11 is 11 prime yes i=4
loop terminates
ans: 11

is 11 2nd prime, No

是 11 第二素数,否

issues:

问题:

  1. why counter starting with 2
  2. termiating condition of loop, evn if the number is found we are not terminating
  1. 为什么计数器以 2 开头
  2. 循环的终止条件,即使找到了数字,我们也不会终止

回答by upog

try

尝试

   int find=10001;
   int counter =1;
   int currentNumber=2;
   while (counter <= find){
       if(Helper.isPrime(currentNumber)){
           counter++;              
       }
       currentNumber++;
   }
    System.out.println(currentNumber-1);