mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2026-02-03 19:52:33 -05:00
Made sure all variables in the functions are local
This commit is contained in:
@@ -21,9 +21,9 @@ function getPrimes(goalNumber)
|
|||||||
--We can now start at 3 and skip all even numbers, because they cannot be prime
|
--We can now start at 3 and skip all even numbers, because they cannot be prime
|
||||||
for possiblePrime=3,goalNumber,2 do
|
for possiblePrime=3,goalNumber,2 do
|
||||||
--Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
--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
|
--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
|
while(primes[primesCnt] <= topPossibleFactor) do
|
||||||
if((possiblePrime % primes[primesCnt]) == 0) then
|
if((possiblePrime % primes[primesCnt]) == 0) then
|
||||||
foundFactor = true;
|
foundFactor = true;
|
||||||
@@ -66,12 +66,12 @@ function getNumPrimes(numberOfPrimes)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--We can now start at 3 and skip all even numbers, because they cannot be prime
|
--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
|
while((#primes < numberOfPrimes) and (possiblePrime >= 0)) do
|
||||||
--Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor
|
--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
|
--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
|
while(primes[primesCnt] <= topPossibleFactor) do
|
||||||
if((possiblePrime % primes[primesCnt]) == 0) then
|
if((possiblePrime % primes[primesCnt]) == 0) then
|
||||||
foundFactor = true;
|
foundFactor = true;
|
||||||
@@ -109,7 +109,7 @@ function getFactors(goalNumber)
|
|||||||
local factors = {}; --Holds all the factors
|
local factors = {}; --Holds all the factors
|
||||||
|
|
||||||
--You need to step through each prime and see if it is a factor of the number
|
--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
|
while((cnt <= #primes) and (goalNumber > 1)) do
|
||||||
--If the prime is a factor you need to add it to the factor list
|
--If the prime is a factor you need to add it to the factor list
|
||||||
if((goalNumber % primes[cnt]) == 0) then
|
if((goalNumber % primes[cnt]) == 0) then
|
||||||
@@ -173,7 +173,7 @@ function getDivisors(goalNumber)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function getSum(ary)
|
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
|
--Look through every element in the array and add the number to the running sum
|
||||||
for location = 1, #ary do
|
for location = 1, #ary do
|
||||||
sum = sum + ary[location];
|
sum = sum + ary[location];
|
||||||
@@ -183,7 +183,7 @@ function getSum(ary)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function getProd(ary)
|
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
|
--Look through every element in the array and add the number to the running product
|
||||||
for location = 1, #ary do
|
for location = 1, #ary do
|
||||||
prod = prod * ary[location];
|
prod = prod * ary[location];
|
||||||
@@ -197,7 +197,7 @@ function getPermutations(master, num)
|
|||||||
local num = num or 1
|
local num = num or 1
|
||||||
local perms = {};
|
local perms = {};
|
||||||
--Check if the number is out of bounds
|
--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
|
--Do nothing and return an empty list
|
||||||
perms = {};
|
perms = {};
|
||||||
--If this is the last possible recurse just return the current string
|
--If this is the last possible recurse just return the current string
|
||||||
|
|||||||
Reference in New Issue
Block a user