# Powerful Number

A number n is said to be Powerful Number if for every prime factor p of it, p2 also divides it. For example:- 36 is a powerful number. It is divisible by both 3 and square of 3 i.e, 9.

The first few Powerful Numbers are:
1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64 ….

Given a number n, our task is to check if this is powerful or not.
Examples :

```Input: 27
Output: YES

Input: 32
Output: YES

Input: 12
Output: NO
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is based on the fact that if a number n is powerful, then all prime factors of it and their squares should be divisible by n. We find all prime factors of given number. And for every prime factor, we find the highest power of it that divides n. If we find a prime factor whose highest dividing power is 1, we return false. If highest dividing power of all prime factors is more than 1, we return true.

Below is implementation of above idea.

## C++

 `// C++ program to find if a number is powerful or not. ` `#include ` `using` `namespace` `std; ` ` `  `// function to check if the number is powerful ` `bool` `isPowerful(``int` `n) ` `{ ` `    ``// First divide the number repeatedly by 2 ` `    ``while` `(n % 2 == 0) { ` `        ``int` `power = 0; ` `        ``while` `(n % 2 == 0) { ` `            ``n /= 2; ` `            ``power++; ` `        ``} ` ` `  `        ``// If only 2^1 divides n (not higher powers), ` `        ``// then return false ` `        ``if` `(power == 1) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``// if n is not a power of 2 then this loop will execute ` `    ``// repeat above process ` `    ``for` `(``int` `factor = 3; factor <= ``sqrt``(n); factor += 2) { ` `        ``// Find highest power of "factor" that divides n ` `        ``int` `power = 0; ` `        ``while` `(n % factor == 0) { ` `            ``n = n / factor; ` `            ``power++; ` `        ``} ` ` `  `        ``// If only factor^1 divides n (not higher powers), ` `        ``// then return false ` `        ``if` `(power == 1) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``// n must be 1 now if it is not a prime numenr. ` `    ``// Since prime numbers are not powerful, we return ` `    ``// false if n is not 1. ` `    ``return` `(n == 1); ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``isPowerful(20) ? cout << ``"YES\n"` `: cout << ``"NO\n"``; ` `    ``isPowerful(27) ? cout << ``"YES\n"` `: cout << ``"NO\n"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find if a ` `// number is powerful or not. ` ` `  `class` `GFG { ` `    ``// function to check if the ` `    ``// number is powerful ` `    ``static` `boolean` `isPowerful(``int` `n) ` `    ``{ ` `        ``// First divide the number ` `        ``// repeatedly by 2 ` `        ``while` `(n % ``2` `== ``0``) { ` `            ``int` `power = ``0``; ` `            ``while` `(n % ``2` `== ``0``) { ` `                ``n /= ``2``; ` `                ``power++; ` `            ``} ` ` `  `            ``// If only 2^1 divides n (not higher powers), ` `            ``// then return false ` `            ``if` `(power == ``1``) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// if n is not a power of 2 then this loop will execute ` `        ``// repeat above process ` `        ``for` `(``int` `factor = ``3``; factor <= Math.sqrt(n); factor += ``2``) { ` `            ``// Find highest power of "factor" that divides n ` `            ``int` `power = ``0``; ` `            ``while` `(n % factor == ``0``) { ` `                ``n = n / factor; ` `                ``power++; ` `            ``} ` ` `  `            ``// If only factor^1 divides n (not higher powers), ` `            ``// then return false ` `            ``if` `(power == ``1``) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// n must be 1 now if it is not a prime numenr. ` `        ``// Since prime numbers are not powerful, we return ` `        ``// false if n is not 1. ` `        ``return` `(n == ``1``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``if` `(isPowerful(``20``)) ` `            ``System.out.print(``"YES\n"``); ` `        ``else` `            ``System.out.print(``"NO\n"``); ` `        ``if` `(isPowerful(``27``)) ` `            ``System.out.print(``"YES\n"``); ` `        ``else` `            ``System.out.print(``"NO\n"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python program to find ` `# if a number is powerful or not. ` `import` `math ` ` `  `# function to check if ` `# the number is powerful ` `def` `isPowerful(n): ` ` `  `    ``# First divide the number repeatedly by 2 ` `    ``while` `(n ``%` `2` `=``=` `0``): ` ` `  `        ``power ``=` `0` `        ``while` `(n ``%` `2` `=``=` `0``): ` `         `  `            ``n ``=` `n``/``/``2`  `            ``power ``=` `power ``+` `1` `         `  `          `  `        ``# If only 2 ^ 1 divides ` `        ``# n (not higher powers), ` `        ``# then return false ` `        ``if` `(power ``=``=` `1``): ` `            ``return` `False` `     `  `  `  `    ``# if n is not a power of 2 ` `    ``# then this loop will execute ` `    ``# repeat above process ` `    ``for` `factor ``in` `range``(``3``, ``int``(math.sqrt(n))``+``1``, ``2``): ` `     `  `        ``# Find highest power of ` `        ``# "factor" that divides n ` `        ``power ``=` `0` `        ``while` `(n ``%` `factor ``=``=` `0``): ` `         `  `            ``n ``=` `n``/``/``factor ` `            ``power ``=` `power ``+` `1` `         `  `  `  `        ``# If only factor ^ 1 divides ` `        ``# n (not higher powers), ` `        ``# then return false ` `        ``if` `(power ``=``=` `1``): ` `            ``return` `false ` `     `  `  `  `     ``# n must be 1 now if it ` `     ``# is not a prime numenr. ` `     ``# Since prime numbers are ` `     ``# not powerful, we return ` `     ``# false if n is not 1. ` `    ``return` `(n ``=``=` `1``) ` ` `  `# Driver code ` ` `  `print``(``"YES"` `if` `isPowerful(``20``) ``else` `"NO"``) ` `print``(``"YES"` `if` `isPowerful(``27``) ``else` `"NO"``) ` ` `  ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `// C# program to find if a ` `// number is powerful or not. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function to check if the ` `    ``// number is powerful ` `    ``static` `bool` `isPowerful(``int` `n) ` `    ``{ ` `        ``// First divide the number ` `        ``// repeatedly by 2 ` `        ``while` `(n % 2 == 0) { ` `            ``int` `power = 0; ` `            ``while` `(n % 2 == 0) { ` `                ``n /= 2; ` `                ``power++; ` `            ``} ` ` `  `            ``// If only 2^1 divides n ` `            ``// (not higher powers), ` `            ``// then return false ` `            ``if` `(power == 1) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// if n is not a power of 2 then ` `        ``// this loop will execute repeat ` `        ``// above process ` `        ``for` `(``int` `factor = 3; factor <= Math.Sqrt(n); factor += 2) { ` `            ``// Find highest power of "factor" ` `            ``// that divides n ` `            ``int` `power = 0; ` `            ``while` `(n % factor == 0) { ` `                ``n = n / factor; ` `                ``power++; ` `            ``} ` ` `  `            ``// If only factor^1 divides n ` `            ``// (not higher powers), then ` `            ``// return false ` `            ``if` `(power == 1) ` `                ``return` `false``; ` `        ``} ` ` `  `        ``// n must be 1 now if it is not ` `        ``// a prime numenr. ` `        ``// Since prime numbers are not ` `        ``// powerful, we return false if ` `        ``// n is not 1. ` `        ``return` `(n == 1); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``if` `(isPowerful(20)) ` `            ``Console.WriteLine(``"YES"``); ` `        ``else` `            ``Console.WriteLine(``"NO"``); ` ` `  `        ``if` `(isPowerful(27)) ` `            ``Console.WriteLine(``"YES"``); ` `        ``else` `            ``Console.WriteLine(``"NO"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```NO
YES
```

This article is contributed by Harsh Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.