题目链接:1013 数素数
代码
/**
* Score: 20
* Run Time: 124ms
* @author wowpH
* @version 1.0
*/
import java.util.Scanner;
public class Main {
// num是素数返回true,num不是素数返回false
private static boolean prime(int num) {
int max = (int) Math.sqrt(num);
for (int i = 2; i <= max; i++) {
if (0 == num % i) {
return false;
}
}
return true;
}
public static void main(String[] args) {
// 输入
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
sc.close();
int count = 1;// 下一个素数的下标,下标从1开始
int num = 2;
// 排除第M个素数之前的素数
while (count < M) {
if (prime(num++)) {
count++;
}
}
int col = 0; // 列,0~9
// 输出第M至N个素数
while (count <= N) {
if (prime(num)) {
if (count == N || 9 == col) {
System.out.println(num);
} else {
System.out.print(num + " ");
}
col = (col + 1) % 10;
count++;
}
num++;
}
}
}