-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerofThree.js
More file actions
30 lines (28 loc) · 802 Bytes
/
powerofThree.js
File metadata and controls
30 lines (28 loc) · 802 Bytes
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
////////////////////////////////////////////////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.
// Example 1:
// Input: n = 27
// Output: true
// Example 2:
// Input: n = 0
// Output: false
// Example 3:
// Input: n = 9
// Output: true
// Example 4:
// Input: n = 45
// Output: false
/**
* @param {number} n
* @return {boolean}
*/
const isPowerOfThree = function(n) {
if(n === 3 || n === 1) return true;
if(n < 3) return false;
return isPowerOfThree(n / 3);
};
// console.log(isPowerOfThree(27));
// console.log(isPowerOfThree(0));
// console.log(isPowerOfThree(9));
// console.log(isPowerOfThree(45));