Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int sum_digits(int n) {
- int r = 0;
- while (n > 0) {
- r = r + (n%10);
- n = n/10;
- }
- return r;
- }
- int count_digits(int n) {
- int r = 0;
- int dig = 0;
- while (n > 0) {
- r = r + (n%10);
- n = n/10;
- dig++;
- }
- return dig;
- }
- bool prime(int a) {
- if (a <= 1) return false;
- for (int i = 2; i*i <= a; ++i) {
- if (a%i == 0) return false;
- }
- return true;
- }
- bool is_perfect_prime (int n) {
- if (not prime(n)) return false;
- int sd = sum_digits(n);
- int cd = count_digits(n);
- bool z = prime(sd); //chek if the sum of the digits is prime
- if (z and cd == 1) return true;
- else if (z) return is_perfect_prime(sd); //recursion: if the number of digits is not 1
- //repeat the process for the sum of digits
- else return false;
- }
- int main() {
- int n;
- while (cin >> n) {
- if (is_perfect_prime(n) == true) cout << "true" << endl;
- if (is_perfect_prime(n) == false) cout << "false" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement