`
universsky
  • 浏览: 92192 次
文章分类
社区版块
存档分类
最新评论

Java编程算法源代码全解-----素数质数判断 东海陈光剑

 
阅读更多

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。
* @author cWX166129
*/
public class findPrimeNumber {
private static int N=200;

public static void main(String[] args){
int count=0;

for(int i=2;i<N;i++){
boolean b=false;
for(int j=2;j<=Math.sqrt(i);j++)
{
if(i % j==0) { // 逐个相除,一直到sqrt(i)不能整除的就是质数
b=false;
break;
}
else {
b=true;
}
}
if (b==true){
count++;
System.out.println(i);
}

}
System.out.println("The 5-N prime number is "+ count);
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics