Prime测试,2位数字
问题描述:
我想要打印所有长度为2位数的素数。这是我的代码:
I want to print all prime numbers that are 2-digits long. Here is my code:
for(int input = 11; input <= 99; input += 2){
for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){
if(input%x != 0){
System.out.println(input);
break;
}else{
break;
}
}
}
问题在于打印35或49之类的数字不是素数。
The problem is that it prints numbers like 35 or 49 which are not prime numbers.
答
这有效。您可以在此处查看输出。
This works. You can see the output here.
public class Main {
public static void main(String[] args) {
for(int input = 11; input <= 99; input += 2){
boolean found = false;
for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){
if(input%x == 0){
found = true;
break;
}
}
if(!found) {
System.out.println(input);
}
}
}
}