From f0ce2d717cc31e78025b745827760ac7dc408816 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 25 Mar 2019 13:43:07 -0400 Subject: [PATCH] Made sure all variables in the functions are local --- Algorithms.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Algorithms.lua b/Algorithms.lua index f35b7ab..ab7cd80 100644 --- a/Algorithms.lua +++ b/Algorithms.lua @@ -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