# Simple Java Solution Without Recursion/Iteration

• If `N` is a power of `3`:

• It follows that `3^X == N`
• It follows that `log (3^X) == log N`
• It follows that `X log 3 == log N`
• It follows that `X == (log N) / (log 3)`
• For the basis to hold, `X` must be an integer.

However, due to the fact that log(3) cannot be precisely represented on a binary computer; X is considered an integer if its decimal component falls within a guard range of `+/-0.00000000000001`. Static imports for `log`, `abs` and `rint` from `java.lang.Math`.

``````public boolean isPowerOfThree(int n) {
double a = log(n) / log(3);
return abs(a - rint(a)) <= 0.00000000000001;
}``````

• How did you come up with `0.00000000000001`?

• ``````isPowerOfThree(Math.pow(3, 99))
``````

This returns false with your solution.

• Since the error is set to be a constant `0.00000000000001`, there must be some huge number that fails his method.

• @GWTW Check out this solution, it works well with Math.pow(3, 99)
https://leetcode.com/discuss/79400/o-1-python-solution-with-explanation

• @lexrus, I am not familiar with python, so I don't have a clear idea of the reason. However, since the error is also fixed in that code, there must be a huge number that fails that one.

• @GWTW Yes. :( It just failed with math.pow(3, 999)

• @lexrus, thanks for letting me know :)

• @lexrus the biggest PowerOfThreeNumber that smaller than a int number is 3^19, 3^20 is bigger than int

• Please take note of the method signature. The method accepts an `int` and this solution works fine for that constraint (i.e for all `n <= Integer.MAX_VALUE`).

• This post is deleted!

• Can anyone enlighten me on why such a solution would be broken by a particularly large input (rather than a small) one?
The trend seems to be that `log3(pow(3,99)) - 99 < log3(pow(3,9)) - 9`, but why?

• @lexrus 99 is not the power of 3,so the answer is false

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