{"record":{"id":"c4ac88d43788f8d9","repo":"TheAlgorithms/JavaScript","slug":"no-natural-numbers-exist-below-1","errorCode":null,"errorMessage":"No natural numbers exist below 1","messagePattern":"No natural numbers exist below 1","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem001.js","lineNumber":9,"sourceCode":"// https://projecteuler.net/problem=1\n/* Multiples of 3 and 5\nIf 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.\nFind the sum of all the multiples of 3 or 5 below the provided parameter value number.\n*/\n\n// 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.\nconst multiplesThreeAndFive = (num) => {\n  if (num < 1) throw new Error('No natural numbers exist below 1')\n  num -= 1\n  let sum = 0\n\n  // The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num.\n  // Here, n is denoted as frequency.\n  const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1\n\n  sum += nSum(3, Math.floor(num / 3))\n  sum += nSum(5, Math.floor(num / 5))\n  sum -= nSum(15, Math.floor(num / 15))\n  return sum\n}\n\nexport { multiplesThreeAndFive }\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem001.js#L1-L24","documentation":"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.","triggerScenarios":"Call `multiplesThreeAndFive(0)`, `multiplesThreeAndFive(-5)`. The check is `num < 1`; `num = 1` is allowed and returns 0.","commonSituations":"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.","solutions":["Validate `num >= 1` and is a finite integer upstream.","Treat 0/negative as 'empty sum = 0' in your caller rather than passing it through.","Coerce with `Number(num)` first so string or undefined inputs are caught."],"exampleFix":"// before\nmultiplesThreeAndFive(userInput) // userInput may be 0 or undefined\n\n// after\nconst n = Number(userInput)\nif (!Number.isInteger(n) || n < 1) throw new RangeError('num must be a positive integer >= 1')\nmultiplesThreeAndFive(n)","handlingStrategy":"validation","validationCode":"const n = Number(num)\nif (!Number.isInteger(n) || n < 1) {\n  throw new RangeError('num must be an integer >= 1')\n}\nmultiplesThreeAndFive(n)","typeGuard":"const isPositiveInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1","tryCatchPattern":null,"preventionTips":["Coerce with Number() so undefined/NaN do not bypass the guard.","Treat 0/negative as 'empty sum = 0' in your caller rather than passing through.","Validate at the boundary, not at the call site."],"tags":["range-validation","project-euler","math","natural-number"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}