Outputs of all prime numbers less than 100

1. What is a prime number?

Also known prime number only divisible by 1 and itself a natural number, that is, except 1 and itself as a divisor, nothing else about the number.

---> 2 from the beginning to the value -1 is not to be this number divisible by itself.

2. way:

Package cn.tust.cycle; 

/ * 
 * output primes less than 50000 
 * 
 * * / 
public  class PrimeNumber { 

    public  static  void main (String [] args) { 
        
        Boolean isFlag = to true ; 
        
        Long startTime = System.currentTimeMillis (); 
        
        // traverse the 50000 number 
        for ( int I = 2; I <= 50000; I ++ ) { 
            
            // primes that can only be 1 and itself divided by a natural number 
            for ( int J = 2; J <I; J ++ ) {
                 IF ( 0% J == I ) { 
                    isFlag =to false ; 
                } 
            } 
            
            IF (== isFlag to true ) { 
                System.out.println (I); 
            } 
        
            isFlag = to true ; 
        } 
        
        Long the endtime = System.currentTimeMillis (); 
        System.out.println ( "the time taken is:" + (endtime- startTime)); 

    } 

} 
the output: the time taken is: 5637ms

When in a mode when a certain divisible i j, i.e. when the first is assigned isFlag false, do not perform again the cycle j, i of the next cycle can be done directly.

2. Optimization 1:
Package cn.tust.cycle; 

/ * 
 * output primes less than 50000 
 * 
 * * / 
public  class PrimeNumber { 

    public  static  void main (String [] args) { 
        
        Boolean isFlag = to true ; 
        
        Long startTime = System.currentTimeMillis (); 
        
        // traverse the 50000 number 
        for ( int I = 2; I <= 50000; I ++ ) { 
            
            // primes that can only be 1 and itself divided by a natural number 
            for ( int J = 2; J <I; J ++ ) {
                 IF ( 0% J == I ) { 
                    isFlag =to false ;
                     BREAK ; // Optimization 1: Only non-prime number of natural numbers is valid 
                } 
            } 
            
            IF (== isFlag to true ) { 
                System.out.println (I); 
            } 
        
            isFlag = to true ; 
        } 
        
        Long the endtime = System.currentTimeMillis ( ); 
        System.out.println ( "is the time taken:" + (endtime- startTime)); 

    } 

} 
the output: the time taken is: 544ms

Optimization 2:

Package cn.tust.cycle; 

/ * 
 * output primes less than 50000 
 * 
 * * / 
public  class PrimeNumber { 

    public  static  void main (String [] args) { 
        
        Boolean isFlag = to true ; 
        
        Long startTime = System.currentTimeMillis (); 
        
        // traverse the number 50000 
        for ( int I = 2; I <= 50000; I ++ ) { 
            
            // prime number is 1 and that only a natural number divisible by itself 
            for ( int J = 2; J <the Math.sqrt (I); ++ J) { // optimization 2: natural number itself is not a prime number and a prime number is valid 
                IF (% I J == 0 ) {
                    isFlag= To false ;
                     BREAK ; // Optimization 1: Only non-prime number of natural numbers is valid 
                } 
            } 
            
            IF (== isFlag to true ) { 
                System.out.println (I); 
            } 
        
            isFlag = to true ; 
        } 
        
        Long the endtime = System.currentTimeMillis (); 
        System.out.println ( "is the time taken:" + (endtime- startTime)); 

    } 

} 
the output: the time taken is: 69 ms

Why the number of times the inner loop cycle will be modified to the square root of i?

Answer: 97, for example, when 97 removed to a certain number of times, the result is also given that the root axes 2-97, divided by 2, when 48.5, so the number would not go after 48 cycle, because after the number 48 is greater than the number of pairs is less than 1 2; in addition to that when the square root of the number of the time, this is the square root of the resulting number, when the upward addition, the results obtained are have already had a number of pairs, i.e., a divisor is just so long as the loop Math.sqrt (i) on the line.

 

Guess you like

Origin www.cnblogs.com/wsxdev/p/11606990.html