# Check if the number is a Prime power number

Given an integer N, the task is to check if the number is a Prime power number. If yes, then print the number along with its power which is equal to N. Else print -1.

A prime power is a positive integer power of a single prime number.
For example: 7 = 71, 9 = 32 and 32 = 25 are prime powers, while 6 = 2 × 3, 12 = 22 × 3 and 36 = 62 = 22 × 32 are not. (The number 1 is not counted as a prime power.)

Note: If there are no such prime number, print -1.
Examples:

Input: N = 49
Output: 72
Explanation:
N can be represented as a power of prime number 7.
N = 49 = 72

Input: N = 100
Output: -1
Explanation:
N cannot be represented as a power of any prime number.

Approach: The idea is use Seive of Eratosthenes to find all the prime number. Then, Iterate over all the prime numbers and check that if any prime number divides the given number N, if yes then divide it until it becomes 1 or not divisible by that prime number. Finally, check that the number is equal to 1, If yes then return prime number otherwise given number cannot be expressed as a prime number raised to some power.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to check if  ` `// a number is a prime power number ` `#include ` `using` `namespace` `std; ` ` `  `// Array to store the  ` `// prime numbers ` `bool` `is_prime;  ` `vector<``int``> primes; ` ` `  `// Function to mark the prime  ` `// numbers using Seive of  ` `// Eratosthenes ` `void` `SieveOfEratosthenes(``int` `n) ` `{ ` `    ``int` `p = 2; ` `     `  `    ``for``(``int` `i = 0; i < n; i++) ` `       ``is_prime[i] = ``true``; ` `        `  `    ``while` `(p * p <= n) ` `    ``{ ` `         `  `        ``// If prime[p] is not  ` `        ``// changed, then it is a prime  ` `        ``if` `(is_prime[p] == ``true``) ` `        ``{ ` `             `  `            ``// Update all multiples of p  ` `            ``for``(``int` `i = p * p; i < n + 1; i += p) ` `            ``{ ` `               ``is_prime[i] = ``false``; ` `            ``}  ` `        ``} ` `        ``p += 1; ` `    ``} ` `    ``for``(``int` `i = 2; i < n + 1; i++) ` `    ``{ ` `       ``if` `(is_prime[i]) ` `           ``primes.push_back(i); ` `    ``} ` `} ` ` `  `// Function to check if the  ` `// number can be represented  ` `// as a power of prime ` `pair<``int``, ``int``> power_of_prime(``int` `n) ` `{ ` `    ``for``(``auto` `i : primes) ` `    ``{ ` `        `  `       ``if` `(n % i == 0) ` `       ``{ ` `           ``int` `c = 0; ` `           ``while``(n % i == 0) ` `           ``{ ` `               ``n /= i; ` `               ``c += 1;  ` `           ``} ` `           ``if``(n == 1) ` `              ``return` `{i, c}; ` `           ``else` `              ``return` `{-1, 1}; ` `       ``} ` `    ``} ` `} ` ` `  `// Driver Code          ` `int` `main() ` `{ ` `    ``int` `n = 49; ` `    ``SieveOfEratosthenes(``int``(``sqrt``(n)) + 1); ` `     `  `    ``// Function Call ` `    ``pair<``int``, ``int``> p = power_of_prime(n); ` `     `  `    ``if` `(p.first > 1) ` `        ``cout << p.first << ``" ^ "`  `             ``<< p.second << endl; ` `    ``else` `        ``cout << -1 << endl; ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Python3

 `# Python3 implementation to check  ` `# if a number is a prime power number ` ` `  `from` `math ``import` `*` ` `  `# Array to store the  ` `# prime numbers ` `is_prime ``=` `[``True` `for` `i ``in` `range``(``10``*``*``6` `+` `1``)]  ` `primes ``=``[] ` ` `  `# Function to mark the prime  ` `# numbers using Seive of  ` `# Eratosthenes ` `def` `SieveOfEratosthenes(n):  ` `    ``p ``=` `2` `    ``while` `(p ``*` `p <``=` `n): ` `        ``# If prime[p] is not  ` `        ``# changed, then it is a prime  ` `        ``if` `(is_prime[p] ``=``=` `True``):  ` `            ``# Update all multiples of p  ` `            ``for` `i ``in` `range``(p ``*` `p, n ``+` `1``, p):  ` `                ``is_prime[i] ``=` `False` `        ``p ``+``=` `1` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` `        ``if` `is_prime[i]: ` `            ``primes.append(i) ` ` `  `# Function to check if the  ` `# number can be represented  ` `# as a power of prime ` `def` `power_of_prime(n): ` `    ``for` `i ``in` `primes: ` `        ``if` `n ``%` `i ``=``=` `0``: ` `            ``c ``=` `0` `            ``while` `n ``%` `i ``=``=` `0``: ` `                ``n``/``/``=` `i ` `                ``c ``+``=` `1` `            ``if` `n ``=``=` `1``: ` `                ``return` `(i, c) ` `            ``else``: ` `                ``return` `(``-``1``, ``1``) ` ` `  `# Driver Code          ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``n ``=` `49` `    ``SieveOfEratosthenes(``int``(sqrt(n))``+``1``) ` `     `  `    ``# Function Call ` `    ``num, power ``=` `power_of_prime(n) ` `    ``if` `num > ``1``: ` `        ``print``(num, ``"^"``, power) ` `    ``else``: ` `        ``print``(``-``1``) `

Output:

```7 ^ 2
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SURENDRA_GANGWAR