首页 > 其他 > 详细

使用筛法打印质数

时间:2018-11-23 20:13:03      阅读:172      评论:0      收藏:0      [点我收藏+]
public static void main(String[] args) {
        
        //范围:2到多少
        int maxfanwei = 20;
        
        boolean[] bolist = new boolean[maxfanwei+1];
        
        for (int i = 2; i < bolist.length; i++) {
            boolean isnotsushu = bolist[i];//不是素数true,是素数false
            if(!isnotsushu){
                int xiabiao = i;
                //这个数的倍数设置为true
                while (true) {
                    xiabiao+=i;
                    if (xiabiao>=(maxfanwei+1)) {
                        break;//超过范围退出设置
                    }
                    bolist[xiabiao] = true;
                }
            }
            
        }
        //打印质数
        for (int i = 2; i < bolist.length; i++) {
            if (!bolist[i]) {
                System.out.println(i+"    ");
            }
            
        }
        
        
    }

 

使用筛法打印质数

原文:https://www.cnblogs.com/RivenLw/p/10008939.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!