-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_of_three.go
54 lines (47 loc) · 909 Bytes
/
power_of_three.go
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
54
// Package power_of_three
//
// 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 == 3x.
//
// Follow up: Could you solve it without loops/recursion?
package power_of_three
func recursive(n int) bool {
// 3^0
if n == 1 {
return true
}
if n < 3 {
return false
}
//3^1
if n == 3 {
return true
}
if n%3 != 0 {
return false
}
return recursive(n / 3)
}
func iterative(n int) bool {
if n <= 0 {
return false
}
for n%3 == 0 {
n /= 3
}
//3^1
return n == 1
}
// math
//
// Found it out there.
//
// The trick here is that the max n is (2^31) - 1 = 2147483647
//
// The last (3^x) less than that is (3^19) = 1162261467
//
// If we divide this number by any (3^x), where x<=19, we won't get any reminder.
func math(n int) bool {
return n > 0 && 1_162_261_467%n == 0
}