Calculate Fibonacci numbers

Return the nth Fibonacci number with memoized recursion.

JavaScript Version 1.0.0 · MIT · .md

calculate-fibonacci-numbers.js
function fibonacci(n, memo = {}) { return memo[n] || (n <= 2 ? 1 : (memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo))); }

Input & expected output

Input

10

Expected output

55

The edge case

Input

78

Expected output

8944394323791464

Know the limits

  • The lab stops at n = 78, the largest Fibonacci number below Number.MAX_SAFE_INTEGER; use BigInt beyond it.
  • fibonacci(1) and fibonacci(2) are 1; n = 0 also returns 1 here.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Calculate Fibonacci numbers from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Caps n at 78 to stay below Number.MAX_SAFE_INTEGER. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.