-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added algo for checking the number is power of four or not (#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]>
- Loading branch information
1 parent
6ad5b9c
commit 4fe8a67
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @author : dev-madhurendra | ||
* Checks whether the given number is a power of four or not. | ||
* | ||
* A number is considered a power of four if and only if there is a single '1' bit in its binary representation, | ||
* and that '1' bit is at the first position, followed by an even number of '0' bits. | ||
* | ||
* @param {number} n - The input number to check. | ||
* @returns {boolean} True if the number is a power of four, false otherwise. | ||
* | ||
* @example | ||
* const result = isPowerOfFour(16); // Returns true (16 is 4^2) | ||
* const result2 = isPowerOfFour(5); // Returns false (5 is not a power of four) | ||
*/ | ||
const isPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1)) | ||
|
||
export { isPowerOfFour } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { isPowerOfFour } from '../IsPowerofFour' | ||
|
||
describe('IsPowerOfFour', () => { | ||
it.each([ | ||
[0, false], | ||
[4, true], | ||
[16, true], | ||
[12, false], | ||
[64, true], | ||
[-64, false] | ||
])('should return the number is power of four or not', (n, expected) => { | ||
expect(isPowerOfFour(n)).toBe(expected) | ||
}) | ||
}) |