Four methods prime numbers less than 100 Java requirements

Prime:

  • Also known prime number, prime number is divisible only by 1 and itself.

difficulty:

  • It requires two cycles, 99 times the outer loop;
  • The inner loop, to control the divisor of 2 98 (i.e. 2 to a number in front of the dividend).
  • Why start from 2? Because all numbers can be divisible.
  • You need to define a variable flag to record a number is not a prime number
  • The need to reset the flag to true after the end of the inner loop

method one:

Import org.junit.Test; 

public  class Demo { 

    @Test 
    public  void primeNumberTest () { 

        Boolean In Flag = to true ; 

        for ( int I = 2; I <= 100; I ++ ) {
             for ( int J = 2; J <I; ++ J ) {
                 IF (J% i == 0 ) {
                 // Why do you want to define a variable flag?
                // because prime number is divisible only by 1 and itself, and if it is judged if the condition is to true
                 // if this number is not a prime number. Because the entry if the determination condition, means that the number can be a number from 2 to i-1 is divisible 
                    In Flag = to false ; 
                }
            } 
            IF (flag) { 
                of System.out.print (I + "," ); 
            } 
        // because the value for the inner loop would flag to false
         // If there is no following statement, the value of the flag into the inner loop after forever to false 
            In Flag = to true ; 
        } 
    } 
}

result:

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

 

Method Two:

Compared with a method where the improvement, the flag is placed between the outer and inner loop, and thus do not have to reset the value of the flag the end of each of the inner loop.

Import org.junit.Test; 

public  class Demo { 

    @Test 
    public  void primeNumberTest () { 

        for ( int I = 2; I <= 100; I ++ ) {
         // because the flag is defined between the inner loop and outer loop, so the value of each flag at the beginning of the outer loop is true
         // This eliminates the need to reset the flag at the end of the inner loop is a true     
            Boolean flag = true ;
             for ( int J = 2; J <I; J ++ ) {
                 IF (J% i == 0 ) {
                     // Why do you want to define a variable flag?
                    //Because the prime number is divisible only by 1 and itself, and if it is judged if the condition is to true
                     // if this number is not a prime number. Because the entry if the determination condition, means that the number can be a number divisible to 2. 1-I 
                    In Flag = to false ; 
                } 
            } 
            if (In Flag) { 
                of System.out.print (I + "," ); 
            } 
        } 
    } 
}

result

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

 

Method three:

Improvement: added after the if statement determines break, and can be as long as 2 to i-1 among a number divisible by the prime number i is not, there is no need to continue the cycle continues
Import org.junit.Test; 

public  class Demo { 

    @Test 
    public  void primeNumberTest () { 
        
        System.out.println (Start); 
        for ( int I = 2; I <= 10000; I ++ ) {
             Boolean In Flag = to true ;
             for ( int J = 2; J <I; J ++ ) {
                 IF (% I J == 0 ) { 
                    in Flag = to false ;
                     BREAK ; // this is the improvement, as long as there can be a number which is 2 to i-1 divisible, this i is not a prime number, there is no need to continue the cycle continues 
                } 
            }
            if (flag) {
                System.out.print(i+",");
            }

        }
    }
}

 

Method four:

Improvement of: j is in the range of <i, to <= Math.sqrt (i)

import org.junit.Test;

public class Demo {

    @Test
    public void primeNumberTest() {

       
        for (int i = 2; i <= 100; i++) {
            boolean flag = true;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + ",");
            }

        }
    }
}

 

Method four small improvements:

Plus a count, to count the number of prime numbers

import org.junit.Test;

public class Demo {

    @Test
    public void primeNumberTest() {

        int count=0;
        for (int i = 2; i <= 100; i++) {
            boolean flag = true;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + ",");
                count++;
            }
        }
        System.out.print("质数个数为:"+count);
    }
}

result:

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97, The number of prime numbers: 25

 

postscript:

March 2020 3 Ri 09:45 AM Tuesday, February tenth day Lunar New Year 2020 Shanghai Clear Temperature: 12 °, relatively cold.

Guess you like

Origin www.cnblogs.com/majestyking/p/12400606.html