grok-4-0709· xAI
LMArena Human Preference 140K
python fib function one line amortized constant runtime linear memory
- User
python fib function one line amortized constant runtime linear memory
- Assistantpython
fib = lambda n, memo={}: memo[n] if n in memo else memo.setdefault(n, fib(n-1, memo) + fib(n-2, memo)) if n > 1 else nExplanation:
- This is a one-line Python lambda function for computing the nth Fibonacci number (where fib(0)=0, fib(1)=1).
- Amortized constant runtime: It uses memoization (via a shared default dictionary). The first call to
fib(n)takes O(n) time to build the cache. Subsequent calls for values up to n are O(1). If you call for increasing n (e.g., fib(5), then fib(6)), each new computation adds just O(1) time amortized, as it builds on the existing cache.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.