﻿ Stuart Salzer view profile on InterSystems Developer Community
Recent posts:
Recent replies:

I wrote this a while ago. Just ran on a MacBook Pro Mid 2015, 2.8 GHz Intel Core i7.

It computed the first 1000 digits in less than 1 minute, 2000 digits in 5 minute, 3000 digits, in 14 minute, 4000 digits in 32 minute, 5000 digits in 60 minute...

PI    ; SRS 2011-07-18
; Compute PI using Plouffe and Bellard algorithm, based upon "C" code
; found at <http://iweb.dl.sourceforge.net/project/projectpi/
; Digit%20Extraction%20Methods/Plouffe%20and%20Bellard%20v1/pi1_f.c>.
KILL ^PI,^PRIME
SET ^PI=-1
FOR I=0:1:9 JOB C(\$INCREMENT(^PI))
QUIT
C(p)    NEW \$ETRAP SET \$ETRAP="DO ^%ETN"
SET n=p*9+1
SET bign=+\$TRANSLATE(n+20*\$ZLN(10)/\$ZLN(2),".","!")
SET sum=0
SET a=3 FOR  QUIT:a>(2*bign)  DO  SET a=\$\$NP(a)
. SET vmax=+\$PIECE(\$ZLN(2*bign)/\$ZLN(a),".")
. SET av=1 FOR i=1:1:vmax SET av=av*a
. SET s=0,num=1,den=1,v=0,kq=1,kq2=1
. FOR k=1:1:bign DO
. . SET t=k
. . IF kq'<a FOR  SET t=t\a,v=v-1 IF t#a SET kq=0 QUIT
. . SET kq=kq+1,num=num*t#av
. . SET t=2*k-1
. . DO:kq2'<a
. . . IF kq2=a FOR  SET t=t\a,v=v+1 QUIT:t#a
. . . SET kq2=kq2-a
. . SET den=den*t#av,kq2=kq2+2
. . DO:v>0
. . . SET t=\$\$IM(den,av),t=t*num#av,t=t*k#av
. . . FOR i=v+1:1:vmax SET t=t*a#av
. . . SET s=s+t
. . . SET:s'<av s=s-av
. SET t=\$\$PM(10,n-1,av),s=s*t#av
. SET sum=sum+(s/av),sum=+("."_\$PIECE(sum,".",2))
SET ^PI(p)=\$EXTRACT(\$PIECE(sum,".",2)_"000000000",1,9)
JOB C(\$INCREMENT(^PI))
QUIT
NP(a)    NEW (a) FOR  SET r=\$ORDER(^PRIME(a)) QUIT:r'=""  DO  QUIT:r'=""
. LOCK ^PRIME
. SET r=\$ORDER(^PRIME(a)) IF r'="" LOCK  QUIT
. IF \$DATA(^PRIME)=0 SET ^PRIME=3,^PRIME(2)="",^PRIME(3)="" LOCK  QUIT
. FOR r=^PRIME:2 DO  IF pr SET ^PRIME(r)="" QUIT:r>a
. . SET pr=1 FOR p=3:2:\$ZSQR(r) IF r#p=0 SET pr=0 QUIT
. SET ^PRIME=r
. LOCK
QUIT r
IM(x,y)    NEW (x,y)
SET u=x,v=y,c=1,a=0
FOR  SET q=v\u,t=c,c=a-(q*c),a=t,t=u,u=v-(q*u),v=t QUIT:u=0
SET a=a#y
SET:a<0 a=a+y
QUIT a
PM(a,b,m)    NEW (a,b,m)
SET r=1,aa=a
FOR  SET:b#2 r=r*aa#m SET b=b\2 QUIT:b=0  SET aa=aa*aa#m
QUIT r

In most recent versions of Caché, we install a directory dev/cache/callout/demo. This contains demonstration code  for writing \$ZF() functions. One of the examples is tzchange(), and it works by setting a environment variable in the current process, and it should be easy enough to adapt to your needs. Start by reading czf.pdf.

Followers:
Stuart has no followers yet.
Following:
Stuart has not followed anybody yet.