[英]Sum of first 1000 prime numbers
I have the below program where I am trying to find the sum of first 1000 prime numbers. 我在下面的程序中尝试查找前1000个质数的总和。 In the code, what's the difference between solution 1, and 2?
在代码中,解决方案1和2有什么区别? Why should I not put the count variable outside the if condition?
为什么不将count变量放在if条件之外? I am obviously not getting the answer I need if I put the variable outside if, but I don't understand why its logically wrong.
如果将变量放在if之外,显然我没有得到所需的答案,但是我不明白为什么它在逻辑上是错误的。 It could be a simple thing, but I am unable to figure it out.
这可能很简单,但是我无法弄清楚。 Experts, please help.
专家,请帮忙。
Solution 1: 解决方案1:
public class SumOfPrimeNumbers {
public static void main(String[] args) {
long result = 0;
int number = 2;
int count = 0;
while (count < 1000) {
if (checkPrime(number) == true) {
result = result + number;
count++;
}
number++;
}
System.out.println("The sum of first 1000 prime numbers is " + result);
}
public static boolean checkPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
} }
Solution 2: 解决方案2:
public class SumOfPrimeNumbers {
public static void main(String[] args) {
long result = 0;
int number = 2;
int count = 0;
while (count < 1000) {
if(checkPrime(number)==true)
{
result = result + number;
}
count++; //The count variable here has been moved to outside the loop.
number++;
}
System.out.println("The sum of first 1000 prime numbers is "+ result);
}
public static boolean checkPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
} }
You should not check the return value of bool
functions for equality to true
: this line 您不应该检查
bool
函数的返回值是否等于true
:此行
if(checkPrime(number)==true)
is equivalent to 相当于
if(checkPrime(number))
Finally, the solution where the count is incremented outside of if
counts non-primes together with primes, producing an obviously wrong result. 最后,在
if
之外将计数加非质数与质数一起计数的解决方案,产生明显错误的结果。
Here are a couple of points "for style" that you should consider: 这是您应该考虑的“针对样式”的几点:
checkPrime
can stop when the candidate divisor is greater than the square root of the number checkPrime
中的候选除数 The place where you increment count
is pretty important. 递增
count
位置非常重要。 Your first code chunk adds up the first 1000 primes, while the second one adds up all the primes less than 1000. 您的第一个代码块将前1000个素数加起来,而第二个则将所有小于1000的素数加起来。
In your solution 2, count
is incremented EVERY time through the loop, regardless of the result of your prime test. 在解决方案2中,无论主要测试的结果如何,
count
都会在循环中每次增加。 So it is not counting primes, but counting iterations through the loop. 因此,它不是在计算素数,而是在循环中计算迭代次数。 As a result, you'll check 1,000 consecutive numbers, not consecutive primes (which involves going through a lot more than 1,000 numbers to accumulate).
结果,您将检查1,000个连续的数字,而不是连续的质数(这涉及到要累加的1,000个数字)。
In addition to what others have pointed out, your check for prime can be made a little more efficient by: 除了其他人指出的以外,您可以通过以下方法使检查质数的效率更高:
public static boolean checkPrime(int number) {
int s = Math.ceil(Math.sqrt(number));
for (int i = 2; i <= s; i++) {
if ((number % i) == 0) {
return false;
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.