Power of Three
Decide whether a whole number is 1, 3, 9, 27 or another power of three.
Given an integer ntruenfalse
Example 1
Output: true
Explanation: 27 = 3 × 3 × 3.
Example 2
Output: false
Example 3
Output: false
Explanation: 45 = 3 × 3 × 5.
The Idea
Start with 1 and keep tripling: 3, 9, 27, 81, 243, and so on. The numbers on that list are the powers of three. The starting 1 belongs to it as 30, before any tripling. The number 27 is on the list, while 45 falls between 27 and 81 and is not.
A list works for small numbers, but how do you tell for a number like 1,162,261,467 without writing out every power up to it? Go backwards. If 27 was made by tripling 1 three times, then dividing 27 by 3 three times must lead back to 1.
The picture shows both numbers being taken apart. Under each box is its remainder after dividing by 3, written with Java's %27 % 3 = 0
The row labelled Example 3 starts the same way: 45 and 15 both divide evenly. Then 5 % 3 = 2, so no more threes come out, and 5 is left over. That 5 is the factor in 45 = 3 × 3 × 5 that is not a 3.
So a positive number is a power of three exactly when taking out every factor of 3 leaves 1. Anything else left over, like the 5, is a factor that no amount of tripling can produce.
Solution Approach
Three choices turn the dividing into code: how to take out the threes, what to do with 0 and negative numbers, and why the code divides n
Taking out the threes
When should the dividing stop? A loop divides nn % 3nn
Zero and negative numbers
What happens with Example 2, where n0 % 30 / 3falsen
Dividing instead of multiplying
Why not build the powers instead, multiplying by 3 from 1 until the product reaches or passes nnInteger.MAX_VALUEnint
Java does not stop there. It wraps the product around to -808,182,895, which is still below nnint
Why logarithms do not work
Could a formula answer at once? If nnMath.log(n) / Math.log(3)Math.logdouble
Optional: a check without a loop
Is there an exact shortcut? The number 1,162,261,467 is 319, the largest power of three below Integer.MAX_VALUE
That gives a one-line test: n > 0 && 1162261467 % n == 0n > 01162261467 % -27
Java Solution
The method isPowerOfThree
- First, check whether
is less than 1. If it is, returnn at once, which covers 0 and every negative number.false - Then a
loop runs as long aswhile is 0, and each pass takes one factor of 3 out withn % 3 .n /= 3 - When the loop stops,
has no factor of 3 left. Returnn , which isn == 1 for a power of three andtrue when another factor, like the 5 in 45, remains.false
The class also holds isPowerOfThreeNoLoop
The mainInteger.MAX_VALUEInteger.MIN_VALUE
public class PowerOfThree {
public static boolean isPowerOfThree(int n) {
if (n < 1) {
return false; // 0 and negatives are never powers of three
}
while (n % 3 == 0) {
n /= 3; // take out one factor of 3
}
return n == 1; // anything but 1 is a factor other than 3
}
// 3^19 = 1162261467 is the largest power of 3 that fits in an int.
public static boolean isPowerOfThreeNoLoop(int n) {
return n > 0 && 1162261467 % n == 0;
}
public static void main(String[] args) {
int[] tests = {27, 0, 45, 1, 243, -27, 1162261467, Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int n : tests) {
System.out.println("n = " + n + ": " + isPowerOfThree(n)
+ " (no loop: " + isPowerOfThreeNoLoop(n) + ")");
}
}
}
Running it prints:
n = 27: true (no loop: true)
n = 0: false (no loop: false)
n = 45: false (no loop: false)
n = 1: true (no loop: true)
n = 243: true (no loop: true)
n = -27: false (no loop: false)
n = 1162261467: true (no loop: true)
n = 2147483647: false (no loop: false)
n = -2147483648: false (no loop: false)
The lines for 27, 0 and 45 match the three examples, and both methods agree on every line. Look at the line n = 1: truen = 243: true
On the line n = -27: false (no loop: false)Integer.MAX_VALUEInteger.MIN_VALUE
Complexity
The cost depends on how many threes the loop can take out of n
- Time: O(log n), because every pass divides
by 3, so after about log3 n passes no 3 can remain; for ann that is at most the 19 passes 319 needs.int - Space: O(1), because the method only changes its own copy of
and keeps nothing else.n
The no-loop test does one comparison and one remainder with a fixed number, so it takes O(1) time and space. Powers of four can also be checked with bit operations: see