TheAlgorithms/JavaScript · error · Error

Number must be greater than zero.

Error message

Number must be greater than zero.

What it means

The isSquareFree function checks whether a number has no repeated prime factors by computing its prime factorization. The guard requires number > 0 because prime factorization is only defined for positive integers. Important: PrimeFactors(number) is called at line 19 BEFORE the guard at line 20, so for non-positive input PrimeFactors may throw its own error first depending on its implementation.

Source

Thrown at Maths/IsSquareFree.js:20

 * Author: Akshay Dubey (https://github.com/itsAkshayDubey)
 * Square free integer: https://en.wikipedia.org/wiki/Square-free_integer
 * function to check if an integer has repeated prime factors.
 * return false if the number as repeated prime factors.
 * else true
 */

/**
 * @function isSquareFree
 * @description -> Checking if number is square free using prime factorization
 * @param {number} number
 * @returns {boolean} true if the number has unique prime factors, otherwise false
 */

import { PrimeFactors } from './PrimeFactors.js'
export const isSquareFree = (number) => {
  const primeFactorsArray = PrimeFactors(number)
  if (number <= 0) {
    throw new Error('Number must be greater than zero.')
  }
  return primeFactorsArray.length === new Set(primeFactorsArray).size
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a positive integer to isSquareFree.
  2. Guard the input with a positivity check before calling to avoid relying on internal ordering.
  3. Filter zero and negative values from data streams before invoking.

Example fix

// before
isSquareFree(x)
// after
if (!Number.isInteger(x) || x <= 0) return false
isSquareFree(x)
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(number) || number <= 0) {
  throw new RangeError('number must be a positive integer')
}
isSquareFree(number)

Type guard

const isPositiveInteger = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0

Prevention

When it happens

Trigger: Calling isSquareFree(0) or isSquareFree(-12). Any non-positive integer triggers this error (or potentially an earlier error from PrimeFactors).

Common situations: Processing sequences that include zero, negative deltas or coordinates, uninitialized variables defaulting to 0, or data streams containing sentinel zero values.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/793f2f0094e673c1. Report an issue: GitHub.