{"record":{"id":"2b65317b6b28c850","repo":"TheAlgorithms/JavaScript","slug":"number-must-be-greater-than-zero-2b6531","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/LiouvilleFunction.js","lineNumber":22,"sourceCode":" * For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity.\n * It has values in {−1, 1} depending on the factorization of n into prime factors:\n *   λ(n) = +1 if n positive integer with an even number of prime factors.\n *   λ(n) = −1 if n positive integer with an odd number of prime factors.\n */\n\n/**\n * @function liouvilleFunction\n * @description -> This method returns λ(n) of given number n\n * returns 1 when number has even number of prime factors\n * returns -1 when number has odd number of prime factors\n * @param {Integer} number\n * @returns {Integer} 1|-1\n */\n\nimport { PrimeFactors } from './PrimeFactors.js'\nexport const liouvilleFunction = (number) => {\n  if (number <= 0) {\n    throw new Error('Number must be greater than zero.')\n  }\n  return PrimeFactors(number).length % 2 === 0 ? 1 : -1\n}\n","sourceCodeStart":4,"sourceCodeEnd":26,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/LiouvilleFunction.js#L4-L26","documentation":"The liouvilleFunction computes the Liouville function lambda(n), which returns 1 if n has an even number of prime factors (counted with multiplicity) and -1 if odd. The guard requires n > 0 since prime factorization via PrimeFactors is only defined for positive integers.","triggerScenarios":"Calling liouvilleFunction(0) or liouvilleFunction(-6). Any non-positive integer triggers this error.","commonSituations":"Zero-initialized counters, negative results from subtraction passed directly, or loop variables starting at 0 that feed into the function.","solutions":["Pass a positive integer to liouvilleFunction.","Pre-validate that n > 0 before calling.","Handle zero and negative cases separately in upstream logic."],"exampleFix":"// before\nliouvilleFunction(n)\n// after\nif (n <= 0) throw new RangeError('n must be a positive integer')\nliouvilleFunction(n)","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(number) || number <= 0) {\n  throw new RangeError('number must be a positive integer')\n}\nliouvilleFunction(number)","typeGuard":"const isPositiveInteger = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0","tryCatchPattern":null,"preventionTips":["Validate positive integers before calling number-theory functions.","Guard against zero-initialized variables being passed to factorization-dependent functions.","Handle negative results from upstream subtraction before invoking."],"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"}