题目:判断101-200之间有多少个素数,并输出所有素数。

时间:2019-06-23 12:02:30   收藏:0   阅读:100

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数

 

public class SingleNum{
	public static void main(String[] args) {
		int count = 0;
		for(int i=101;i<200;i++) {
			//默认是素数
			boolean flag = true;
			for(int j=2;j<=Math.sqrt(i);j++) {
				if(i%j == 0) {
					//能整除
					flag = false;
				}
			}
			if(flag) {
				count +=1;
				System.out.print(i+",");
			}
		}
		
		System.out.println("\n有"+count+"个素数");
	}
}

 

  

 

输出结果:

技术分享图片

 

原文:https://www.cnblogs.com/squirrel-xie/p/11072009.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!