Calculating pi
im making a factory game where you use the digits of pi to make pies,
I have the code to calculate it, but I can’t get around the decimal place restrictions.
Does anyone have a work-around to this problem?
(I’ll put your username in credits for help)
here’s my code:
make_pie = function(size)
k = 1
s = 0
for i=0 to size
if i % 2 == 0 then
s = s + 4/k
else
s = s - 4/k
end
print(s)
k = k + 2
end
return s
end
I don't know If this is what you need but, you can use the integrated PI variable, but I havn't seen PI calculated like that, nice work!
The code you wrote wasn’t actually calculating π because the setup didn’t follow the correct mathematical pattern. The variable i was never defined, so the even and odd check never worked, and since k started at 0, dividing by it immediately caused a problem. Even if those parts were fixed, the formula itself wasn’t structured in a way that makes the value move toward π — it just kept shifting around without ever converging.
This updated version fixes all of that by using the proper Leibniz series for π. It adds and subtracts fractions in a specific pattern that slowly approaches the real value. The variable k starts at 1 and increases by 2 each time to only use odd numbers, while sign flips between positive and negative so each term alternates correctly. As it runs, the printed value gradually gets closer and closer to 3.14159.
init = function()
s = 0
k = 1
sign = 1
i = 0
end
update = function()
for n = 1 to 100 do
s = s + sign * 4 / k
k = k + 2
sign = -sign
i += 1
end
print("iteration: " .. i .. " | pi ≈ " .. s)
end
I did do that
It just didn’t register as code
Look up
Also the problem was that i can’t get around float precision, not the formula i was using
If you put a high enough number it works