Java 我的 isPrime 方法有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20798391/
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
What is wrong with my isPrime method?
提问by usama8800
This is my isPrime
method:
这是我的isPrime
方法:
private static boolean isPrime(int num) {
if (num % 2 == 0) return false;
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
return true;
}
I put isPrime(9)
and it returns true
. What is wrong with the method?
我把isPrime(9)
它返回true
。方法有什么问题?
采纳答案by Tareq Salah
Your condition should be i * i <= num
你的条件应该是 i * i <= num
private static boolean isPrime(int num)
{
if (num == 2)
return true;
if (num < 2 || num % 2 == 0)
return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0)
return false;
return true;
}
You didn't take number 9 in your consideration so 9<9 will result false. But you need to check 9.
您没有考虑数字 9,因此 9<9 将导致错误。但是你需要检查9。
回答by NPE
Here are some hints:
这里有一些提示:
The main bug is that you never check divisibility by
sqrt(num)
due to an off-by-one error in the loop.The other bug is that you don't consider
2
to be prime (which it is).
主要错误是
sqrt(num)
由于循环中的一对一错误,您永远不会检查可分性。另一个错误是您不认为
2
自己是素数(确实如此)。
回答by LynxZh
the loop condition with i * i < num
should be i * i <= num
循环条件i * i < num
应为i * i <= num
回答by JosefN
the loop is never executed so it returns directly true
循环永远不会执行,所以它直接返回真
回答by Duleeka Gunatilake
The loop does not run. It gets terminated in the very first value of i because 3 x 3 = 9 it does not meet the condition i * i < n
循环不运行。它在 i 的第一个值处终止,因为 3 x 3 = 9 它不满足条件 i * i < n
回答by Sujith PS
Change your code like this ( check condition) :
像这样更改您的代码(检查条件):
private static boolean isPrime(int num) {
if (num == 2) return true;
if (num % 2 == 0)
return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}
回答by Student
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
i * i
is 9, and 9 in not less than 9, thus for loop is not run.
i * i
是 9,并且 9 不少于 9,因此不会运行 for 循环。
回答by dognose
(Late) Sidenode:
(后期)侧节点:
private static boolean isPrime(int num) {
if (num % 2 == 0) return false;
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
return true;
}
This code is missing 2
; 2
is a primenumber. Everything dividable by 2
isn't, except the 2
- so, use:
缺少此代码2
;2
是素数。一切可被除以2
除2
- 所以,使用:
private static boolean isPrime(int num) {
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
return true;
}
回答by K.Hayoev
my sample:
我的样本:
public boolean isPrime(int x) {
if (x==1) {
return true;
} else {
for(int i=2;i<=Math.sqrt(x);i++) {
if (x%i==0) return false;
}
return true;
}
回答by d.danailov
Java 8: (Example with lambda expression and streams)
Java 8:(使用 lambda 表达式和流的示例)
public static boolean isPrimeFunctionalStyle(int number) {
return number > 1 &&
IntStream.rangeClosed(2, (int) Math.sqrt(number))
.noneMatch(i -> number % i == 0);
}