Implement the method fibonacci that returns the nth Fibonacci number.
Return the Fibonacci number at position n. The sequence starts with F(0) = 0 and F(1) = 1.
The task is designed to test careful handling of edge cases, not only the most common input.
- Return
0forn = 0. - Return
1forn = 1. - An iterative solution avoids repeated recursive work.
Example 1
Input
n (int) =
0Returns (int)
0Example 2
Input
n (int) =
1Returns (int)
1Example 3
Input
n (int) =
5Returns (int)
5Example 4
Input
n (int) =
10Returns (int)
55Keep the previous two sequence values and update them until the requested position is reached.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run