We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 193763d commit 1951f32Copy full SHA for 1951f32
Maths/MobiusFunction.js
@@ -18,16 +18,19 @@
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
29
- ? 0
30
- : primeFactorsArray.length % 2 === 0
31
- ? 1
32
- : -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) {
33
+ return 0
34
+ }
35
+ return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
36
0 commit comments