-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfThree_warn.java
More file actions
executable file
·42 lines (36 loc) · 1.11 KB
/
PowerOfThree_warn.java
File metadata and controls
executable file
·42 lines (36 loc) · 1.11 KB
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
package code.coder.lee.easy;
/**
* Created by bcc on 16/3/23.
*/
public class PowerOfThree_warn {
private static final double epsilon = 10e-15;
public boolean isPowerOfThree(int n) {
if (n == 0)
return false;
if (n == 1) {
return true;
}
while (n != 1) {
if (n % 3 != 0)
return false;
n /= 3;
}
return true;
}
public boolean isPowerOfThree1(int n) {
if (n==0)
return false;
/**
* 由于java浮点数精度的问题..下面的代码会导致n=243时出错.
*/
// return Double.valueOf(Math.log(n)/Math.log(3)).intValue() == Math.log(n)/Math.log(3);
// return Math.floor(res) == Math.ceil(res);
double res = Math.log(n)/Math.log(3);
/*只需要判断res是否为整数*/
return Math.abs(res - Math.round(res)) < epsilon;
}
public static void main(String[] args) {
PowerOfThree_warn powerOfThreeWarn = new PowerOfThree_warn();
System.out.println(powerOfThreeWarn.isPowerOfThree1(81*4));
}
}