Data structure (b) - seeking <= N is the largest prime number

Q: Solving the largest prime number less than or equal to the integer N

A: exhaustive enumeration method from N to √N, √N one by one with the number 2 to the divisible, if it is not divisible by a prime number.

tips: If the integer N is not a prime number, it must be smaller than it can be decomposed into the product of two integers, wherein a multiplier is less than or equal to the square root of n, greater than or equal to the other root n.

How to prove the largest prime number must be in the root n between n appear?

A: Bertrand - Chebyshev Theorem

 

// root of n to n number of each factor range can be covered by the judgment n 
#include <the iostream> 
#include <cstdio> 
#include <the cmath>
 the using  namespace STD; 

int main () {
     int n; 
    CIN >> n-;
     int D = ( int ) sqrt (n-); // D: boundary 
    for ( int I = n-; I> = D; i--) { // descending checked one by one 
        int J;
         for (J = 2 ; J <= D; J ++ ) {
             IF (% I J == 0 ) BREAK ; 
        } 
        IF(j > d) {printf("%d\n",i); break;}
    }
    return 0;
}
View Code

 

Postscript: arithmetic problem solving strategies and mathematical thinking useful links.

For example iterative method for quadratic equation roots; successive approximation thinking.

Simple principle to break down the problem to Jane Yu Fan

Guess you like

Origin www.cnblogs.com/SUMaywlx/p/11520069.html