Fibonacci Sequence
Implement the fibonacci
function that takes an integer n
as input and returns the n
th Fibonacci number.
The Fibonacci sequence is a series of numbers where each number (after the first two) is the sum of the two preceding ones. The sequence starts with 0 and 1. For example, the sequence begins: 0, 1, 1, 2, 3, 5, 8, ...
If the input is a negative number, the function should handle it by panicking with a meaningful error message.
-
Given a valid input
n = 5
, the function should return5
:let result = fibonacci(5); // 5
-
Given another valid input
n = 10
, the function should return55
:let result = fibonacci(10); // 55
-
If the input is a negative number, such as
n = -1
, the function should panic with an error message:fibonacci(-1); // Should panic with "Negative input is not allowed"