1.暴力匹配,根据质数的定义判断x是否为质数,我们看它能否被2、3、4······、x-1整除,如果它不能被其中任何一个整数整除,则这个数就是质数。
@Test public void zuoye06(){ //使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数) int count01 = 0 ; int count02 = 0 ; for(int i = 101; i <= 200 ; i++){ for(int j = 2; j < i; j++){ if(i % j == 0){ count01++; System.out.println(i+"不是质数"); break; } if(j == i-1 ) { count02++; System.out.println(i+"是质数"); } } } System.out.println("100-200之间共"+(100 - count01)+"个质数"); System.out.println("100-200之间共"+count02+"个质数"); System.out.println(count01+count02); }
@Test
public void zuoye06(){
//使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数)
int count01 = 0 ;
int count02 = 0 ;
for(int i = 101; i <= 200 ; i++){
for(int j = 2; j < i; j++){
if(i % j == 0){
count01++;
System.out.println(i+"不是质数");
break;
}
if(j == i-1 )
{
count02++;
System.out.println(i+"是质数");
}
}
}
System.out.println("100-200之间共"+(100 - count01)+"个质数");
Sys