## FANDOM

608 Pages

```p={}

-----------------------------------------------------------------------------
--                          DIGIT SUM METHODS                              --
--  This is done by adding up all the digits, or alternating digits in a   --
--  certain way, used on divisibilities of 3, 9, and 11. It is also used   --
--  on numbers like 7, 13 that uses blocks of 3 to add up alternating sum. --
--  For numbers like 27, it adds the numbers in blocks of 3.               --
-----------------------------------------------------------------------------

function p.digitsum(frame) --- For calculating divisibility of 3 and 9
local total = 0
for digit in string.gmatch(frame.args[1], "%d") do
total = total + digit
end
end

function p.altsum(frame) -- For calculating divisibility of 11
local total = 0
local i = 0
for digit in string.gmatch(string.reverse(frame.args[1]), "%d") do
i = i + 1
if (i % 2 == 0) then
if (frame.args[2] == "odd") then else
total = total + digit
end
elseif (frame.args[2] == "even") then else
total = total - digit
end
end
if (frame.args[2] == "odd" or "even") then
total = math.abs(total)
end
end

function p.digitsum3(frame) -- For calculating divisibility of 27, and various others
local total = 0
local i = 0
for digit in string.gmatch(string.reverse(frame.args[1]), "%d") do
i = i + 1
if (i % 3 == 1) then
total = total + digit
elseif (i % 3 == 2) then
total = total + 10*digit
elseif (i % 3 == 0) then
total = total + 100*digit
end
end
end

function p.altsum3(frame) -- For calculating divisibility of 7, 13, and various others
local total = 0
local i = 0
for digit in string.gmatch(string.reverse(frame.args[1]), "%d") do
i = i + 1
if (i % 6 == 1) then
total = total + digit
elseif (i % 6 == 2) then
total = total + 10*digit
elseif (i % 6 == 3) then
total = total + 100*digit
elseif (i % 6 == 4) then
total = total - digit
elseif (i % 6 == 5) then
total = total - 10*digit
elseif (i % 6 == 0) then
total = total - 100*digit
end
end
end

-----------------------------------------------------------------------------
--                          TRIM METHODS                                   --
--  Trim methods, i.e. subtracting the last digit "n" from the rest of the --
--  digits, and multiply it by "m", a certain number. Repeat the process by--
--  "x" times.                                                             --
--                                                                         --
--  Parameters: 1 = The number to perform; 2 = number to be multiplied     --
--  3 = Repeat how many times. 4: If 4 = ld, the value returned is "n".    --
--  If 4 = ldm, the value returned is n * m (the value "ldmdabs" can be    --
--  used for returning absolute value) If 4 = m, returns "m" (Parameter 2) --
--  If 4 = flnum, return the value that has been striped of last digit.    --
-----------------------------------------------------------------------------

function p.trimsum(frame) -- Main trim methods. Right trim 1
local number = 0 -- The number to be converted to pure number
local a = tonumber(frame.args[3]) --The number of times to be performed
local n                           -- The last digit of the number
local m = 0                       -- Multiply by m times.
local timer = 1                   -- Timer check of how many times performed.
local result                      -- For establishing a variable
for digit in string.gmatch(frame.args[1], "%d") do
number = 10*number + digit
end
while timer <= a do
if (result == undefined) then
n = number %10
else n = result %10
end
timer = timer + 1
m = n*tonumber(frame.args[2])
if (result == undefined) then
floorednum = math.floor(number/10)
else floorednum = math.floor(result/10)
end
result = floorednum + m
end
if (frame.args[4] == "ld") then
return n
elseif (frame.args[4] == "ldm") then
return m
elseif (frame.args[4] == "ldmabs") then
return math.abs(m)
elseif (frame.args[4] == "m") then
return frame.args[2]
elseif (frame.args[4] == "flnum") then
return floorednum
else return result
end
end

function p.trimsum3(frame) -- Right Trim 3
local number = 0 -- The number to be converted to pure number
local a = tonumber(frame.args[3]) --The number of times to be performed
local n                           -- The last 2 digits of the number
local m = 0                       -- Multiply by m times.
local timer = 1                   -- Timer check of how many times performed.
local result                      -- For establishing a variable
for digit in string.gmatch(frame.args[1], "%d") do
number = 10*number + digit
end
while timer <= a do
if (result == undefined) then
n = number %1000
else n = result %1000
end
timer = timer + 1
m = n*tonumber(frame.args[2])
if (result == undefined) then
floorednum = math.floor(number/1000)
else floorednum = math.floor(result/1000)
end
result = floorednum + m
end
if (frame.args[4] == "ld") then
return n
elseif (frame.args[4] == "ldm") then
return m
elseif (frame.args[4] == "ldmabs") then
return math.abs(m)
elseif (frame.args[4] == "m") then
return frame.args[2]
elseif (frame.args[4] == "flnum") then
return floorednum
else return result
end
end

function p.checktrim(frame) -- Used for finding the correct trim number.
local a = tonumber(frame.args[1])
local b = tonumber(frame.args[2])
if (a % 2 == 0) then return "This number is divisible by 2."
elseif (a % 5 == 0) then return "This number is divisible by 5."
elseif (b >= 7) then
return "Too many digits to perform trimming. Process terminated."
else local n = a
while n <= a*10^b do  -- For 2 digits, 10; for 3 digits, 100, etc.
if (n % 10^b == 1) then break end
n = n + 2*a
end
local x = -1*math.floor(n/10^b)
local y = a + x
if (frame.args[3] == "x") then
return x -- x is the negative number
elseif (frame.args[3] == "y") then
return y -- y is the positive number
else return "("..x.." , "..y..")"
end
end
end

return p```