Skip to content

Commit 6f55ed4

Browse files
algorithm: mobius function implementation (TheAlgorithms#1088)
* feat: Add mobius function implementation * test: Add tests for mobius function * fix: Code style fixes * fix: Code style fixes * fix: Store prime factors in a variable & add throw error * fix: Fix unit tests for zero and negative numbers * fix: Minor code style fixes
1 parent ad41e8c commit 6f55ed4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Maths/MobiusFunction.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
3+
* Mobius Function: https://en.wikipedia.org/wiki/M%C3%B6bius_function
4+
* For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity.
5+
* It has values in {−1, 0, 1} depending on the factorization of n into prime factors:
6+
* μ(n) = +1 if n is a square-free positive integer with an even number of prime factors.
7+
* μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
8+
* μ(n) = 0 if n has a squared prime factor.
9+
*/
10+
11+
/**
12+
* @function mobiusFunction
13+
* @description -> This method returns μ(n) of given number n
14+
* returns 1 when number is less than or equals 1
15+
* or number has even number of prime factors
16+
* returns 0 when number has repeated prime factor
17+
* returns -1 when number has odd number of prime factors
18+
* @param {Integer} number
19+
* @returns {Integer}
20+
*/
21+
22+
import { PrimeFactors } from './PrimeFactors.js'
23+
export const mobiusFunction = (number) => {
24+
const primeFactorsArray = PrimeFactors(number)
25+
if (number <= 0) {
26+
throw new Error('Number must be greater than zero.')
27+
}
28+
return primeFactorsArray.length !== new Set(primeFactorsArray).size ? 0 : primeFactorsArray.length % 2 === 0 ? 1 : -1
29+
}

Maths/test/MobiusFunction.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { mobiusFunction } from '../MobiusFunction'
2+
3+
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]
4+
5+
describe('Testing mobius function', () => {
6+
for (let i = 1; i <= 100; i++) {
7+
it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => {
8+
expect(mobiusFunction(i)).toBe(expectedValuesArray[i - 1])
9+
})
10+
}
11+
12+
it('should throw error when supplied negative numbers', () => {
13+
expect(() => { mobiusFunction(-1) }).toThrow(Error)
14+
})
15+
16+
it('should throw error when supplied zero', () => {
17+
expect(() => { mobiusFunction(0) }).toThrow(Error)
18+
})
19+
})

0 commit comments

Comments
 (0)