文章目录
题目
列出前六个质数,我们可以发现第六个质数为 13,那么第 10001 个质数是多少?
解析(暴力求解为主)
直接从2开始遍历,判断是否为质数,直到找到第10001个质数遍历结束
质数(素数)的判断
//判断是否为质数
public static boolean isPrime(int temp){
// 判断temp是否为质子数
int max=(int)Math.sqrt(temp);
for(int i=2;i<=max;i++){
if(temp%i==0){
// 表明不是质数
return false;
}
}
return true;
}
代码
package edu.wust.competiton;
public class chapter6 {
//判断是否为质数
public static boolean isPrime(int temp){
// 判断temp是否为质子数
int max=(int)Math.sqrt(temp);
for(int i=2;i<=max;i++){
if(temp%i==0){
// 表明不是质数
return false;
}
}
return true;
}
public static void main(String[] args) {
int i=0;
for(int j=2;i!=10001;j++){
if(isPrime(j)){
i++;
}
if(i==10001){
System.out.println(j);
}
}
}
}