Affluent Savvy
Photo by Binyamin Mellish Pexels Logo Photo: Binyamin Mellish

Which is the 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.

Can I apply cinnamon directly to skin?
Can I apply cinnamon directly to skin?

Yes, it's true. This humble kitchen ingredient can do a lot, much more than you can ever imagine. Cinnamon has anti-fungal, antioxidant and...

Read More »
What color attracts positivity?
What color attracts positivity?

Pink brings strong positive energy into your environment. Mood-lifting yellow is another colour that belongs to the fire element. Sep 17, 2014

Read More »

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

Approach:

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++

#include using namespace std; bool isPowerful( int n) { while (n % 2 == 0) { int power = 0; while (n % 2 == 0) { n /= 2; power++; } if (power == 1) return false ; } for ( int factor = 3; factor <= sqrt (n); factor += 2) { int power = 0; while (n % factor == 0) { n = n / factor; power++; } if (power == 1) return false ; } return (n == 1); } int main() { isPowerful(20) ? cout << "YES

" : cout << "NO

" ; isPowerful(27) ? cout << "YES

" : cout << "NO

" ; return 0; } Java

class GFG { static boolean isPowerful( int n) { while (n % 2 == 0 ) { int power = 0 ; while (n % 2 == 0 ) { n /= 2 ; power++; } if (power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math.sqrt(n); factor += 2 ) { int power = 0 ; while (n % factor == 0 ) { n = n / factor; power++; } if (power == 1 ) return false ; } return (n == 1 ); } public static void main(String[] args) { if (isPowerful( 20 )) System.out.print( "YES

" ); else System.out.print( "NO

What color makes you spend money?
What color makes you spend money?

Red is more effective for impulse purchases, so it's a common buy button choice on ecommerce websites. But it's more effective for B2B software...

Read More »
How much savings should I have at 40?
How much savings should I have at 40?

You may be starting to think about your retirement goals more seriously. By age 40, you should have saved a little over $175,000 if you're earning...

Read More »
Awaken your dormant DNA ability to attract wealth effortlessly
Awaken your dormant DNA ability to attract wealth effortlessly

The simple yet scientifically proven Wealth DNA method laid out in the report allows you to effortlessly start attracting the wealth and abundance you deserve.

Learn More »

" ); if (isPowerful( 27 )) System.out.print( "YES

" ); else System.out.print( "NO

" ); } } Python3

import math def isPowerful(n): while (n % 2 = = 0 ): power = 0 while (n % 2 = = 0 ): n = n / / 2 power = power + 1 if (power = = 1 ): return False for factor in range ( 3 , int (math.sqrt(n)) + 1 , 2 ): power = 0 while (n % factor = = 0 ): n = n / / factor power = power + 1 if (power = = 1 ): return false return (n = = 1 ) print ( "YES" if isPowerful( 20 ) else "NO" ) print ( "YES" if isPowerful( 27 ) else "NO" ) C# using System; class GFG { static bool isPowerful( int n) { while (n % 2 == 0) { int power = 0; while (n % 2 == 0) { n /= 2; power++; } if (power == 1) return false ; } for ( int factor = 3; factor <= Math.Sqrt(n); factor += 2) { int power = 0; while (n % factor == 0) { n = n / factor; power++; } if (power == 1) return false ; } return (n == 1); } public static void Main() { if (isPowerful(20)) Console.WriteLine( "YES" ); else Console.WriteLine( "NO" ); if (isPowerful(27)) Console.WriteLine( "YES" ); else Console.WriteLine( "NO" ); } } PHP " : "NO

What is a Chinese birthday?
What is a Chinese birthday?

The Chinese aren't big on having birthday celebrations every year; instead, they focus on the 1st, 10th, 60th, and 70th. For a child's first...

Read More »
What is a person with a lot of money called?
What is a person with a lot of money called?

multimillionaire. noun. someone who has money or property worth many millions of pounds, dollars etc.

Read More »
Awaken your dormant DNA ability to attract wealth effortlessly
Awaken your dormant DNA ability to attract wealth effortlessly

The simple yet scientifically proven Wealth DNA method laid out in the report allows you to effortlessly start attracting the wealth and abundance you deserve.

Learn More »

" ; echo $d ; $d = isPowerful(27) ? "YES

" : "NO

" ; echo $d ; ?> Javascript

Output NO YES

Time complexity: O(sqrt(n))

Auxiliary space: O(1)

References:

https://en.wikipedia.org/wiki/Powerful_number

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.

Can I take cinnamon during my period?
Can I take cinnamon during my period?

In this study, the women receiving cinnamon capsules reported significantly decreased instances of nausea in relation to their menstrual cycles as...

Read More »
What does red ink on money mean?
What does red ink on money mean?

The red seal appears on United States Notes issued from from 1862 to 1971. It shows that the money is an obligation of the U.S. Treasury. Until...

Read More »
Awaken your dormant DNA ability to attract wealth effortlessly
Awaken your dormant DNA ability to attract wealth effortlessly

The simple yet scientifically proven Wealth DNA method laid out in the report allows you to effortlessly start attracting the wealth and abundance you deserve.

Learn More »
Who is the god of happiness?
Who is the god of happiness?

Mythology. Euphrosyne is a Goddess of Good Cheer, Joy and Mirth. Her name is the female version of a Greek word euphrosynos, which means "merriment".

Read More »
How do I restart my life with no money?
How do I restart my life with no money?

Contents: Examine How You Got Here. Consider Low-Cost Living Options. Start with a Strict Budget. Reach Out for Assistance. Apply for Jobs. Begin...

Read More »