# Java 1 line of code and can be extended to any radix solution

• The idea is that numbers in quaternary system that is power of 4 will be like 10, 100, 1000 and such. Similar to binary case. And this can be extended to any radix.

``````public boolean isPowerOfFour(int num) {
return Integer.toString(num, 4).matches("10*");
}``````

• Looks similar to https://leetcode.com/discuss/78708/ternary-number-solution
did you come up with this on your own?

• I wonder whether 10 and 1000 are power of Four

• num = 4 is power of 4 : Integer.toString(num, 4) = 10 => true

num = 1024 is power of 4 : Integer.toString(num, 4) = 100000 => true

num = 2048 is not power of 4: Integer.toString(num, 4) = 200000 = > false

• I love this method, but it is quite slow...

• @yfcheng Can you eleaborate how it can be extended to any radix?

• @yfcheng Can you eleaborate how it can be extended to any radix?

Indeed, this one-liner can convert an integer to a new presentation of any radix, and check if this integer is a multiple of the radix, based on Regex matching.

``return Integer.toString(num, anyRadix).matches("10*");``

• @zzhai Thank you, I got it

• The following is my solution, its not the best one though

``````if((Math.log10(num)/Math.log10(4))%1==0) return true;
else return false;
``````

But, it was executed in 4ms and yours is taking 16ms.

• @yfcheng Your ides is very fantistic, however, I think toString function is a format of loop

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.