用JAVA做,输出101到200中所有质数

来源:百度知道 编辑:UC知道 时间:2024/05/13 15:01:47
质数就是只能被1和被它自身整除的数

public class Num {
public static void main(String args[]){

for (int i = 100; i <=200; i++) {//101-200
int count = 2;
for (int j =2; j < i; j++) {
if(i%j==0){
break;//从2-i的数 只要有一个满足表达式==0跳出下面不打印
}else{
count++;
}
if(count==(i-3)){//当所有循环过后从没进入if表达式说明是质数
System.out.println(i);
}
}
}
}

}

34

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub

//输出101到200中所有质数
for(int i = 101;i<=200;i++){
//记录能够整除几次
int count = 0;
//这里循环次数,循环到这个数的平方根就可以了,减少运算次数
for(int j = 2;j<=((int)Math.sqrt(i));j++){
if(i%j==0){
count++;
}
}
if(count == 0){
System.out.println(i);
}
}
}
}

你试试对么