A language-learning ritual
One problem,
many languages.
Finding the nth Fibonacci number is my go-to exercise when trying a new programming language. A tiny problem opens up questions about recursion, memory, numbers, and what a language makes easy.
Start with F(0) = 0 and F(1) = 1. Every later term is the sum of the two before it: F(n) = F(n − 1) + F(n − 2). Here, n is the zero-based index, so F(10) is 55.
Change it. Run it. Compare.
Start with simple recursion, then select memoization. Try n = 10, then 35. How much work can you avoid by remembering an answer? Iteration offers a third approach: keep just the two preceding values.
JavaScript runs natively. Ruby and Python download WebAssembly runtimes from jsDelivr only when you press Run; the first download can be substantial. Your source is executed locally, not sent to a compilation service. Each run starts in a fresh, isolated worker and stops after four seconds of execution. Inputs are limited to 0–500; deeply recursive edits may reach runtime stack limits.
Remember the answer.
Simple recursion asks for F(n − 1) and F(n − 2), but the first branch already needs the second. Those overlapping branches repeat the same work. Memoization stores each computed term; the next request reads it from the cache. Step through the unedited memoized algorithm below.
Simple recursion takes exponentially many calls. Memoization takes a linear number of calls and stores a linear number of integers; iteration takes a linear number of additions and retains a constant number of integers. These are operation and integer counts: the integers themselves grow with n, so big-integer arithmetic and storage are not constant-cost.
Same idea. Different idioms.
JavaScript and Ruby evaluate a default cache argument when a call omits it. Passing that same cache into recursive calls is essential. Ruby treats zero as truthy, so memo[n] ||= … also works for the cached F(0).
Python evaluates default arguments when the function is defined. A dictionary default would survive across top-level calls. This sample uses None to make each top-level call start fresh. Go has no default arguments: a closure over a map makes the shared cache explicit instead.
JavaScript uses BigInt; ordinary Number stops representing every integer above 2⁵³ − 1. Ruby, Python, C#, and Go examples use growing integers. The Swift and Rust references use fixed-width unsigned integers with explicit limits. Memoization does not prevent numeric overflow.
The C# example adapts my existing implementation, correcting its example comments and rejecting negative inputs explicitly. The other examples are new teaching implementations prepared with AI assistance, rather than recovered copies of my earlier solutions. Swift, Rust, C#, and Go are readable references here; this page does not compile or execute those languages yet.
Runtime documentation: ruby.wasm and Pyodide workers. The runner compares each returned value with an independent iterative calculation. Passing one input does not establish correctness for every input.