-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
31 lines (29 loc) · 863 Bytes
/
res.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
/**
* Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
*
* Example:
*
* Given num = 16, return true. Given num = 5, return false.
*
* Follow up: Could you solve it without loops/recursion?
*
* Hint: We know that if number is power of two, it satisfies that n & (n – 1) equals zero, that is: n & (n – 1) removes the least significant bit, leaving the number (that is power of two) zero. We also know that mathematically, 4^n - 1 can be divided by 3.
*
* @authors Joe Jiang (hijiangtao@gmail.com)
* @date 2017-02-18 20:13:10
* @version $Id$
*/
/**
* @param {number} num
* @return {boolean}
*/
let isPowerOfFour = function(num) {
let compNum = num-1;
if (num<1 || num & compNum) {
return false;
}
if (compNum%3 !== 0) {
return false;
}
return true;
};