[英]Check if an int is prime Java
sorry for the "fix my code" post 对不起“修复我的代码”帖子
EDIT: related more to syntax of a for
loop than prime numbers, also solved now. 编辑:更多地涉及
for
循环的语法而不是素数,现在也解决了。
My task is to take an int from the console and print out (on separate lines) all the prime numbers from 1 to n inclusive. 我的任务是从控制台获取一个int并打印(在单独的行上)从1到n的所有素数。 My method starts at n, checks if its prime, then increments n down by one and loops until n=2.
我的方法从n开始,检查它是否为素数,然后将n递减1并循环直到n = 2。 To check if a number is prime I run a loop, checking is the remainder of diving the number by x is equal to zero, with x starting at 2 and stopping at root(n).
为了检查一个数字是否为素数,我运行一个循环,检查剩余的潜水数量x等于零,x从2开始并在root(n)处停止。 Now this all works in theory, and reading my code I don't see where it goes wrong.
现在这一切都在理论上有效,并且阅读我的代码我看不出它出了什么问题。
public class Prime {
public static boolean isPrime(int n) {
boolean result = true;
for (int x = 2; x>=sqrt(n); x++) {
if ((n % x) == 0) {
result = false;
break;
} else {
x++;
}
}
return result;
}
public static void main(String[] args) {
Scanner intIn = new Scanner(System.in);
int i = intIn.nextInt();
while (i>=2) {
if (isPrime(i)) {
System.out.println(i);
i--;
} else {
i--;
}
}
}
}
For example an input of 10 will return 10 (along with 9,8,7,6,5,3), even though isPrime() checks if 10 % 2 == 0, then sets result
to false. 例如,输入10将返回10(以及9,8,7,6,5,3),即使isPrime()检查10%2 == 0,然后将
result
设置为false。 What am I missing here?? 我在这里失踪了什么?
Again I apologise for the annoying (slightly duplicate) question. 我再次为烦人(略微重复)的问题道歉。
The condition in the for
loop is the condition to continue the loop, not the condition to stop it . for
循环中的条件是继续循环的条件,而不是停止循环的条件。 You need to replace >=
with <=
: 您需要更换
>=
与<=
for (int x = 2; x<=sqrt(n); x++) {
// Here -----^
You are incrementing x twice, and the loop's condition should be x<=sqrt(n)
: 你正在递增x两次,循环的条件应该是
x<=sqrt(n)
:
for (int x = 2; x>=sqrt(n); x++) { // here
if ((n % x) == 0) {
result = false;
break;
} else {
x++; // and here
}
}
The correct logic should be: 正确的逻辑应该是:
public static boolean isPrime(int n) {
for (int x = 2; x<=sqrt(n); x++) {
if ((n % x) == 0) {
return false;
}
}
return true;
}
在循环中x必须小于或等于所以将(int x = 2; x> = sqrt(n); x ++)的表达式更改为for(int x = 2; x <= sqrt(n); x ++)
Try it this way, it's much clearer and concise. 试试这种方式,它更清晰简洁。
public static boolean isPrime(int candidate) {
int candidateRoot = (int) Math.sqrt((double) candidate);
return IntStream.rangeClosed(2, candidateRoot)
.noneMatch(i -> candidate % i == 0); // return true if the candidate
// isn't divisible for any of the
// numbers in the stream
}
public static void main(String[] args) {
Scanner intIn = new Scanner(System.in);
int i = intIn.nextInt();
List<Integer> primeList = IntStream.rangeClosed(2, i)
.filter(candidate -> isPrime(candidate))
.boxed()
.collect(toList());
System.out.println(primeList);
// Another way
Map<Boolean, List<Integer>> primeAndNotPrimeMap = IntStream.rangeClosed(2, i)
.boxed()
.collect(partitioningBy(candidate -> isPrime(candidate)));
System.out.println(primeAndNotPrimeMap);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.