From 0388a1c9a5c5ed2723beb9746f5f157ddf71f6a3 Mon Sep 17 00:00:00 2001 From: amyesh Date: Mon, 22 Apr 2019 21:29:10 -0700 Subject: [PATCH 1/3] completed fibonacci algorithm --- lib/fibonacci.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 606755b..a779b3a 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 every track 2 values, regardless +# the input integer. def fibonacci(n) - raise NotImplementedError + if n == nil || n < 0 + raise ArgumentError, "Cannot find fibonacci of an irrational number" + else + fib = 0 + x = 1 + n.times do + fib += x + x = (fib - x) + end + return fib + end end From 60ff247efbb4da5110c92f700b0fbf757ad144f3 Mon Sep 17 00:00:00 2001 From: amyesh Date: Mon, 22 Apr 2019 21:30:58 -0700 Subject: [PATCH 2/3] updated error message --- lib/fibonacci.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index a779b3a..1571028 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -10,7 +10,7 @@ # the input integer. def fibonacci(n) if n == nil || n < 0 - raise ArgumentError, "Cannot find fibonacci of an irrational number" + raise ArgumentError, "The fibonacci number does not exist for #{n}" else fib = 0 x = 1 From c2ae9124c07c8d3c5b0801da15d96d384cc4b526 Mon Sep 17 00:00:00 2001 From: amyesh Date: Wed, 24 Apr 2019 19:13:11 -0700 Subject: [PATCH 3/3] completed fibonacci algorithm --- lib/fibonacci.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 1571028..ec05b4a 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -5,8 +5,8 @@ # .... # e.g. 6th fibonacci number is 8 -# 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 every track 2 values, regardless +# 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) if n == nil || n < 0