{"record":{"id":"e341b45965fe2f80","repo":"TheAlgorithms/JavaScript","slug":"fibonacci-sequence-limit-can-t-be-less-than-1","errorCode":null,"errorMessage":"Fibonacci sequence limit can't be less than 1","messagePattern":"Fibonacci sequence limit can't be less than 1","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem002.js","lineNumber":9,"sourceCode":"// https://projecteuler.net/problem=2\nconst SQ5 = 5 ** 0.5 // Square root of 5\nconst PHI = (1 + SQ5) / 2 // definition of PHI\n\n// theoretically it should take O(1) constant amount of time as long\n// arithmetic calculations are considered to be in constant amount of time\nexport const EvenFibonacci = (limit) => {\n  if (limit < 1)\n    throw new Error(\"Fibonacci sequence limit can't be less than 1\")\n\n  const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))\n  const n = Math.floor(highestIndex / 3)\n  return Math.floor(\n    ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -\n      ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) /\n      SQ5\n  )\n}\n","sourceCodeStart":1,"sourceCodeEnd":19,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem002.js#L1-L19","documentation":"Thrown by `EvenFibonacci(limit)` (Project Euler #2) when `limit < 1`. The closed-form formula uses `Math.log(limit * SQ5)` which is undefined or negative for limits below 1, so the guard rejects them. Non-number inputs (undefined, NaN) bypass it because comparisons with undefined/NaN return false.","triggerScenarios":"Call `EvenFibonacci(0)`, `EvenFibonacci(-10)`. `limit = 1` passes but yields 0 because the highest even Fibonacci below 1 is none.","commonSituations":"Defaulting a config field to 0 when unset, off-by-one from inclusive/exclusive boundary confusion, or a UI spinner that allows 0.","solutions":["Validate `limit >= 1` and is a finite number upstream.","Coerce non-number inputs with `Number(limit)` before validation.","Treat `limit < 1` as 'empty sum = 0' in the caller if that fits your domain."],"exampleFix":"// before\nEvenFibonacci(config.upperBound) // could be 0\n\n// after\nconst limit = Number(config.upperBound)\nif (!Number.isFinite(limit) || limit < 1) throw new RangeError('limit must be >= 1')\nEvenFibonacci(limit)","handlingStrategy":"validation","validationCode":"const l = Number(limit)\nif (!Number.isFinite(l) || l < 1) {\n  throw new RangeError('limit must be a finite number >= 1')\n}\nEvenFibonacci(l)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Coerce with Number() so undefined/NaN do not bypass the guard.","Pick a non-zero default when the bound is missing.","Decide whether your domain treats 'below 1' as 'empty sum = 0'."],"tags":["range-validation","project-euler","math","fibonacci"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}