diff --git a/Algorithms.lua b/Algorithms.lua new file mode 100644 index 0000000..9828747 --- /dev/null +++ b/Algorithms.lua @@ -0,0 +1,92 @@ +--luaClasses/Algorithms.lua +--Matthew Ellison +-- Created: 2-4-19 +--Modified: 2-4-19 +--This is a file of algorithms that I have found it useful to keep around at all times + + +--This function returns a list with all the primes numbers <= goalNumber +function getPrimes(goalNumber) + local primes = {}; --Holds the prime numbers + local foundFactor = false; + + --If the number is 0 or negative return an empty table + if(goalNumber <= 1) then + return primes; + --Otherwise the number is at lease 2, therefore 2 should be added to the list + else + primes[#primes + 1] = 2; + end + + --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; + --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)) + while(primes[primesCnt] <= topPossibleFactor) do + if((possiblePrime % primes[primesCnt]) == 0) then + foundFactor = true; + break; + else + primesCnt = primesCnt + 1; + end + --Check if the index has gone out of range + if(primesCnt >= #primes) then + break; + end + end + + --If you didn't find a factor then the current number must be prime + if(not foundFactor) then + primes[#primes + 1] = possiblePrime; + else + foundFactor = false; + end + end + + --Sort the array just to be neat and safe + table.sort(primes); + + --Return the array + return primes; +end + +--This function gets a specific number of primes +function getNumPrimes(numberOfPrimes) + +end + +--This is a function that returns all the factors of goalNumber +function getFactors(goalNumber) + local primes = getPrimes(math.ceil(math.sqrt(goalNumber))); --Get all the primes up the largest possible divisor + 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; + 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 + factors[#factors + 1] = primes[cnt]; + goalNumber = goalNumber / primes[cnt]; + --Otherwise advance the location in the primes array you are looking at + --By not advancing if the primes is a factor you allow for multiple of the same prime number as a factor + else + cnt = cnt + 1 + end + end + + --If you didn't get any factors the number itself must be a prime number + if(#factors == 0) then + factors[#factors + 1] = goalNumber; + goalNumber = 1 + end + + --If your some reason teh goalNumber is not 1 print an error message + if(goalNumber > 1) then + print("There was an error in getFactors(). A leftover of " .. goalNumber); + end + + --Return the list of factors + return factors; +end