Added function to test if an integer is prime

This commit is contained in:
2021-06-30 17:29:30 -04:00
parent 75f4441f18
commit e5f9a26dc6
2 changed files with 68 additions and 1 deletions

View File

@@ -119,6 +119,21 @@ function getNumPrimes(numberOfPrimes)
return primes;
end
--This function determines whether the number passed into it is a prime
function isPrime(possiblePrime)
if(possiblePrime <= 3) then
return possiblePrime > 1;
elseif(((possiblePrime % 2) == 0) or ((possiblePrime % 3) == 0)) then
return false;
end
for cnt = 5, math.sqrt(possiblePrime), 6 do
if(((possiblePrime % cnt) == 0) or ((possiblePrime % (cnt + 2)) == 0)) then
return false;
end
end
return true;
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