TheAlgorithms/JavaScript · error · Error

No natural numbers exist below 1

Error message

No natural numbers exist below 1

What it means

Thrown by `multiplesThreeAndFive(num)` (Project Euler #1) when `num < 1`. The problem sums natural numbers strictly below the input, so values below 1 yield an empty set. Note this is a plain `Error`, not a `RangeError`, and non-number inputs (undefined, NaN) bypass it because `undefined < 1` is false.

Source

Thrown at Project-Euler/Problem001.js:9

// https://projecteuler.net/problem=1
/* Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below the provided parameter value number.
*/

// This method uses the nSum function to add the nSum for 3 and 5. However, it needs to subtract the nSum for 15 once to avoid double counting.
const multiplesThreeAndFive = (num) => {
  if (num < 1) throw new Error('No natural numbers exist below 1')
  num -= 1
  let sum = 0

  // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num.
  // Here, n is denoted as frequency.
  const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1

  sum += nSum(3, Math.floor(num / 3))
  sum += nSum(5, Math.floor(num / 5))
  sum -= nSum(15, Math.floor(num / 15))
  return sum
}

export { multiplesThreeAndFive }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Validate `num >= 1` and is a finite integer upstream.
  2. Treat 0/negative as 'empty sum = 0' in your caller rather than passing it through.
  3. Coerce with `Number(num)` first so string or undefined inputs are caught.

Example fix

// before
multiplesThreeAndFive(userInput) // userInput may be 0 or undefined

// after
const n = Number(userInput)
if (!Number.isInteger(n) || n < 1) throw new RangeError('num must be a positive integer >= 1')
multiplesThreeAndFive(n)
Defensive patterns

Strategy: validation

Validate before calling

const n = Number(num)
if (!Number.isInteger(n) || n < 1) {
  throw new RangeError('num must be an integer >= 1')
}
multiplesThreeAndFive(n)

Type guard

const isPositiveInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1

Prevention

When it happens

Trigger: Call `multiplesThreeAndFive(0)`, `multiplesThreeAndFive(-5)`. The check is `num < 1`; `num = 1` is allowed and returns 0.

Common situations: Off-by-one from a UI control that allows 0, defensive code that passes a default of 0 when input is missing, or a computation that produced a negative bound.

Related errors


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