From 01785bf4ca6cf60b0b41bece1762cfa46733c67d Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Sat, 3 Sep 2022 12:10:02 +0530 Subject: [PATCH 1/7] feat: Add mobius function implementation --- Maths/MobiusFunction.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Maths/MobiusFunction.js diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js new file mode 100644 index 0000000000..e16e5a9698 --- /dev/null +++ b/Maths/MobiusFunction.js @@ -0,0 +1,25 @@ +/* + * Author: Akshay Dubey (https://github.com/itsAkshayDubey) + * Mobius Function: https://en.wikipedia.org/wiki/M%C3%B6bius_function + * For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity. + * It has values in {−1, 0, 1} depending on the factorization of n into prime factors: + * μ(n) = +1 if n is a square-free positive integer with an even number of prime factors. + * μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors. + * μ(n) = 0 if n has a squared prime factor. + */ + +/** + * @function mobiusFunction + * @description -> This method returns μ(n) of given number n + * returns 1 when number is less than or equals 1 + * or number has even number of prime factors + * returns 0 when number has repeated prime factor + * returns -1 when number has odd number of prime factors + * @param {Integer} number + * @returns {Integer} + */ + +import { PrimeFactors } from './PrimeFactors.js' +export const mobiusFunction = (number) => { + return number <= 0 ? "Number must be greater than zero." : PrimeFactors(number).length != new Set(PrimeFactors(number)).size ? 0 : PrimeFactors(number).length % 2 === 0 ? 1 : -1 +} \ No newline at end of file From 376edfe8542545ad949374b05c7b5920dbd87093 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Sat, 3 Sep 2022 12:10:16 +0530 Subject: [PATCH 2/7] test: Add tests for mobius function --- Maths/test/MobiusFunction.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Maths/test/MobiusFunction.test.js diff --git a/Maths/test/MobiusFunction.test.js b/Maths/test/MobiusFunction.test.js new file mode 100644 index 0000000000..996b8c2045 --- /dev/null +++ b/Maths/test/MobiusFunction.test.js @@ -0,0 +1,21 @@ +import { mobiusFunction } from '../MobiusFunction' + +const expectedValuesArray = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, 0, 1, 0, 1, 1, 1, 0, -1, 0, 0, 0] + +describe('Testing mobius function', () => { + for (let i = 1; i <= 100; i++) { + it('Testing for number = '+i+', should return '+expectedValuesArray[i], () => { + expect(mobiusFunction(i)).toBe(expectedValuesArray[i-1]) + }) + } + + it('should not support negative numbers', () => { + const actualMessage = mobiusFunction(-1) + expect(actualMessage).toBe('Number must be greater than zero.') + }) + + it('should not support zero', () => { + const actualMessage = mobiusFunction(0) + expect(actualMessage).toBe('Number must be greater than zero.') + }) +}) \ No newline at end of file From 47ded10dff4a2035950d313ad72f99a91b465fb8 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Sat, 3 Sep 2022 12:20:30 +0530 Subject: [PATCH 3/7] fix: Code style fixes --- Maths/MobiusFunction.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index e16e5a9698..a522b366f4 100644 --- a/Maths/MobiusFunction.js +++ b/Maths/MobiusFunction.js @@ -1,7 +1,7 @@ /* * Author: Akshay Dubey (https://github.com/itsAkshayDubey) * Mobius Function: https://en.wikipedia.org/wiki/M%C3%B6bius_function - * For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity. + * For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity. * It has values in {−1, 0, 1} depending on the factorization of n into prime factors: * μ(n) = +1 if n is a square-free positive integer with an even number of prime factors. * μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors. @@ -11,7 +11,7 @@ /** * @function mobiusFunction * @description -> This method returns μ(n) of given number n - * returns 1 when number is less than or equals 1 + * returns 1 when number is less than or equals 1 * or number has even number of prime factors * returns 0 when number has repeated prime factor * returns -1 when number has odd number of prime factors @@ -21,5 +21,5 @@ import { PrimeFactors } from './PrimeFactors.js' export const mobiusFunction = (number) => { - return number <= 0 ? "Number must be greater than zero." : PrimeFactors(number).length != new Set(PrimeFactors(number)).size ? 0 : PrimeFactors(number).length % 2 === 0 ? 1 : -1 -} \ No newline at end of file + return number <= 0 ? 'Number must be greater than zero.' : PrimeFactors(number).length !== new Set(PrimeFactors(number)).size ? 0 : PrimeFactors(number).length % 2 === 0 ? 1 : -1 +} From f3ec9966b5a465dd5fc62ec4a94024caa9740884 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Sat, 3 Sep 2022 12:20:52 +0530 Subject: [PATCH 4/7] fix: Code style fixes --- Maths/test/MobiusFunction.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Maths/test/MobiusFunction.test.js b/Maths/test/MobiusFunction.test.js index 996b8c2045..fb90bf6f93 100644 --- a/Maths/test/MobiusFunction.test.js +++ b/Maths/test/MobiusFunction.test.js @@ -4,8 +4,8 @@ const expectedValuesArray = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, describe('Testing mobius function', () => { for (let i = 1; i <= 100; i++) { - it('Testing for number = '+i+', should return '+expectedValuesArray[i], () => { - expect(mobiusFunction(i)).toBe(expectedValuesArray[i-1]) + it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => { + expect(mobiusFunction(i)).toBe(expectedValuesArray[i - 1]) }) } @@ -18,4 +18,4 @@ describe('Testing mobius function', () => { const actualMessage = mobiusFunction(0) expect(actualMessage).toBe('Number must be greater than zero.') }) -}) \ No newline at end of file +}) From 148ef6c43927f39273333e5b0fd133b06784b0e0 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Tue, 6 Sep 2022 13:02:48 +0530 Subject: [PATCH 5/7] fix: Store prime factors in a variable & add throw error --- Maths/MobiusFunction.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index a522b366f4..cde05792eb 100644 --- a/Maths/MobiusFunction.js +++ b/Maths/MobiusFunction.js @@ -21,5 +21,9 @@ import { PrimeFactors } from './PrimeFactors.js' export const mobiusFunction = (number) => { - return number <= 0 ? 'Number must be greater than zero.' : PrimeFactors(number).length !== new Set(PrimeFactors(number)).size ? 0 : PrimeFactors(number).length % 2 === 0 ? 1 : -1 + const primeFactorsArray = PrimeFactors(number) + if (number <= 0) { + throw new Error('Number must be greater than zero.') + } + return primeFactorsArray.length !== new Set(primeFactorsArray).size ? 0 : primeFactorsArray.length % 2 === 0 ? 1 : -1 } From 2affaa10e27153a74026ee86354231327af03a1c Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Tue, 6 Sep 2022 13:03:11 +0530 Subject: [PATCH 6/7] fix: Fix unit tests for zero and negative numbers --- Maths/test/MobiusFunction.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Maths/test/MobiusFunction.test.js b/Maths/test/MobiusFunction.test.js index fb90bf6f93..ba91ea2be2 100644 --- a/Maths/test/MobiusFunction.test.js +++ b/Maths/test/MobiusFunction.test.js @@ -9,13 +9,11 @@ describe('Testing mobius function', () => { }) } - it('should not support negative numbers', () => { - const actualMessage = mobiusFunction(-1) - expect(actualMessage).toBe('Number must be greater than zero.') + it('should throw error when supplied negative numbers', () => { + expect(() => {mobiusFunction(-1)}).toThrow(Error) }) - it('should not support zero', () => { - const actualMessage = mobiusFunction(0) - expect(actualMessage).toBe('Number must be greater than zero.') + it('should throw error when supplied zero', () => { + expect(() => {mobiusFunction(0)}).toThrow(Error) }) }) From 844bc2ab80894e695ba61464f458c3c5507a57c7 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Tue, 6 Sep 2022 13:07:46 +0530 Subject: [PATCH 7/7] fix: Minor code style fixes --- Maths/test/MobiusFunction.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/test/MobiusFunction.test.js b/Maths/test/MobiusFunction.test.js index ba91ea2be2..78b87261c4 100644 --- a/Maths/test/MobiusFunction.test.js +++ b/Maths/test/MobiusFunction.test.js @@ -10,10 +10,10 @@ describe('Testing mobius function', () => { } it('should throw error when supplied negative numbers', () => { - expect(() => {mobiusFunction(-1)}).toThrow(Error) + expect(() => { mobiusFunction(-1) }).toThrow(Error) }) it('should throw error when supplied zero', () => { - expect(() => {mobiusFunction(0)}).toThrow(Error) + expect(() => { mobiusFunction(0) }).toThrow(Error) }) })