Made sure all variables in the functions are local

This commit is contained in:
2019-03-25 13:43:07 -04:00
parent 5e2d87a029
commit f0ce2d717c

View File

@@ -21,9 +21,9 @@ function getPrimes(goalNumber)
--We can now start at 3 and skip all even numbers, because they cannot be prime
for possiblePrime=3,goalNumber,2 do
--Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
primesCnt = 1;
local primesCnt = 1;
--We can safely assume that there will be at least 1 element in the primes list because of 2 being added before the loop
topPossibleFactor = math.ceil(math.sqrt(possiblePrime))
local topPossibleFactor = math.ceil(math.sqrt(possiblePrime))
while(primes[primesCnt] <= topPossibleFactor) do
if((possiblePrime % primes[primesCnt]) == 0) then
foundFactor = true;
@@ -66,12 +66,12 @@ function getNumPrimes(numberOfPrimes)
end
--We can now start at 3 and skip all even numbers, because they cannot be prime
possiblePrime = 3;
local possiblePrime = 3;
while((#primes < numberOfPrimes) and (possiblePrime >= 0)) do
--Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
primesCnt = 1;
local primesCnt = 1;
--We can safely assume that there will be at least 1 element in the primes list because of 2 being added before the loop
topPossibleFactor = math.ceil(math.sqrt(possiblePrime))
local topPossibleFactor = math.ceil(math.sqrt(possiblePrime))
while(primes[primesCnt] <= topPossibleFactor) do
if((possiblePrime % primes[primesCnt]) == 0) then
foundFactor = true;
@@ -109,7 +109,7 @@ function getFactors(goalNumber)
local factors = {}; --Holds all the factors
--You need to step through each prime and see if it is a factor of the number
cnt = 1;
local cnt = 1;
while((cnt <= #primes) and (goalNumber > 1)) do
--If the prime is a factor you need to add it to the factor list
if((goalNumber % primes[cnt]) == 0) then
@@ -173,7 +173,7 @@ function getDivisors(goalNumber)
end
function getSum(ary)
sum = 0; --Holds the sum of all elements. Start at 0 because num+1 = num
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
for location = 1, #ary do
sum = sum + ary[location];
@@ -183,7 +183,7 @@ function getSum(ary)
end
function getProd(ary)
prod = 1; --Holds the product of all elements. Start at 1 because num*1 = num
local prod = 1; --Holds the product of all elements. Start at 1 because num*1 = num
--Look through every element in the array and add the number to the running product
for location = 1, #ary do
prod = prod * ary[location];
@@ -197,7 +197,7 @@ function getPermutations(master, num)
local num = num or 1
local perms = {};
--Check if the number is out of bounds
if((num > string.len(master)) or (num < 0)) then
if((num > string.len(master)) or (num <= 0)) then
--Do nothing and return an empty list
perms = {};
--If this is the last possible recurse just return the current string