As I mentioned in other discussions, I don’t play games, but I do like to play with formulas and calculations. Long ago I posted a program that calculated the digits of Pi, but I haven’t been able to find it on the forum. I’m not referring to the one I posted a few days ago. The first program here might be the one I originally posted. It calculated 5,000 digits of Pi in 36.8 seconds on my iPad Air. Since then I’ve been playing around with other calculations and came up with the second program shown here. It calculates 5,000 digits of Pi in .67 seconds, an increase of about 55 times. I ran it for 100,000 digits which took 460 seconds. I’m not sure how many digits it can calculate before it overflows in a calculation.
Original program.
-- calculate digits of Pi
function setup()
local digits=5000 -- number of digits of Pi
print("Calculating "..digits.." digits of pi.")
start=os.clock()
local pi={}
local tab={}
local mf=math.floor
local size=mf(digits)
local size1=mf(size*6.8)
local sum
local carry
local b
for i=0,size1 do
tab[i]=20
end
for t=1,size do
carry=0
for i=size1,0,-1 do
sum=tab[i]*10+carry
b=i*2+1
tab[i]=sum%b
carry=mf(sum/b)*i
end
tab[0]=sum%10
table.insert(pi,mf((sum)/10))
end
carry=0
local x
for z=size,1,-1 do
x=pi[z]
x=x+carry
if x>9 then
x=x-10
carry=1
else
carry=0
end
pi[z]=string.format("%0d",x)
end
pi[1]="3.1"
print(table.concat(pi))
print()
print(digits.." digits of Pi in")
print(os.clock()-start,"seconds")
end
Update program.
-- calculate digits of Pi
function setup()
local digits=5000 -- number of digits of Pi
print("Calculating "..digits.." digits of pi.")
start=os.clock()
local pi={}
local tab={}
local size=math.ceil(digits/7)+1
local size1=math.ceil(size*7)
local sum
local carry
local b
local d1=10000000
local d2=9999999
local str=""
for z=0,size1 do
tab[z]=5*z+3
end
for t=1,size do
carry=0
for z=size1,0,-1 do
sum=tab[z]*d1+carry
b=27*z*z+27*z+6
tab[z]=sum%b
carry=(sum//b)*(2*z*z-z)
end
tab[0]=sum%d1
table.insert(pi,sum//d1)
end
carry=0
for z=size,1,-1 do
b=pi[z]+carry
if b>d2 then
b=b-d1
carry=1
else
carry=0
end
pi[z]=string.format("%07d",b)
end
pi[1]="3."
str=table.concat(pi)
print(string.sub(str,1,digits+2))
print(digits.." digits of Pi in "..os.clock()-start,"seconds")
end