This program can calculate PI to over 8000 decimal places– but don’t hold your breath. It takes over 12 hours for 512 digits! The algorithm uses base 100 arithmetic, storing each digit in one byte of a character string (codes 0-99). It is based on the Taylor series for the arctangent.
First PI prints out 8 digits, then it starts all over again and does 16, then 32, 64, etc. If you don’t have a printer, change all the LPRINTs to PRINTs and add a STOP at line 6095. Go on to the next calculation with CONT.
Since the program allocates 12K of RAM for the accumulators A$(1), A$(2), and A$(3), CLEAR before SAVEing to avoid storing lots of variable space to tape.
3.141592653589793238462643383279
50288419716939937510582097494459
23078164062862089986280348253421
17067982148086513282306647093844
60955058223172535940812848111745
02841027019385211055596446229489
54930381964428810975665933446128
47564823378678316527120190914564
85669234603486104543266482133936
07260249141273724587006606315588
17488152092096282925409171536436
78925903600113305305488204665213
84146951941511609433057270365759
59195309218611738193261179310511
85480744623799627495673518857527
24891227938183011949129833673362
10 REM PI TO 4,8,16,,,8192 DECIMAL PLACES
20 LET T=100
30 FOR J=2 TO 12
40 LET N=2**J+1
50 DIM A$(3,N)
60 LET A$(1)=""
70 LET A$(2)=""
80 LET A$(2,1)=CHR$ 16
90 LET D=5
100 GOSUB 4700
110 LET S=1
120 LET D=25
130 FOR E=1 TO 9999 STEP 2
140 GOSUB 5000
150 GOSUB 4700
160 LET S=-S
170 IF Z<>0 THEN NEXT E
180 LET A$(2)=""
190 LET A$(2,1)=CHR$ 4
200 LET D=239
210 GOSUB 4700
220 LET S=-1
230 FOR E=1 TO 9999 STEP 2
240 GOSUB 5000
250 GOSUB 4700
260 LET S=-S
270 GOSUB 4700
280 IF Z<>0 THEN NEXT E
290 GOSUB 6000
300 NEXT J
310 STOP
320 CLEAR
330 LET X$="PI"
340 SAVE X$
350 RUN
4700 LET A=0
4710 LET Z=0
4720 FOR K=1 TO N
4730 LET A=CODE A$(2,K)+A
4740 LET Q=INT (A/D)
4750 LET A$(2,K)=CHR$ Q
4760 LET R=A-D*Q
4770 LET A=R*T
4780 LET Z=Z OR (Q+R)
4790 NEXT K
4800 RETURN
5000 LET A=0
5100 FOR K=1 TO N
5110 LET A=CODE A$(2,K)+A
5120 LET Q=INT (A/E)
5130 LET A$(3,K)=CHR$ Q
5140 LET A=(A-E*Q)*T
5150 NEXT K
5160 LET C=0
5170 IF S<0 THEN GOTO 5240
5180 FOR K=N TO 1 STEP -1
5190 LET A=CODE A$(1,K)+CODE A$(3,K)+C
5200 LET C=A>=T
5210 LET A$(1,K)=CHR$ (A-(T AND C))
5220 NEXT K
5230 RETURN
5240 FOR K=N TO 1 STEP -1
5250 LET A=CODE A$(1,K)-CODE A$(3,K)-C
5260 LET C=A<0
5270 LET A$(1,K)=CHR$ (A+(T AND C))
5280 NEXT K
5290 RETURN
6000 LPRINT " "
6010 LET P$="3."
6020 FOR K=2 TO N-1
6030 LET X$="0"+STR$ CODE A$(1,K)
6040 LET P$=P$+X$(LEN X$-1 TO )
6060 IF LEN P$<32 THEN NEXT K
6070 LPRINT P$
6080 LET P$=""
6090 NEXT K
6100 RETURN
SYNTACTIC SUM: 14842, 8K ROM
For the TS2068, translate with FIRSTLOADER (SYNTAX, Dec. 83), then delete lines 60 and 70, add 55, and replace 180:
55 LET Z$="": FOR K=1 TO N: LE
T Z$=Z$+CHR$ 0: NEXT K: LET A$(1
)=Z$: LET A$(2)=Z$: LET A$(3)=Z$
180 LET A$(2)=Z$
Media
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.