Check Whether a Number is a Power of 4 or not in JavaThere 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. 4^{2} = 16 Approach: Using log base 4 with ceil and floorIn this approach, we find the value of log_{4} of the input number. If the value of the log_{4}(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 floorIn this approach, we find the value of log_{4} 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 4Step 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 BitThe 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.
The numbers whose only one bit is set is 1, 2, 4, 8. Notice that all these numbers are power of 2. 2^{0} = 1, 2^{1} = 2, 2^{2} = 4, and 2^{3 }= 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
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 SquareSince 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.
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. |