import java.util.*;
public class PrimeNum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
for(int i = a ; i <= b ; i++ ) {
if ( i == 2 || i == 3 ) System.out.println(i);
for(int j = 2; j <= (i / 2) ; j++ ) {
if ( (i % j) == 0 ) break;
if ( j == (i / 2) ) System.out.println(i);
}
}
}
}
这个程序很简单,输入 2 int a 和 b。它会在 a 和 b 中找到任何素数。
我怎样才能让它更快?我尝试了 Math.sqrt ,但在这种情况下效果不佳 :( 我真的不知道,因为每当我使用它时,它都会导致很多错误。我希望看到有人在这种情况下使用 Squareroot。