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)+"个质数");
System.out.println("100-200之间共"+count02+"个质数");
System.out.println(count01+count02);
}
1-1改进版,利用boolean类型进行判断
@Test public void test10(){ //找区间内的所有素数 int count01 = 0; for (int i = 100; i <= 200; i++) { boolean issushu = true ; for(int j = 2;j < i ; j++){ if(i % j == 0 ){ issushu = false; break; } } if(issushu == true){ count01 ++; System.out.println(i+"是素数"); } } System.out.println(count01+"个素数"); }
@Test
public void test10(){ //找区间内的所有素数
int count01 = 0;
for (int i = 100; i <= 200; i++) {
boolean issushu = true ;
for(int j = 2;j < i ; j++){
if(i % j == 0 ){
issushu = false;
break;
}
}
if(issushu == true){
count01 ++;
System.out.println(i+"是素数");
}
}
System.out.println(count01+"个素数");
}
2.用奇数 ,2、3、5、7、9、11、13、17、19(提高效率)
@Test public void zuoye07(){ //使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数) //暴力匹配 int count02 = 0 ; for(int i = 101; i <= 200 ; i+=2){ for(int j = 2; j < i; j++){ if(i % j == 0){ System.out.println(i+"不是质数"); break; } if(j == i-1 ) { count02++; System.out.println(i+"是质数"); } } } System.out.println("100-200之间共"+count02+"个质数"); }
@Test
public void zuoye07(){
//使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数)
//暴力匹配
int count02 = 0 ;
for(int i = 101; i <= 200 ; i+=2){
for(int j = 2; j < i; j++){
if(i % j == 0){
System.out.println(i+"不是质数");
break;
}
if(j == i-1 )
{
count02++;
System.out.println(i+"是质数");
}
}
}
System.out.println("100-200之间共"+count02+"个质数");
}
3.奇数双管齐下
@Test public void zuoye08(){ //使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数) //奇数双管齐下 int count02 = 0 ; for(int i = 101; i <= 200 ; i+=2){ for(int j = 3; j < i; j+=2){ if(i % j == 0){ System.out.println(i+"不是质数"); break; } if((j == i-1 )||(j == i-2)) //这一步很关键 { count02++; System.out.println(i+"是质数"); } } } System.out.println("100-200之间共"+count02+"个质数"); }
@Test
public void zuoye08(){
//使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数)
//奇数双管齐下
int count02 = 0 ;
for(int i = 101; i <= 200 ; i+=2){
for(int j = 3; j < i; j+=2){
if(i % j == 0){
System.out.println(i+"不是质数");
break;
}
if((j == i-1 )||(j == i-2))
{
count02++;
System.out.println(i+"是质数");
}
}
}
System.out.println("100-200之间共"+count02+"个质数");
}
·4.使用循环打印任意区间所有的素数
@Test public void zuoye09(){ int count01 = 0 ; char char01; do{ System.out.println("请输入开始和结束值:"); Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int finish = scanner.nextInt(); while(start > finish){ System.out.println("输入的不对,请重新输入:"); start = scanner.nextInt(); finish = scanner.nextInt(); } for(int i = start+1; i <= finish ; i+=2){ for(int j = 3; j < i; j+=2){ if(i % j == 0){ System.out.println(i+"不是质数"); break; } if((j == i-1 )||(j == i-2)) { count01++; System.out.println(i+"是质数"); } } } System.out.println(start+"-"+finish+"间共"+count01+"个质数"); System.out.println("还需要继续输入吗?y/else"); String str01 = scanner.next(); char01 = str01.charAt(0); }while(char01 == 'y' || char01 =='Y'); }
@Test
public void zuoye09(){
int count01 = 0 ;
char char01;
do{
System.out.println("请输入开始和结束值:");
Scanner scanner = new Scanner(System.in);
int start = scanner.nextInt();
int finish = scanner.nextInt();
while(start > finish){
System.out.println("输入的不对,请重新输入:");
start = scanner.nextInt();
finish = scanner.nextInt();
}
for(int i = start+1; i <= finish ; i+=2){
for(int j = 3; j < i; j+=2){
if(i % j == 0){
System.out.println(i+"不是质数");
break;
}
if((j == i-1 )||(j == i-2))
{
count01++;
System.out.println(i+"是质数");
}
}
}
System.out.println(start+"-"+finish+"间共"+count01+"个质数");
System.out.println("还需要继续输入吗?y/else");
String str01 = scanner.next();
char01 = str01.charAt(0);
}while(char01 == 'y' || char01 =='Y');
}