您的位置:首页 > 其它

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

2012-05-30 18:29 417 查看
题目:判断101-200之间有多少个素数,并输出所有素数。

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

public class lianxi02 {

public static void main(String[] args) {

int count = 0;

for(int i=101; i<200; i+=2) {

boolean b = false;

for(int j=2; j<=Math.sqrt(i); j++)

{

if(i % j == 0) { b = false; break; }

else { b = true; }

}

if(b == true) {count++;System.out.println(i );}

}

System.out.println( "素数个数是: " + count);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