Skip to content

Commit 1951f32

Browse files
author
Mrinal Chauhan
committed
fix: updated code
1 parent 193763d commit 1951f32

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Maths/MobiusFunction.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818
* @param {Integer} number
1919
* @returns {Integer}
2020
*/
21-
2221
import { PrimeFactors } from './PrimeFactors.js'
22+
2323
export const mobiusFunction = (number) => {
24-
const primeFactorsArray = PrimeFactors(number)
2524
if (number <= 0) {
2625
throw new Error('Number must be greater than zero.')
2726
}
28-
return primeFactorsArray.length !== new Set(primeFactorsArray).size
29-
? 0
30-
: primeFactorsArray.length % 2 === 0
31-
? 1
32-
: -1
27+
28+
const primeFactorsArray = PrimeFactors(number)
29+
const uniquePrimeFactors = new Set(primeFactorsArray)
30+
31+
// If there are duplicate factors, it means number is not square-free.
32+
if (primeFactorsArray.length !== uniquePrimeFactors.size) {
33+
return 0
34+
}
35+
return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
3336
}

0 commit comments

Comments
 (0)