-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0326. Power of Three.js
45 lines (39 loc) · 997 Bytes
/
0326. Power of Three.js
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
// Given an integer, write a function to determine if it is a power of three.
//
// Follow up:
// Could you do it without using any loop / recursion?
/**
* @param {number} n
* @return {boolean}
*/
// 1)
// Time O(log n). In our case that is O(log3 n). The number of divisions is given by that logarithm.
// Space O(1)
const isPowerOfThree1 = (n) => {
if (n === 0) return false;
while (n % 3 === 0) n /= 3;
return n === 1;
};
// 2)
const isPowerOfThree2 = (n) => {
for (let i = 0; i < n; i++) {
if (3 ** i === n) return true;
else if (3 ** i > n) return false;
}
return false;
};
// 3)
const isPowerOfThree3 = (n) => {
return /^10*$/.test(n.toString(3));
};
// 4) Mathematics
// n = 3^i
// i = log3(n)
// i = log10(n) / log10(3)
const isPowerOfThree4 = (n) => {
return (Math.log10(n) / Math.log10(3)) % 1 === 0; // using % 1 to get the decimal part
};
// 5) Similar to 4)
const isPowerOfThree = (n) => {
return Number.isInteger(Math.log10(n) / Math.log10(3));
};