From a8d62c462c64c3e6d90d948cc1dd78ae084e9967 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 26 Mar 2019 02:02:06 -0400 Subject: [PATCH] Fixed bug in getFib functions that gave incorrect answers for low subscripts --- Algorithms.lua | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Algorithms.lua b/Algorithms.lua index 78becec..34cf9f2 100644 --- a/Algorithms.lua +++ b/Algorithms.lua @@ -183,14 +183,18 @@ function getFib(goalSubscript) end --Loop through the list, generating Fibonacci numbers until it finds the correct subscript - local fibLoc = 3; + local fibLoc = 2; while(fibLoc <= goalSubscript) do - fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3]; + fibNums[(fibLoc % 3) + 1] = fibNums[((fibLoc - 1) % 3) + 1] + fibNums[((fibLoc - 2) % 3) + 1]; fibLoc = fibLoc + 1; end --Return the propper number - return fibNums[(fibLoc - 1) % 3]; + local answerLocation = ((fibLoc - 1) % 3); + if(answerLocation == 0) then + answerLocation = 3; + end + return fibNums[answerLocation]; end function getLargeFib(goalSubscript) @@ -204,14 +208,18 @@ function getLargeFib(goalSubscript) end --Loop through the list, generating Fibonacci numbers until it finds the correct subscript - local fibLoc = 3; + local fibLoc = 2; while(fibLoc <= goalSubscript) do - fibNums[fibLoc % 3] = bigint.add(fibNums[(fibLoc - 1) % 3], fibNums[(fibLoc - 2) % 3]); + fibNums[(fibLoc % 3) + 1] = bigint.add(fibNums[((fibLoc - 1) % 3) + 1], fibNums[((fibLoc - 2) % 3) + 1]); fibLoc = fibLoc + 1; end --Return the propper number - return fibNums[(fibLoc - 1) % 3]; + local answerLocation = ((fibLoc - 1) % 3); + if(answerLocation == 0) then + answerLocation = 3; + end + return fibNums[answerLocation]; end function getSum(ary)