{"record":{"id":"5ce6a1285c5db11e","repo":"TheAlgorithms/JavaScript","slug":"number-must-be-greater-than-zero-5ce6a1","errorCode":null,"errorMessage":"Number must be greater than zero.","messagePattern":"Number must be greater than zero\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/MobiusFunction.js","lineNumber":26,"sourceCode":" *   μ(n) = 0 if n has a squared prime factor.\n */\n\n/**\n * @function mobiusFunction\n * @description -> This method returns μ(n) of given number n\n * returns 1 when number is less than or equals 1\n * or number has even number of prime factors\n * returns 0 when number has repeated prime factor\n * returns -1 when number has odd number of prime factors\n * @param {Integer} number\n * @returns {Integer}\n */\n\nimport { PrimeFactors } from './PrimeFactors.js'\nexport const mobiusFunction = (number) => {\n  const primeFactorsArray = PrimeFactors(number)\n  if (number <= 0) {\n    throw new Error('Number must be greater than zero.')\n  }\n  return primeFactorsArray.length !== new Set(primeFactorsArray).size\n    ? 0\n    : primeFactorsArray.length % 2 === 0\n    ? 1\n    : -1\n}\n","sourceCodeStart":8,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/MobiusFunction.js#L8-L34","documentation":"The mobiusFunction computes the Mobius function mu(n): returns 0 if n has repeated prime factors, 1 if even count of factors, -1 if odd. Requires n > 0 for valid prime factorization. Important: PrimeFactors(number) is called at line 24 BEFORE the guard at line 25, so for non-positive input PrimeFactors may throw its own error first.","triggerScenarios":"Calling mobiusFunction(0) or mobiusFunction(-1). Any non-positive integer triggers this error (or potentially an earlier error from PrimeFactors).","commonSituations":"Zero from uninitialized variables, negative inputs from subtraction, or data containing sentinel zero values.","solutions":["Pass a positive integer to mobiusFunction.","Guard n > 0 before calling to control which error surfaces.","Filter zero and negative values from data streams upstream."],"exampleFix":"// before\nmobiusFunction(n)\n// after\nif (!Number.isInteger(n) || n <= 0) throw new RangeError('n must be a positive integer')\nmobiusFunction(n)","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(number) || number <= 0) {\n  throw new RangeError('number must be a positive integer')\n}\nmobiusFunction(number)","typeGuard":"const isPositiveInteger = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0","tryCatchPattern":null,"preventionTips":["Pre-check positivity for number-theory functions before calling.","Be aware PrimeFactors runs before the internal guard, so validate early to control error surfacing.","Filter zero and negative values from data streams upstream."],"tags":["math","number-theory","precondition","prime-factorization"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}