From 5d25dd2963cff85a903e00490bd44617c9e740b3 Mon Sep 17 00:00:00 2001 From: Riyo Date: Tue, 23 Apr 2019 18:39:37 -0700 Subject: [PATCH] finished method, tests pass --- lib/fibonacci.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 606755b..65aa10f 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -5,8 +5,23 @@ # .... # e.g. 6th fibonacci number is 8 -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) where n is the input number +# Space complexity: O(1) the number of variables is constant regardless of size of n def fibonacci(n) - raise NotImplementedError + fibonacci_num = 0 + prior_num = 0 + current_num = 1 + + if !n || n < 0 + raise ArgumentError + elsif n == 0 || n == 1 + return n + else + (n-1).times do + fibonacci_num = prior_num + current_num + prior_num = current_num + current_num = fibonacci_num + end + return current_num + end end