寻找素数

题干

找到n以内的所有素数

 

方法一:穷举法

找10000以内的素数

package Important;

public class primeSieve {

    public static void printNum(int[] num,int count){
        int length=num.length;
        for(int i=0;i<count;i++){
            System.out.println(num[i]);
        }
    }

    public static void primeSelect(int num){
        int[] res=new int[num+1];
        int count=0;
        res[0]=2;
        count++;
        for(int i=3;i<=num;i++){
            int flag=0;
            for(int j=2;j<i;j++){
                if(i%j==0){
                    flag=1;
                }
            }
            if(flag==0){
                res[count++]=i;
            }
        }
        printNum(res,count);
    }

    public static void main(String[] args){
        primeSelect(10000);
    }
}

我们给这个函数加上时间监控

public static void primeSelect1(int num){
        Long startTime = System.currentTimeMillis();
        int[] res=new int[num+1];
        int count=0;
        res[0]=2;
        count++;
        for(int i=3;i<=num;i++){
            int flag=0;
            for(int j=2;j<i;j++){
                if(i%j==0){
                    flag=1;
                }
            }
            if(flag==0){
                res[count++]=i;
            }
        }
        printNum(res,count);
        Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - startTime);
        System.out.println("共找到"+count+"个"+"花费时间:"+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
    }

 

 

方法二:改进的穷举法

将除数的最大值调为被除数的值的一半。当n的范围比较大的时候就可以明显发现时间节约了,我们现在来输出每个函数处理的时间

n=10000时

public static void primeSelect2(int num){
        Long startTime = System.currentTimeMillis();
        int[] res=new int[num+1];
        int count=0;
        res[0]=2;
        res[1]=3;
        res[2]=5;
        count=3;
        for(int i=7;i<=num;i++){
            int flag=0;
            for(int j=2;j<i/2;j++){
                if(i%j==0){
                    flag=1;
                }
            }
            if(flag==0){
                res[count++]=i;
            }
        }
        printNum(res,count);
        Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - startTime);
        System.out.println("花费时间:"+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
    }

 

方法三:线性素数筛

tmp遍历从2到n之间的所有非0数,以tmp为基点,到n之间的tmp的倍数全部置为0,剩下的筛选出来非0的就是素数

public static void primeSelect3(int n){
        Long startTime = System.currentTimeMillis();
        int count=0;
        int[] num=new int[n+1];
        for(int i=2;i<=n;i++){
            num[i]=i;
        }
        for(int i=2;i<n/2;i++){
            int tmp=num[i];
            if(tmp!=0){
                for(int j=2*tmp;j<=n;j+=tmp){
                    num[j]=0;
                }
            }
        }
        for(int i=0;i<n+1;i++){
            if(num[i]!=0){
                count++;
                System.out.println(num[i]);
            }
        }
        Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - startTime);
        System.out.println("共找到"+count+"个"+"花费时间:"+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
    }

 

posted @ 2021-01-28 23:53  Heinrich♣  阅读(72)  评论(0编辑  收藏  举报