×

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic exams usually involve questions about the powerful number program.

Powerful Number

A number X is referred to as the powerful number if all of its prime divisors and the square of those prime numbers must leave the number X with a zero remainder when divided.

In other words, the factor of the number X must equal the square of all the prime factors of the number X. The form of X must also be l2m3, where l and m are positive integers.

If the following conditions hold for every value of P that is a factor of X: X = l2m3, l > 0, m > 0, X% P = 0, and X% (P * P) = 0, and P is a prime number, then we can claim that X is a powerful number.

If p2 divides a given number n for every prime number p, that number is said to be powerful. For instance, 36 is a powerful number. It is divided by 3 and the square of 3 (i.e., 9).

The initial group of powerful numbers includes:

1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64 . . . . .

Our objective is to determine whether a given number n is powerful or not.

How to Find the powerful Numbers

Step 1: Assign a number to the variable.

Step 2: Determine the prime factors of the given variable.

Step 3. Store factors in a List.

Step 4: Find the square of the factors in the list as you iterate over them.

Step 5: Determine whether or not each factor in the list has a square that divides the supplied number.

The given number is not a powerful number if the square of any one factor does not divide it.

A number is a powerful number if the prime factor and the square of the prime factor divide the provided number.

Powerful Number Examples

Example 1:

Let us take,  X = 15

Factors of X: 3, 5 (prime factors)

Square the prime factors, we get 3 * 3 = 9, and 5 * 5 = 25.

Now, we divide 15 by 9 and 25, respectively.

15 % 9 = 6, and 15 % 25 = 15. Thus, we see remainder is not zero. Hence, the number 15 is not a powerful number.

Example 2:

Let us take, X = 27

Factors of X:  3 (prime factors)

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

Divide 27 by 9 we get:

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

Example 3:

Let us take, X = 100

Factors of X:  2, 5 (prime factors)

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

Now, we divide 100 by 4 and 25, respectively.

100 % 4 = 0, and 100 % 25 = 0. Thus, we see remainder is zero. Hence, the number 100 is a powerful number.

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.

Example 4:

Let us take X=5. 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.

Example 5:

Let us take X=7. 7 is a prime number. Hence, its prime factor is also 7. Also, 7 * 7 = 49. Now, 7 % 49 = 7, which is not 0. Hence, 7 is not a powerful number.

Program to find Powerful Number

File name: Pow.java

// Java program to find if a number is powerful or not.
 import java.io.* ;
 import java.util.* ;
class Pow {
    // function to 
//determine whether a number is powerful or not
    static boolean isPowerful(int p)
    {
        // First, divide the number.
 //by two repeatedly.
        
        while (p % 2 == 0) {
            int power = 0 ;
            while (p % 2 == 0) {
                p /= 2;
                power++ ;
            }


            // Return false if only 2^1 divides n (no higher powers).


            if (power == 1)
                return false ;
        }
 
        // This loop will run if n is not a power of 2.
        // re-do the previous steps.
        for (int factor = 3; factor <= Math.sqrt(p); factor += 2) {
            // Determine the greatest power of the "factor" that divides n.
            int power = 0;
            while (p % factor == 0) {
                p = p / factor;
                power++;
            }
 
            // Return false if only factor^1 divides n (not higher powers).


            if (power == 1)
                return false;
        }
 
        // Now, if n is not a prime number, it must equal 1.
        // We return false if n is not 1 because prime numbers are not powerful numbers.
        return (p == 1);
    }
 
    // code logic
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if (isPowerful(n))
            System.out.print("YES\n");
        else
            System.out.print("NO\n");
      
    }
}

Output 1:

Powerful Number in Java

In the above-displayed output, 20 is the given number, and as 20 is not divided by its prime factor's square, 20 is not a powerful number.

Therefore, the program prints “NO”.

Output 2:

Powerful Number in Java

In the above-displayed output, 27 is the given number, and as 27 is divided by its prime factor's square, 27 is a powerful number. The prime factor of 27 is 3, the square of 3 is 9, and 9 divides 27.

