## FANDOM

608 Pages

```local p={}

local function digitsum(str) --- For a simple check on divisibility of 3; works for multiple digits
local total = 0
for digit in string.gmatch(str, "%d") do
total = total + digit
end
if total == 0 then return "Invalid number input."
end
end

local function checkprime(n, text)
--Pre-eliminary checks done below in Main--
--Large check--
local i = 7
local t = 0 -- timer for advancing numbers in specific orders
--Start of division trials
while i < math.sqrt(n) do
if (n % i == 0) then break end
t = t + 1
if (t % 8 == 2 or t % 8 == 4 or t % 8 == 7) then i = i + 2
-- for numbers 30n + 11(to 13); 30n + 17 (to 19); 30n + 29 (to 1)
elseif (t % 8 == 1 or t % 8 == 3 or t % 8 == 5) then i = i + 4
-- for numbers 30n + 7 (to 11); 30n + 13 (to 17); 30n + 19 (to 23)
elseif (t % 8 == 0 or t % 8 == 6) then i = i + 6
-- for numbers 30n + 1 (to 7); 30n
end
--End of division trials
end
if (i <= math.sqrt(n)) then
return "Divisible by "..i
else return text or "Prime!"
end
end

--Main--

function p.prime(frame)
-- Pre-eliminary checks --
if (frame.args[1] == "1") then return "Not prime"
elseif (frame.args[1] == "2"
or frame.args[1] == "3"
or frame.args[1] == "5"
or frame.args[1] == "7"
or frame.args[1] == "11") then
return frame.args[3] or "Prime!"
elseif (frame.args[1]:sub(-1) == "2"
or frame.args[1]:sub(-1) == "4"
or frame.args[1]:sub(-1) == "6"
or frame.args[1]:sub(-1) == "8"
or frame.args[1]:sub(-1) == "0") then
return "Divisible by 2"
elseif (frame.args[1]:sub(-1) == "5") then
return "Divisible by 5"
elseif (digitsum(frame.args[1]) % 3 == 0) then
return "Divisible by 3"
elseif (frame.args[1]:len() > 15) then
return "Number is too big to calculate (Over 15 digits)"
else local aa = frame.args[1]:gsub("%p","")
if (string.match(aa, "%D")) then
return "Enter a plain number, please."
end
local a = tonumber(aa)
local b = frame.args[3]
return checkprime(a, b)
end
end

function p.prevprime(frame)
local aa = frame.args[1]:gsub("%p","")
if (string.match(aa, "%D")) then
return "Enter a plain number, please."
end
local a = tonumber (aa)
local b = frame.args[3]
if (a == 7) then return "5"
elseif (a == 3) then return "2"
elseif (a <= 2) then return "No more previous primes."
else local timer = 0
local r = tonumber(frame.args[2]) or 30 -- Defaults to 30 - 1 checks.
while timer <= r do
if (a % 10 == 7) then a = a - 4
elseif (a % 2 == 0) then a = a - 1
else a = a - 2
end
if (checkprime(a, "prime") == "prime") then break
end
timer = timer + 1
end
if (timer == r) then return "The previous prime number might be very far away; please extend allowed search."
else return a
end
end
end

function p.nextprime(frame)
local aa = frame.args[1]:gsub("%p","")
if (string.match(aa, "%D")) then
return "Enter a plain number, please."
elseif (frame.args[1]:len() > 15) then
return "Number is too big to calculate (Over 15 digits)"
end
local a = tonumber (aa)
local b = frame.args[3]
if (a == 2) then return "3"
elseif (a == 3) then return "5"
elseif (a <= 1) then return "2"
else local timer = 0
local r = tonumber(frame.args[2]) or 30 -- Defaults to 30 checks.
while timer <= r do
if (a % 10 == 3) then a = a + 4
elseif (a % 2 == 0) then a = a + 1
else a = a + 2
end
if (checkprime(a, "prime") == "prime") then break
end
timer = timer + 1
end
if (timer == r) then return "Error. The next prime number might be very far away; please extend allowed search."
else return a
end
end
end

return p

--[[ Using a 210 numbers wheel sieve (unused)
while i < math.sqrt(n) do
if (n % i == 0) then break end
t = t + 1
if (t % 48 == 0 or t % 48 == 2 or t % 48 == 4 or t % 48 == 7
or t % 48 == 10 or t % 48 == 14 or t % 48 == 17 or t % 48 == 23
or t % 48 == 25 or t % 48 == 31 or t % 48 == 34 or t % 48 == 38
or t % 48 == 41 or t % 48 == 44 or t % 48 == 46) then i = i + 2
elseif (t % 48 == 3 or t % 48 == 5 or t % 48 == 9 or t % 48 == 11
or t % 48 == 16 or t % 48 == 19 or t % 48 == 22 or t % 48 == 24
or t % 48 == 26 or t % 48 == 29 or t % 48 == 32 or t % 48 == 37
or t % 48 == 39 or t % 48 == 43 or t % 48 == 45) then i = i + 4
elseif (t % 48 == 6 or t % 48 == 8 or t % 48 == 12 or t % 48 == 13
or t % 48 == 15 or t % 48 == 18 or t % 48 == 20 or t % 48 == 28
or t % 48 == 30 or t % 48 == 33 or t % 48 == 35 or t % 48 == 36
or t % 48 == 40 or t % 48 == 42) then i = i + 6
elseif (t % 48 == 21 or t % 48 == 27) then i = i + 8
elseif (t % 48 == 1 or t % 48 == 47) then i = i + 10
end]]```