← Back to Recursion
Question 605
Naive Fibonacci
Naive Fibonacci
Write fib(n: int) -> int that returns the nth Fibonacci number by plain recursion: fib(0) is 0, fib(1) is 1, and every later number is the sum of the two numbers before it, so the sequence runs 0, 1, 1, 2, 3, 5, 8. Write a base case for n below 2 and one recursive case that calls the function twice. This naive version repeats the same work many times over, so it gets slow quickly; the tests keep n at 20 or below. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one integer n, no prompt, which the tests read and pass to your function before printing the value it returns.
Expected Output:
55= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Recursion
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.