Therefore, the program prints “YES”.

Output 3:

Powerful Number in Java

In the above-displayed output, 5 is the given number, and 5 is not divided by its prime factor's square, so 5 is not a powerful number. The prime factor of 5 is 5 itself, the square of 5 is 25, and 25 doesn’t divide 5.

Therefore, the program prints “NO”.

Program to display powerful and not powerful numbers

File name: Pow1.java

// Java program to display powerful numbers and not powerful numbers.
 import java.io.* ;
 import java.util.* ;
 
class Pow1 {
        // function to determine whether a number is powerful or not
    static boolean isPowerful(int p)
    {
         // First, divide the number
        // by two repeatedly.
        while (p % 2 == 0) {
            int power = 0 ;
            while (p % 2 == 0) {
                p /= 2 ;
                power++ ;
            }
 
        // Return false if only 2^1 divides n (no higher powers)


            if (power == 1)
                return false ;
        }
 
        // This loop will run if n is not a power of 2.
        // re-do the previous steps.
        for (int factor = 3; factor <= Math.sqrt(p); factor += 2) {
            // Determine the greatest power of the "factor" that divides n.
            int power = 0 ;
            while (p % factor == 0) {
                p = p / factor ;
                power++ ;
            }
 
         // Return false if only factor^1 divides n (not higher powers)


            if (power == 1)
                return false ;
        }
 


        // Now, if n is not a prime number, it must equal 1.
        // We return false if n is not 1 because prime numbers are not powerful
          // numbers.
        return (p == 1) ;
    }
 
    // code logic
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in) ;
        int n=sc.nextInt() ;
        for(int i=1;i<=n;i++){
        if (isPowerful(i))
            System.out.print(i+" is a powerful number\n")  ;
        else
            System.out.print(i+" is not a powerful number\n")  ;
      
    }
    }
}

Output 1:

Powerful Number in Java

In the above-displayed output, a range of powerful numbers is displayed. Here, the input is 10, and the program prints the powerful numbers and not powerful numbers from 1 to 10.

The powerful numbers from 1 to 10 are 1, 4, 8, and 9.

Output 2:

Powerful Number in Java

In the above-displayed output, a range of powerful numbers is displayed. Here, the input is 20, and the program prints the powerful numbers and not powerful numbers from 1 to 20.

The powerful numbers from 1 to 10 are 1, 4, 8, 9, and 16.

Program to find powerful numbers in a given range

File name: Pow2.java

// Java program to find only the powerful numbers.
 import java.io.* ;
 import java.util.* ;
 
class Pow2 {
// function to 
//determine whether a number is powerful or not
    static boolean isPowerful(int p)
    {
         // First, divide the number
            //by two repeatedly.
        while (p % 2 == 0) {
            int power = 0;
            while (p % 2 == 0) {
                p /= 2;
                power++ ;
            }
 
  // Return false if only 2^1 divides n (no higher powers)


            if (power == 1)
                return false ;
        }
 
         // This loop will run if n is not a power of 2.
        // re-do the previous steps.
        for (int factor = 3; factor <= Math.sqrt(p); factor += 2) {
            // Determine the greatest power of the "factor" that divides n.
            int power = 0;
            while (p % factor == 0) {
                p = p / factor;
                power++ ;
            }
 
// Return false if only factor^1 divides n (not higher powers)


 if (power == 1)
                return false ;
        }
 
        // Now, if n is not a prime number, it must equal 1.
        // We return false if n is not 1 because prime numbers are not powerful
        // numbers.        
     
        return (p == 1) ;
        }
 
    // code logic
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in) ;
        int n=sc.nextInt();
        System.out.print("Powerful numbers : ") ;
        for(int i=1;i<=n;i++){
        if (isPowerful(i))
            System.out.print(i+" ") ;
      
    }
    }

Output:

Powerful Number in Java

The above-displayed output displays a range of powerful numbers from 1 to 27.


Related Topics

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.