Palindromic primes (array) (C++) kkmd66

ideas

1. Judging palindrome first, think of the idea of ​​quick sorting;
2. Judging prime numbers, format output.

Description:

Find the palindromic prime numbers between m~n (m≥10, n≤10000), the so-called palindromic prime numbers, that is, this number is both a palindrome and a prime number. The program must first determine whether a number is a palindrome, and then determine whether it is a prime number. If it is a palindromic prime number, it is stored in an array and output in a 6-bit field width, 5 per line.

Input:

m n

Output:

Palindromic prime numbers between m~n, each number occupies 6 bits, and each line is limited to 5 output

Sample Input:

10 10000

Sample Output:

    11   101   131   151   181
   191   313   353   373   383
   727   757   787   797   919
   929
#include <iostream>
#include "string"
#include "cmath"
#include "iomanip"

using namespace std;

/**
 * kkmd66
 * @return
 */

int main() {
    
    

    int a, b, count = 0;
    cin >> a >> b;

    for (int i = a; i <= b; ++i) {
    
    

        //找回文
        string str = to_string(i);

        bool flag_1 = true;
        int begin = 0, end = str.size() - 1;
        while (begin <= end) {
    
    
            if (str[begin] != str[end]) {
    
    
                flag_1 = false;
                break;
            }
            begin++;
            end--;
        }

        //找质数
        if (flag_1) {
    
    

            bool flag_2 = true;
            for (int j = 2; j < sqrt(i * i); ++j) {
    
    
                if (i % j == 0) {
    
    
                    flag_2 = false;
                    break;
                }
            }

            //输出
            if (flag_2) {
    
    
                cout << fixed << setw(6) << right << setfill(' ') << i;
                count++;
                if (count == 5) {
    
    
                    cout << endl;
                    count = 0;
                }
            }
        }
    }

    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324036716&siteId=291194637