Skip to content

Commit

Permalink
fix: updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrinal Chauhan committed Oct 25, 2024
1 parent 193763d commit 1951f32
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Maths/MobiusFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
* @param {Integer} number
* @returns {Integer}
*/

import { PrimeFactors } from './PrimeFactors.js'

export const mobiusFunction = (number) => {
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

const primeFactorsArray = PrimeFactors(number)
const uniquePrimeFactors = new Set(primeFactorsArray)

// If there are duplicate factors, it means number is not square-free.
if (primeFactorsArray.length !== uniquePrimeFactors.size) {
return 0
}
return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
}

0 comments on commit 1951f32

Please sign in to comment.