题目要求
解题思路
这个题最好还是写一个函数来判断是不是质数,要注意 i 的最大值是n的开方。求值循环的时候记得%50000以免位数超限 。
import java.util.Scanner;
public class Main {
public static boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int total = 0;
int cheng = 1;
for (int i = 2; total < n; i++) {
if (isPrime(i)) {
total++;
cheng *= i;
cheng %= 50000;
}
}
System.out.println(cheng);
}
}