×

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

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Second Smallest Number in an Array in Java

By sorting the arrays and returning the second element, we can use Java to discover the second-smallest number in the array. Input:  arr[] = {10, 11, 13, 15, 34, 51} Output: The...

6 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.