diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 606755b..5e64efb 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -5,8 +5,29 @@ # .... # e.g. 6th fibonacci number is 8 -# Time complexity: ? -# Space complexity: ? +# Time complexity: 0(n), where n is the given number +# Space complexity: O(1) def fibonacci(n) - raise NotImplementedError + # raise NotImplementedError + + if n == 0 + return 0 + end + + if n == nil || n < 0 + raise ArgumentError + else + a = 0 + b = 1 + for i in (1...n) + temp = b + b += a + a = temp + end + # Golden Ratio + # f = ((1.618034 ** n) - ((-0.618034) ** n)) / Math.sqrt(5) + end + + return b + # return f.round(0) end