Javatpoint Logo
Javatpoint Logo

Powerful Number in Java

In this section, we will learn what is a powerful number and also create Java programs to check if the given number is a powerful number or not. The powerful number program is frequently asked in Java coding interviews and academics.

Powerful Number

A number X is said to be the powerful number if all the prime numbers that are divisors of X and the square of those prime numbers must leave remainder 0 when they divide the number X.

In other words, the square of all those factors of X that are prime numbers must be the factor of the number X. Also, X must be of the form l2m3, where l and m are positive numbers. Mathematically,

If X = l2m3, l > 0, m > 0, and X % P = 0, and X % (P * P) = 0, where P is a prime number, and the given condition must hold for every value of P that is the factor of X, then we can say that X is a powerful number.

Steps to Find the Powerful Numbers

Step 1: Assign a number to the variable.

Step 2: Find the prime factors of the given number.

Step 3: Store these factors in a list.

Step 4: Iterate over these factors present in the list and find the square of these factors.

Step 5: Check whether the square of every factor present in the list divides the given number or not.

If the square of any one factor does not divide the given number, then the given number is not a powerful number; otherwise, a powerful number.

Examples of Powerful Numbers

Given, X = 10

Factors of X: 2, 5 (prime factors)

Square the prime factors, we get 2 * 2 = 4, and 5 * 5 = 25.

Now, we divide 10 with 4 and 25, respectively.

10 % 4 = 2, and 10 % 25 = 10. Thus, we see remainder is not zero. Hence, the number 10 is not a powerful number.

Given, X = 36

Factors of X: 2, 3 (prime factors)

Square of prime factors, we get 2 * 2 = 4, and 3 * 3 = 9.

Divide 36 by 4 and 9, respectively, we get:

36 % 4 = 0, and 36 % 9 = 0. Thus, we see remainder is zero in every case. Also, 36 can be written as 2232, where 2 > 0 and 3 > 0. Hence, the number 36 is a powerful number.

Powerful Number in Java

The above diagram shows 1, 4, 8, and 9 are powerful numbers.

Note: A prime number can never become a powerful number, as the prime factor of the number is the number itself, and the square of the number can never divide the number itself.

For example, 5 is a prime number. Hence, its prime factor is also 5. Also, 5 * 5 = 25. Now, 5 % 25 = 5, which is not 0. Hence, 5 is not a powerful number.

Powerful Number Java Program

Observe the following Java program that checks the powerful numbers from 1 to 20.

FileName: PowerfulNumberExample.java

Output:

The number 1 is the powerful number.
The number 2 is not the powerful number.
The number 3 is not the powerful number.
The number 4 is the powerful number.
The number 5 is not the powerful number.
The number 6 is not the powerful number.
The number 7 is not the powerful number.
The number 8 is the powerful number.
The number 9 is the powerful number.
The number 10 is not the powerful number.
The number 11 is not the powerful number.
The number 12 is not the powerful number.
The number 13 is not the powerful number.
The number 14 is not the powerful number.
The number 15 is not the powerful number.
The number 16 is the powerful number.
The number 17 is not the powerful number.
The number 18 is not the powerful number.
The number 19 is not the powerful number.
The number 20 is the powerful number.

Explanation: For every number from 1 to 20, the method isPowerfulNo() is invoked with the help of for-loop. For every number, a vector primeFactors is created for storing its prime divisors. Then, we check whether the square of every number present in the vector primeFactors divides the number or not. If all square of all the numbers present in the vector primeFactors divides the number completely, the number is a powerful number; otherwise, not.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA