Added getFib functions

This commit is contained in:
2019-03-26 00:58:54 -04:00
parent f0ce2d717c
commit 382e441a29

View File

@@ -172,6 +172,48 @@ function getDivisors(goalNumber)
return divisors;
end
--This function returns the numth Fibonacci number
function getFib(goalSubscript)
--Setup the variables
local fibNums = {1, 1, 0}; --A list to keep track of the Fibonacci Numbers. It need only be 3 long because we only need the one we are working on and the last 2
--If the number is <= 0 return 0
if(goalSubscript <= 0) then
return 0;
end
--Loop through the list, generating Fibonacci numbers until it finds the correct subscript
local fibLoc = 3;
while(fibLoc <= goalSubscript) do
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
fibLoc = fibLoc + 1;
end
--Return the propper number
return fibNums[(fibLoc - 1) % 3];
end
function getLargeFib(goalSubscript)
local bigint = require "bigint";
--Setup the variables
local fibNums = {bigint.new(1), bigint.new(1), bigint.new(0)};
--If the number <= 0 return 0
if(goalSubscript <= 0) then
return bigint.new(0);
end
--Loop through the list, generating Fibonacci numbers until it finds the correct subscript
local fibLoc = 3;
while(fibLoc <= goalSubscript) do
fibNums[fibLoc % 3] = bigint.add(fibNums[(fibLoc - 1) % 3], fibNums[(fibLoc - 2) % 3]);
fibLoc = fibLoc + 1;
end
--Return the propper number
return fibNums[(fibLoc - 1) % 3];
end
function getSum(ary)
local sum = 0; --Holds the sum of all elements. Start at 0 because num+1 = num
--Look through every element in the array and add the number to the running sum