Blame view

P22467_en/S003-AC.cc 675 Bytes
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cmath>

using namespace std;

int sum_digits(int n)
{
	if(!(n/10))
        {
                return n;
        }
	return n % 10 + sum_digits(n/10);
}

bool is_perfect_prime(int n)
{
	if(n < 10)
	{
		if( n == 0 || n == 1 || n == 4 || n == 6 || n == 8 || n == 9)
		{
			return false;
		}
	        return true;
	}
	if(!(n%2) || !(n%3) || !(n%5))
	{
		return false;
	}
	for(int i = 2; i < (sqrt(n) + 1); i++)
	{
		if(!(n % i))
		{
			return false;
		}
	}
        return is_perfect_prime(sum_digits(n));
}

int main()
{
	while(1)
	{
		int a;
		cin >> a;
		if(cin.eof())
		{
			return 0;
		}
		cout << a << " : " << is_perfect_prime(a) << endl;
	}

}