Skip to content

Commit 4fe8a67

Browse files
dev-madhurendramadhuredra
and
madhuredra
authored
added algo for checking the number is power of four or not (TheAlgorithms#1360)
* added algo for checking the number is power of four or not * Update IsPowerofFour.js * Update IsPowerofFour.js * fix code style * used proper JSDoc comment and fixed test issues * fixed test case issue --------- Co-authored-by: madhuredra <[email protected]>
1 parent 6ad5b9c commit 4fe8a67

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Bit-Manipulation/IsPowerofFour.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @author : dev-madhurendra
3+
* Checks whether the given number is a power of four or not.
4+
*
5+
* A number is considered a power of four if and only if there is a single '1' bit in its binary representation,
6+
* and that '1' bit is at the first position, followed by an even number of '0' bits.
7+
*
8+
* @param {number} n - The input number to check.
9+
* @returns {boolean} True if the number is a power of four, false otherwise.
10+
*
11+
* @example
12+
* const result = isPowerOfFour(16); // Returns true (16 is 4^2)
13+
* const result2 = isPowerOfFour(5); // Returns false (5 is not a power of four)
14+
*/
15+
const isPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1))
16+
17+
export { isPowerOfFour }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isPowerOfFour } from '../IsPowerofFour'
2+
3+
describe('IsPowerOfFour', () => {
4+
it.each([
5+
[0, false],
6+
[4, true],
7+
[16, true],
8+
[12, false],
9+
[64, true],
10+
[-64, false]
11+
])('should return the number is power of four or not', (n, expected) => {
12+
expect(isPowerOfFour(n)).toBe(expected)
13+
})
14+
})

0 commit comments

Comments
 (0)