mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Added function to test if an integer is prime
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user