From 0e154eca313089cab4082c74d71e1a764bfd5204 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 20:38:00 +0530 Subject: [PATCH 1/6] added algo for checking the number is power of four or not --- Bit-Manipulation/IsPowerofFour.js | 24 +++++++++++++++++++++ Bit-Manipulation/test/IsPowerOfFour.test.js | 14 ++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 Bit-Manipulation/IsPowerofFour.js create mode 100644 Bit-Manipulation/test/IsPowerOfFour.test.js diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js new file mode 100644 index 0000000000..e1f61c63d3 --- /dev/null +++ b/Bit-Manipulation/IsPowerofFour.js @@ -0,0 +1,24 @@ +/* + author: @dev-madhurendra + + This script will check whether the given + number is a power of four or not. + + A number will be power of four if and only if there is a 1 in the binary + of the digit and it will be at the first position + + n n-1%3==0 (n-1)bin (n)bin + 1 0 0 1 - 4^0 + 4 3 011 100 - 4^1 + 16 15 01111 10000 - 4^2 + 64 63 011111 1000000 - 4^3 + + a) There is only one bit set in the binary representation of n (or n is a power of 2) + b) There is only one bit unset in the n-1 + c) And the and of n&n-1 will be 0 + d) N-1 will be divisble by 3 +*/ + +const IsPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1)) + +export { IsPowerOfFour } diff --git a/Bit-Manipulation/test/IsPowerOfFour.test.js b/Bit-Manipulation/test/IsPowerOfFour.test.js new file mode 100644 index 0000000000..18e16fde76 --- /dev/null +++ b/Bit-Manipulation/test/IsPowerOfFour.test.js @@ -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) + }) +}) From b7cae5c77876aec72a781c4db01ff00773ed81f7 Mon Sep 17 00:00:00 2001 From: Madhurendra Nath Tiwari <68775519+dev-madhurendra@users.noreply.github.com> Date: Tue, 12 Sep 2023 00:22:13 +0530 Subject: [PATCH 2/6] Update IsPowerofFour.js --- Bit-Manipulation/IsPowerofFour.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js index e1f61c63d3..dbb5c6f7c9 100644 --- a/Bit-Manipulation/IsPowerofFour.js +++ b/Bit-Manipulation/IsPowerofFour.js @@ -5,7 +5,7 @@ number is a power of four or not. A number will be power of four if and only if there is a 1 in the binary - of the digit and it will be at the first position + of the digit and it will be at the first position and followed by even number of zeroes are there. n n-1%3==0 (n-1)bin (n)bin 1 0 0 1 - 4^0 From 48e54daefedae2cd114e90ce82ec7eb6b9cc771a Mon Sep 17 00:00:00 2001 From: Madhurendra Nath Tiwari <68775519+dev-madhurendra@users.noreply.github.com> Date: Tue, 12 Sep 2023 00:27:10 +0530 Subject: [PATCH 3/6] Update IsPowerofFour.js --- Bit-Manipulation/IsPowerofFour.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js index dbb5c6f7c9..c22d4c438d 100644 --- a/Bit-Manipulation/IsPowerofFour.js +++ b/Bit-Manipulation/IsPowerofFour.js @@ -7,16 +7,12 @@ A number will be power of four if and only if there is a 1 in the binary of the digit and it will be at the first position and followed by even number of zeroes are there. - n n-1%3==0 (n-1)bin (n)bin + n n-1%3==0 (n-1)'sbin (n)'sbin 1 0 0 1 - 4^0 4 3 011 100 - 4^1 16 15 01111 10000 - 4^2 64 63 011111 1000000 - 4^3 - a) There is only one bit set in the binary representation of n (or n is a power of 2) - b) There is only one bit unset in the n-1 - c) And the and of n&n-1 will be 0 - d) N-1 will be divisble by 3 */ const IsPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1)) From e1fb577ca5fa67b831e0a18d2cb63f8a9b9a66f2 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Tue, 12 Sep 2023 09:38:07 +0530 Subject: [PATCH 4/6] fix code style --- Bit-Manipulation/IsPowerofFour.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js index c22d4c438d..6f430573fe 100644 --- a/Bit-Manipulation/IsPowerofFour.js +++ b/Bit-Manipulation/IsPowerofFour.js @@ -5,7 +5,7 @@ number is a power of four or not. A number will be power of four if and only if there is a 1 in the binary - of the digit and it will be at the first position and followed by even number of zeroes are there. + of the digit and it will be at the first position and followed by even number of zeroes are there. n n-1%3==0 (n-1)'sbin (n)'sbin 1 0 0 1 - 4^0 From 82b11c7c4de5b70850fcacfb9029930fa901f4f9 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Thu, 21 Sep 2023 15:16:04 +0530 Subject: [PATCH 5/6] used proper JSDoc comment and fixed test issues --- Bit-Manipulation/IsPowerofFour.js | 35 ++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/Bit-Manipulation/IsPowerofFour.js b/Bit-Manipulation/IsPowerofFour.js index 6f430573fe..38431f9242 100644 --- a/Bit-Manipulation/IsPowerofFour.js +++ b/Bit-Manipulation/IsPowerofFour.js @@ -1,20 +1,17 @@ -/* - author: @dev-madhurendra +/** + * @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)) - This script will check whether the given - number is a power of four or not. - - A number will be power of four if and only if there is a 1 in the binary - of the digit and it will be at the first position and followed by even number of zeroes are there. - - n n-1%3==0 (n-1)'sbin (n)'sbin - 1 0 0 1 - 4^0 - 4 3 011 100 - 4^1 - 16 15 01111 10000 - 4^2 - 64 63 011111 1000000 - 4^3 - -*/ - -const IsPowerOfFour = (n) => ((n > 0) && ((n & n - 1) === 0) && (n % 3 === 1)) - -export { IsPowerOfFour } +export { isPowerOfFour } From bc7f916aa113b07e4c25d1eebb10ff0580caf6dc Mon Sep 17 00:00:00 2001 From: madhuredra Date: Thu, 21 Sep 2023 15:17:09 +0530 Subject: [PATCH 6/6] fixed test case issue --- Bit-Manipulation/test/IsPowerOfFour.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bit-Manipulation/test/IsPowerOfFour.test.js b/Bit-Manipulation/test/IsPowerOfFour.test.js index 18e16fde76..5bd666fadd 100644 --- a/Bit-Manipulation/test/IsPowerOfFour.test.js +++ b/Bit-Manipulation/test/IsPowerOfFour.test.js @@ -1,4 +1,4 @@ -import { IsPowerOfFour } from '../IsPowerofFour' +import { isPowerOfFour } from '../IsPowerofFour' describe('IsPowerOfFour', () => { it.each([ @@ -9,6 +9,6 @@ describe('IsPowerOfFour', () => { [64, true], [-64, false] ])('should return the number is power of four or not', (n, expected) => { - expect(IsPowerOfFour(n)).toBe(expected) + expect(isPowerOfFour(n)).toBe(expected) }) })