Judgment palindromic prime

Palindromic prime: two aspects of a judge is not a palindrome,

     Another is the judgment is not a prime number.

One by one to solve.

Palindrome: As the name suggests, is symmetric about the center. We now have a simple way, that is, according to the characteristics of palindromic number is counted from the front and from behind all count the same number can be calculated.

We also need to know a number x, the number (x% 10) obtained in the final surface size of the number x. This number (x / 10) was obtained in the foregoing except the last number.

#include <the iostream>
 the using  namespace STD; 

int Huiwen ( int n-) {
     int A, B;
     int X = 0 ; 
    A = n-;
     the while (A> 0 ) 
    { 
        X =% A 10 + X * 10 ;   // here What is counted? It is to start counting from the single digits, then add up. The number behind the front as to count the number of 
        A = A / 10 ;    // here what count? Operators who except for the last few foregoing that, to let the above equation can be counted 

    } 
    IF (n-== X) 
    { 
        return  . 1 ; 
    } 
    the else
    {
        return 0;
    }


}
int sushu(int n){
    int x=n;
    for(int i=2;i<n;i++)
    {
        if(x%i==0)
            return 0;
        
    }
    return 1;

}
int main(){
   
    int a,b;
    cin>>a>>b;
    int n=0;
    for(int i=a;i<b;i++)
    {
        if(huiwen(i)&&sushu(i))
        {
            n++;
            cout<<i<<"是回文素数"<<endl;
        }
    }
    cout<<"一共有"<<n<<""<<endl;


    return 0;
}

Note also two points:

  1, where the determination of a palindrome:

    x = x * 10 + a% 10; palindrome is judged whether the end of counting to count as previously.

    a = a / 10; finally obtained just thanks to a number used to count the remaining front sawed.

  2, it is determined prime numbers: int sushu (int n) {

    int X = n-;         1. Note that there is a number needed to pick n, as in the for loop where if not contact it, n being always changing, not 
    for (int I = 2; I <n-; I ++ )  {  IF (% I X == 0 )  return 0 ; 2. here also, the following return 1; for loop is outside, why is because, if you are else {return 1;} outside if there is such a this number is not equal to 0% i will return . 1; for example where such is not equal to 0. 27% 4 27 but can not be said that primes. And 27 can get 3 * 9. To pull out all. 1 return; } return. 1 ;}

 

Guess you like

Origin www.cnblogs.com/chenjiaolong/p/12540098.html