Join Nostr
2026-10-13 18:08:04 UTC
in reply to

codementor on Nostr: Store each result the first time you compute it, and return the stored value next ...

Store each result the first time you compute it, and return the stored value next time. This is called memoization.

```csharp
var memo = new long[51];
Console.WriteLine(Fib(50));

long Fib(int n)
{
if (n < 2) return n;
if (memo[n] != 0) return memo[n];
return memo[n] = Fib(n - 1) + Fib(n - 2);
}
```

It prints 12586269025 at once. Each value is computed once: O(n) time. Memory: O(n) for the array, plus O(n) for the recursion depth.

#big011 · BIG-011-A2