-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPowerOfThree.java
53 lines (49 loc) · 1.2 KB
/
PowerOfThree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package by.andd3dfx.numeric;
/**
* <pre>
* <a href="https://leetcode.com/problems/power-of-three/description/">Task description</a>
*
* Given an integer n, return true if it is a power of three. Otherwise, return false.
*
* An integer n is a power of three, if there exists an integer x such that n == 3^x.
*
* Example 1:
*
* Input: n = 27
* Output: true
* Explanation: 27 = 3^3
*
* Example 2:
*
* Input: n = 0
* Output: false
* Explanation: There is no x where 3^x = 0.
*
* Example 3:
*
* Input: n = -1
* Output: false
* Explanation: There is no x where 3^x = (-1).
* </pre>
*
* @see <a href="https://youtu.be/E1Gue5EcvK4">Video solution</a>
*/
public class PowerOfThree {
public static boolean isPowerOfThree(int n) {
while (n > 0 && n % 3 == 0) {
n /= 3;
}
return n == 1;
}
/**
* n = 3^x => log_3 (n) = x
* log_a (b) = ln(b) /ln(a) = lg(b)/lg(a)
*/
public static boolean isPowerOfThree_usingLog(int n) {
if (n <= 0) {
return false;
}
var power = Math.round(Math.log(n) / Math.log(3));
return Math.pow(3, power) == n;
}
}