it's a bit early but this program will compute Pi to a million digits in mere seconds, or less if you have a fast PC
formula from https://en.wikipedia.org/wiki/Borwein%2 ... nce_(1985)
in case you want to check your result https://stuff.mit.edu/afs/sipb/contrib/pi/
my time about 1.33 seconds
Code: Select all
#include once "gmp.bi"
dim as __mpf_struct a, y, y2, y4, pi
dim as long prec, p2, n, n2=1000000
dim as double lt=3.33333, t=timer
prec=n2*lt
mpf_init2(@a, prec)
mpf_init2(@y, prec)
mpf_init2(@y2, prec)
mpf_init2(@y4, prec)
mpf_init2(@pi, prec)
mpf_set_ui(@a, 2) 'a=2
mpf_sqrt(@a, @a) 'a=sqr(2)
mpf_set(@y, @a) 'y=sqr(2)
mpf_sub_ui(@y, @y, 1) 'y=sqr(2)-1
mpf_mul_ui(@a, @a, 4) 'a=sqr(2)*4
mpf_ui_sub(@a, 6, @a) 'a=6-sqr(2)*4
p2=2
n=log(n2)*log(2)
for i as long=1 to n
p2*=4
mpf_mul(@y2, @y, @y)
mpf_mul(@y4, @y2, @y2)
mpf_ui_sub(@y, 1, @y4)
mpf_sqrt(@y, @y)
mpf_sqrt(@y, @y)
mpf_ui_sub(@y2, 1, @y) 'y2=1-y
mpf_add_ui(@y4, @y, 1) 'y4=1+y
mpf_div(@y, @y2, @y4) 'y=(1-y)/(1+y)
mpf_add_ui(@y2, @y, 1) 'y2=1+y
mpf_mul(@y2, @y2, @y2) 'y2=y2*y2
mpf_mul(@y4, @y2, @y2) 'y4=y2*y2
mpf_mul(@y2, @y, @y) 'y2=y*y
mpf_add(@y2, @y2, @y) 'y2=y+y*y
mpf_add_ui(@y2, @y2, 1) 'y2=1+y+y*y
mpf_mul(@y2, @y2, @y)
mpf_mul_ui(@y2, @y2, p2)
mpf_mul(@a, @a, @y4)
mpf_sub(@a, @a, @y2)
'a=a*y4-p2*y*(1+y+y*y)'/
next
mpf_ui_div(@pi, 1, @a) 'pi=1/a
t=timer-t
print "it took ";n;" loops in ";t;" seconds"
print "press return to continue"
sleep
dim frmt as string
dim as long length = mpf_get_prec(@pi)*0.3010299956639812
if length>n2 then length=n2
frmt="%"+str(length+4)+"."+str(length-1)+"f"
frmt = left(frmt,len(frmt)-1)+"F"+right(frmt,1)
length = gmp_snprintf(0, 0, frmt, @pi)
dim as zstring ptr rstring=allocate(length+20)
gmp_sprintf(rstring, frmt, @pi)
print *rstring
print length
print "press return to end"
sleep
deallocate(rstring)
mpf_clear(@pi)
mpf_clear(@y4)
mpf_clear(@y2)
mpf_clear(@y)
mpf_clear(@a)