#127Medium
Fibonacci and the number of calls
Time: 1 s · Memory: 256 MB
- Recursion
Statement
Fibonacci numbers: , . They are computed by this recursive function:
fib(n):
if n <= 2: return 1
return fib(n - 1) + fib(n - 2)
Find and how many times in total the function fib is called while computing fib(n) (the first call counts too).
Input format
A single integer ().
Output format
A single line with two numbers: and the number of calls.
Examples
Input
5
Output
5 9
Input
1
Output
1 1
Note
Increase a global counter by inside the function. For : fib(5) → fib(4), fib(3); fib(4) → fib(3), fib(2); each fib(3) → fib(2), fib(1). That is calls in total, and .
The same values are computed again and again, so the number of calls grows very fast with : almost million for . This is the price of naive recursion.
