Hello Devforum members! I have recently learned, while social distantly learning, to calculate pi! Although roblox already has math.pi I just wanted to make the game to increase some of my skills and also for my portfolio creations I created it in two days so it isn’t one of my best creations lol
Feedback is appreciated
https://www.roblox.com/games/5249683843/Pi-Calculator-Learn-Explore
Here is the code (sorry it’s kinda messy )
-- I love pi! :)
-- sadly lua doesn't like large decimals
--[[ Calculation Method #1 (Nilakantha Series)
pi = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - ...
]]--
local function pi1(precision)
local timesnum = 0 -- 2*3*4 and so on
local methods = 0 -- number of loops
local divstuff = 0 -- decimals of pi
if precision <= 10000 then -- Crash Proofing :)
repeat
timesnum = timesnum + 4
-- 4/(2*3*4) - 4/(4*5*6) --
divstuff = divstuff + 4/(timesnum*(timesnum+1)*(timesnum+2)) - 4/((timesnum+2)*(timesnum+3)*(timesnum+4))
methods = methods + 1
until methods == precision
else
repeat
timesnum = timesnum + 2
divstuff = divstuff + 4/(timesnum*(timesnum+1)*(timesnum+2)) - 4/((timesnum+2)*(timesnum+3)*(timesnum+4))
methods = methods + 1
if methods == 5000 or methods == 7500 or methods == 10000 or methods == 20000 or methods == 30000 or methods == 50000 then
wait(1) -- Prevents crashing
end
until methods == precision
end
local pi = 3 + divstuff
return pi
end
-- pi2 calculation (Gregory-Leibniz Series)
-- pi/4 == 1 - 1/3 + 1/5 - 1/7 + 1/9...
local function pi2(precision)
local pival = 1
local isNegative = 1
local k = 0
local calc = 3
while k < precision do -- Rerun the calculation, adding accuracy each time, for precision # of steps.
pival = pival - (1/calc) * isNegative -- This is the 1 - 1/3 + 1/5 - 1/7...part.
calc = calc + 2 -- This is the 3, 5, 7 part.
isNegative = -isNegative -- This is the + - alternation part.
k = k + 1 -- How many steps it's run
end
return pival * 4
end
it uses two methods of calculating pi (The Gregory-Leibniz Series and The Nilakantha Series)
This Website was really helpful for learning about pi and was very interesting
Thank you for reading