Home » Check Whether a Number is a Power of 4 or not in Java

Check Whether a Number is a Power of 4 or not in Java

by Online Tutorials Library

Check Whether a Number is a Power of 4 or not in Java

There are many ways to check whether a number is a power of 4 or not. In this section, we will discuss the different approaches to check whether a number is a power of 4 or not.

Examples:

Input: num = 7

Output: 7 is not the power of 4

Input: num = 12

Output: 12 is not the power of 4.

Input: num = 16

Output: 16 is the power of 4. 42 = 16

Approach: Using log base 4 with ceil and floor

In this approach, we find the value of log4 of the input number. If the value of the log4(y), where y is the input number, is an integer, then y is a number that is the power of 4; otherwise, not.

FileName: NumPowFour.java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

Approach: Using log base 4 without ceil and floor

In this approach, we find the value of log4 of the input number (say, the input number is y). Let’s assume the value is t. We take an integral part of the value and use it as an exponent of the number 4, i.e., 4(int) t. If the value is y, then y is the power of 4; otherwise, not.

FileName: NumPowFour1.java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

Approach: Divide by 4

Step 1: Take the input number and check whether the number is divisible by 4 or not. If it is not divisible, then the input number is not the power of 4.

Step 2: If it is divisible, divide the number by 4. If the number we get after division is 1, then the input number is a power of 4. If the received number is not 1, then repeat steps 1 and 2.

Let’s see the implementation of it.

FileName: NumPowFour2.java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

Approach: Set Bit

The common thing about any number that is a power of 2 is that it has only one set bit in its binary representation. Observe the following table.

Number Binary Representation
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

The numbers whose only one bit is set is 1, 2, 4, 8. Notice that all these numbers are power of 2. 20 = 1, 21 = 2, 22 = 4, and 23 = 8.

Since a number that is the power of 4 has to be the number of power of 2. Therefore, we will only look for those numbers that are the power of 2, and then we will filter out the numbers that are the power of 4.

A number that is the power of 4 has the following characteristics

  • Only one bit is set in that number as a power 4 number is always a power 2 number.
  • The number of zeros after the set bit has to be even. 2 = 10 in its binary representation, and the number of zero after the set bit is 1, which is odd. Therefore 2 is not the power of 4. 16 = 10000 in its binary representation, and the number of zeros after the set bit is 4, which is even. Therefore, 16 is the power of 4.

The following program will use the above-mentioned characteristics to find out the numbers that are the power of 4.

FileName: NumPowFour3.java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

Instead of counting zeros after the set bit, one can also do the & operation with 0xAAAAAAAA. The & operation with 0xAAAAAAAA is important as it removes the numbers whose number of zeros after the set bit is odd (2, 8, 32, etc.). Observe the following program.

FileName: NumPowFour4.java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

Approach: Perfect Square

Since 4 is a perfect square, any number that is the power of 4 has to be the perfect square. Thus, the following two conditions are sufficient to check whether a number is a power of 4 or not.

  • The number should be the power of 2
  • The number should be a perfect square.

FileName:java

Output:

17 is not the power four.   256 is the power of four.   8 is not the power four.   16 is the power of four.  

You may also like