TheAlgorithms/JavaScript · error · Error
Number must be greater than zero.
Error message
Number must be greater than zero.
What it means
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.
Source
Thrown at Maths/MobiusFunction.js:26
* μ(n) = 0 if n has a squared prime factor.
*/
/**
* @function mobiusFunction
* @description -> This method returns μ(n) of given number n
* returns 1 when number is less than or equals 1
* or number has even number of prime factors
* returns 0 when number has repeated prime factor
* returns -1 when number has odd number of prime factors
* @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
}
View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
mobiusFunction(n)
// after
if (!Number.isInteger(n) || n <= 0) throw new RangeError('n must be a positive integer')
mobiusFunction(n) Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isInteger(number) || number <= 0) {
throw new RangeError('number must be a positive integer')
}
mobiusFunction(number) Type guard
const isPositiveInteger = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0
Prevention
- 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.
When it happens
Trigger: Calling mobiusFunction(0) or mobiusFunction(-1). Any non-positive integer triggers this error (or potentially an earlier error from PrimeFactors).
Common situations: Zero from uninitialized variables, negative inputs from subtraction, or data containing sentinel zero values.
Related errors
- Number must be greater than zero.
- Number must be greater than zero.
- Number must be greater than zero.
- Expected a valid real number
- Index cannot be Negative
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/5ce6a1285c5db11e.
Report an issue: GitHub.