diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 606755b..ec05b4a 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -5,8 +5,19 @@ # .... # e.g. 6th fibonacci number is 8 -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) - linear, because the number of loops is determined by the value of the input integer, "n." +# Space complexity: Constant, because the algorithm only ever tracks 2 values, regardless of +# the input integer. def fibonacci(n) - raise NotImplementedError + if n == nil || n < 0 + raise ArgumentError, "The fibonacci number does not exist for #{n}" + else + fib = 0 + x = 1 + n.times do + fib += x + x = (fib - x) + end + return fib + end end