mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Created a Sieve of Eratosthenes
This commit is contained in:
77
SieveOfEratosthenes.lua
Normal file
77
SieveOfEratosthenes.lua
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
--luaClasses/SieveOfEratosthenes.lua
|
||||||
|
--Matthew Ellison
|
||||||
|
-- Created: 06-30-21
|
||||||
|
--Modified: 06-30-21
|
||||||
|
--This creates a class that uses the Sieve of Eratosthenes to generate an infinite number of primes
|
||||||
|
--[[
|
||||||
|
Copyright (C) 2021 Matthew Ellison
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
]]
|
||||||
|
|
||||||
|
|
||||||
|
--Create the table
|
||||||
|
SieveOfEratosthenes = {};
|
||||||
|
SieveOfEratosthenes.__index = SieveOfEratosthenes;
|
||||||
|
|
||||||
|
--This is needed to create a Sieve and link the variables and functions
|
||||||
|
--It should be called before anything when creating a Sieve
|
||||||
|
function SieveOfEratosthenes:create()
|
||||||
|
local sieve = {
|
||||||
|
possiblePrime = 2;
|
||||||
|
compositeMap = {};
|
||||||
|
};
|
||||||
|
setmetatable(sieve, SieveOfEratosthenes); --This links the new variable with the functions from the SieveOfEratosthenes
|
||||||
|
return sieve;
|
||||||
|
end
|
||||||
|
|
||||||
|
--This function returns the next prime number
|
||||||
|
function SieveOfEratosthenes:next()
|
||||||
|
local prime = 0;
|
||||||
|
if(self.possiblePrime > 2) then
|
||||||
|
--Loop until you find a prime number
|
||||||
|
while(self.compositeMap[self.possiblePrime] ~= nil) do
|
||||||
|
--Create the next entry for all entries in the map
|
||||||
|
for cnt = 1, #self.compositeMap[self.possiblePrime] do
|
||||||
|
local num = self.compositeMap[self.possiblePrime][cnt];
|
||||||
|
if(self.compositeMap[self.possiblePrime + num + num] == nil) then
|
||||||
|
local tempArray = {num};
|
||||||
|
self.compositeMap[self.possiblePrime + num + num] = tempArray;
|
||||||
|
else
|
||||||
|
table.insert(self.compositeMap[self.possiblePrime + num + num], num);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--Delete the current entry
|
||||||
|
self.compositeMap[self.possiblePrime] = nil;
|
||||||
|
--table.remove(self.compositeMap, self.possiblePrime);
|
||||||
|
self.possiblePrime = self.possiblePrime + 2;
|
||||||
|
end
|
||||||
|
--Save that the number is prime
|
||||||
|
prime = self.possiblePrime;
|
||||||
|
--Add the next entry to the prime
|
||||||
|
if(self.compositeMap[prime * 3] == nil) then
|
||||||
|
local tempArray = {prime};
|
||||||
|
self.compositeMap[prime * 3] = tempArray;
|
||||||
|
else
|
||||||
|
table.insert(self.compositeMap[prime * 3], prime);
|
||||||
|
end
|
||||||
|
--Move on to the next possible prime
|
||||||
|
self.possiblePrime = self.possiblePrime + 2;
|
||||||
|
else
|
||||||
|
--Return 2 and move to 3
|
||||||
|
prime = self.possiblePrime;
|
||||||
|
self.possiblePrime = self.possiblePrime + 1;
|
||||||
|
end
|
||||||
|
return prime;
|
||||||
|
end
|
||||||
49
testSieveOfEratosthenes.lua
Normal file
49
testSieveOfEratosthenes.lua
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
--luaClasses/testSieveOfEratosthenes.lua
|
||||||
|
--Matthew Ellison
|
||||||
|
-- Created: 06-30-21
|
||||||
|
--Modified: 06-30-21
|
||||||
|
--This tests the SieveOfEratosthenes class
|
||||||
|
--[[
|
||||||
|
Copyright (C) 2021 Matthew Ellison
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
]]
|
||||||
|
|
||||||
|
require "SieveOfEratosthenes"
|
||||||
|
require "Algorithms"
|
||||||
|
|
||||||
|
|
||||||
|
local function testSieveOfEratosthenes()
|
||||||
|
local failed = false; --Signals whether a test was failed
|
||||||
|
|
||||||
|
--Test 1
|
||||||
|
local sieve = SieveOfEratosthenes:create();
|
||||||
|
local correctAnswer = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
|
||||||
|
local answer = {};
|
||||||
|
for _ = 0, 25 do
|
||||||
|
table.insert(answer, sieve:next());
|
||||||
|
end
|
||||||
|
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
|
||||||
|
io.write("SieveOfEratosthenes failed the test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Print a message if all of the tests passed
|
||||||
|
if(not failed) then
|
||||||
|
io.write("SieveOfEratosthenes passed all tests\n");
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test SieveOfEratosthenes
|
||||||
|
testSieveOfEratosthenes();
|
||||||
Reference in New Issue
Block a user