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;
|
return primes;
|
||||||
end
|
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
|
--This is a function that returns all the factors of goalNumber
|
||||||
function getFactors(goalNumber)
|
function getFactors(goalNumber)
|
||||||
local primes = getPrimes(math.ceil(math.sqrt(goalNumber))); --Get all the primes up the largest possible divisor
|
local primes = getPrimes(math.ceil(math.sqrt(goalNumber))); --Get all the primes up the largest possible divisor
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
--lua/luaClasses/testAlgorithms.lua
|
--lua/luaClasses/testAlgorithms.lua
|
||||||
--Mathew Ellison
|
--Mathew Ellison
|
||||||
-- Created: 03-28-19
|
-- Created: 03-28-19
|
||||||
--Modified: 06-29-21
|
--Modified: 06-30-21
|
||||||
--This script is used to test the Algorithms script
|
--This script is used to test the Algorithms script
|
||||||
--[[
|
--[[
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -64,6 +64,52 @@ local function testGetNumPrimes()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function testIsPrime()
|
||||||
|
local failed = false; --Holds whether a test was failed
|
||||||
|
|
||||||
|
--Test 1
|
||||||
|
local num = 2;
|
||||||
|
local correctAnswer = true;
|
||||||
|
local answer = isPrime(num);
|
||||||
|
--Print an error message if the function returned the wrong answer
|
||||||
|
if(correctAnswer ~= answer) then
|
||||||
|
io.write("isPrime failed the first test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
--Test 2
|
||||||
|
num = 97;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = isPrime(num);
|
||||||
|
--Print an error message if the function returned the wrong answer
|
||||||
|
if(correctAnswer ~= answer) then
|
||||||
|
io.write("isPrime failed the second test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
--Test 3
|
||||||
|
num = 1000;
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = isPrime(num);
|
||||||
|
--Print an error message if the function returned the wrong answer
|
||||||
|
if(correctAnswer ~= answer) then
|
||||||
|
io.write("isPrime failed the third test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
--Test 4
|
||||||
|
num = 1;
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = isPrime(num);
|
||||||
|
--Print an error message if the function returned the wrong answer
|
||||||
|
if(correctAnswer ~= answer) then
|
||||||
|
io.write("isPrime failed the fourth test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Print a message if all of the tests passed
|
||||||
|
if(not failed) then
|
||||||
|
io.write("isPrime passed all tests\n");
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function testGetFactors()
|
local function testGetFactors()
|
||||||
local failed = false; --Holds whether a test was failed
|
local failed = false; --Holds whether a test was failed
|
||||||
|
|
||||||
@@ -458,6 +504,12 @@ testGetNumPrimes();
|
|||||||
timer:stop();
|
timer:stop();
|
||||||
io.write("It took " .. timer:getString() .. " to run this test\n");
|
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||||
|
|
||||||
|
--Test isPrime
|
||||||
|
timer:start();
|
||||||
|
testIsPrime();
|
||||||
|
timer:stop();
|
||||||
|
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||||
|
|
||||||
--Test getFactors
|
--Test getFactors
|
||||||
timer:start();
|
timer:start();
|
||||||
testGetFactors();
|
testGetFactors();
|
||||||
|
|||||||
Reference in New Issue
Block a user