Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
In this article we will look into ways of speeding up simple computations and why that is useful in practice.
\nOne simple way of finding out if a number n
is a power of a number b
is to keep dividing n
by b
as long as the remainder is 0. This is because we can write
\n\n
\nHence it should be possible to divide n
by b
x
times, every time with a remainder of 0 and the end result to be 1.
Notice that we need a guard to check that n != 0
, otherwise the while loop will never finish. For negative numbers, the algorithm does not make sense, so we will include this guard as well.
Complexity Analysis
\nTime complexity : . In our case that is . The number of divisions is given by that logarithm.
\nSpace complexity : . We are not using any additional memory.
\nIn Base 10, all powers of 10 start with the digit 1 and then are followed only by 0 (e.g. 10, 100, 1000). This is true for other bases and their respective powers. For instance in base 2, the representations of , and are , and respectively. Therefore if we convert our number to base 3 and the representation is of the form 100...0, then the number is a power of 3.
\nProof
\nGiven the base 3 representation of a number as the array s
, with the least significant digit on index 0, the formula for converting from base 3 to base 10 is:
\n\n
\nTherefore, having just one digit of 1 and everything else 0 means the number is a power of 3.
\nImplementation
\nAll we need to do is convert [4] the number to base 3 and check if it is written as a leading 1 followed by all 0.
\nA couple of built-in Java functions will help us along the way.
\n\nThe code above converts number
into base base
and returns the result as a String
. For example, Integer.toString(5, 2) == "101"
and Integer.toString(5, 3) == "12"
.
The code above checks if a certain Regular Expression[2] pattern exists inside a string. For instance the above will return true if the substring "123" exists inside the string myString
.
We will use the regular expression above for checking if the string starts with 1 ^1
, is followed by zero or more 0s 0*
and contains nothing else $
.
Complexity Analysis
\nTime complexity : .
\nAssumptions:
\nInteger.toString()
- Base conversion is generally implemented as a repeated division. The complexity of should be similar to our approach #1: .String.matches()
- Method iterates over the entire string. The number of digits in the base 3 representation of n
is .Space complexity : .
\nWe are using two additional variables,
\nWe can use mathematics as follows
\n\n\n
\nn
is a power of three if and only if i
is an integer. In Java, we check if a number is an integer by taking the decimal part (using % 1
) and checking if it is 0.
Common pitfalls
\nThis solution is problematic because we start using double
s, which means we are subject to precision errors. This means, we should never use ==
when comparing double
s. That is because the result of Math.log10(n) / Math.log10(3)
could be 5.0000001
or 4.9999999
. This effect can be observed by using the function Math.log()
instead of Math.log10()
.
In order to fix that, we need to compare the result against an epsilon
.
Complexity Analysis
\nTime complexity : The expensive operation here is Math.log
, which upper bounds the time complexity of our algorithm. The implementation is dependent on the language we are using and the compiler[3]
Space complexity : . We are not using any additional memory. The epsilon
variable can be inlined.
An important piece of information can be deduced from the function signature
\n\nIn particular, n
is of type int
. In Java, this means it is a 4 byte, signed integer [ref]. The maximum value of this data type is 2147483647. Three ways of calculating this value are
System.out.println(Integer.MAX_VALUE);
Knowing the limitation of n
, we can now deduce that the maximum value of n
that is also a power of three is 1162261467. We calculate this as:
\n\n
\nTherefore, the possible values of n
where we should return true
are , ... . Since 3 is a prime number, the only divisors of are , ... , therefore all we need to do is divide by n
. A remainder of 0 means n
is a divisor of and therefore a power of three.
Complexity Analysis
\nTime complexity : . We are only doing one operation.
\nSpace complexity : . We are not using any additional memory.
\nSingle runs of the function make it is hard to accurately measure the difference of the two solutions. On LeetCode, on the Accepted Solutions Runtime Distribution page, all solutions being between 15 ms
and 20 ms
. For completeness, we have proposed the following benchmark to see how the two solutions differ.
Java Benchmark Code\n
\nIn the table below, the values are in seconds.
\nIterations | \n\n\n | \n\n\n | \n\n\n | \n\n\n | \n\n\n | \n
---|---|---|---|---|---|
Java Approach #1 (Naive) | \n0.04 | \n0.07 | \n0.30 | \n2.47 | \n5.26 | \n
Java Approach #2 (Strings) | \n0.68 | \n4.02 | \n38.90 | \n409.16 | \n893.89 | \n
Java Approach #3 (Logarithms) | \n0.09 | \n0.50 | \n4.59 | \n45.53 | \n97.50 | \n
Java Approach #4 (Fast) | \n0.04 | \n0.06 | \n0.08 | \n0.41 | \n0.78 | \n
As we can see, for small values of N, the difference is not noticeable, but as we do more iterations and the values of n
passed to isPowerOfThree()
grow, we see significant boosts in performance for Approach #4.
Simple optimizations like this might seem negligible, but historically, when computation power was an issue, it allowed certain computer programs (such as Quake 3[1]) possible.
\nAnalysis written by: @aicioara
\n