T1.第 10001 个质数(11.18)

题目

列出前六个质数,我们可以发现第六个质数为 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);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_51517771/article/details/121434256
今日推荐