Lots of Pi

Developer(s): Blanchard Smith
Date: 1983
Type: Program
Platform(s): TS 2068

Calculates pi to ever more decimal places: 6, then 14, 30, 62 and so on, doubling each time up to 8,190. Each result is printed on the printer in lines of 80 digits. The arithmetic is done in base 100, with each pair of decimal digits held as one character of a string, so the precision is limited only by memory and patience. The small sizes take a minute or two; the largest would take a very long time.

“Lots of Pi” is by Blanchard Smith and appeared in his “Just for Fun” column in SYNC, November/December 1983, page 9, as the program’s REMs note. This copy is a TS2068 typing of the listing, found on a Larken disk of magazine type-ins. The original sends its output only to the printer, so the zip also holds a second TAP, “screen output”, in which lines 6000 and 6070 use PRINT instead of LPRINT. Nothing else is changed. The Run in Browser button uses the screen version.

Program Analysis

Method

The program uses Machin’s formula, pi = 16·arctan(1/5) − 4·arctan(1/239), summing each arctangent series term by term. A$(1) holds the running total, A$(2) the current power (16/5n, then 4/239n) and A$(3) that power divided by the term’s odd denominator. The sign flips each term (S=-S), and the loop ends when the power has shrunk to zero at the working precision.

Structure

  • Lines 20–300: the outer loop over J = 2 to 12, with N=2^J+1 base-100 digits; the two arctangent series; then printing.
  • Lines 4700–4800: divide A$(2) by D (long division, base 100), and set Z to zero when the result is all zeros.
  • Lines 5000–5290: divide A$(2) by E into A$(3), then add it to or subtract it from A$(1) with carry or borrow.
  • Lines 6000–6100: print “3.” and the digits in 80-character lines.

Notable Techniques

  • Strings of CHR$ values from 0 to 99 serve as multi-digit numbers, read and written with CODE and CHR$.
  • FOR E=1 TO 9E9 STEP 2 with IF Z<>0 THEN NEXT E makes an open-ended loop over the odd denominators.
  • Carries use logical values: LET C=A>=T and CHR$ (A-(T AND C)).
  • Line 55 builds a string of N zero bytes to clear each row of the array.

Potential Issues

  • Without a printer the original version shows nothing; use the screen-output TAP to watch the digits.
  • Computing time grows roughly with the square of the number of digits, so the later passes run for hours.

This program came from a Larken disk and was converted to run from tape: the LKDOS save and load lines and the LOGICALL load were removed, and it is saved without variables. It was tested in ZEsarUX (screen version), giving 3.141592 and 3.14159265358979 for the first two passes.

Related Articles

Calculate PI to 8,192 places; update to Ricochet; INPUT anywhere; COPY lines 23 and 24.

Image Gallery

Source Code

    5 REM PI TO 6,14,...8190 DECIMAL PLACES
    6 REM FROM SYNC NOV./DEC. 1983 PGE 9
   20 LET T=100
   30 FOR J=2 TO 12
   40 LET N=2^J+1
   50 DIM A$(3,N)
   55 LET Z$="": FOR K=1 TO N: LET Z$=Z$+CHR$ 0: NEXT K: LET A$(1)=Z$: LET A$(2)=Z$: LET A$(3)=Z$
   80 LET A$(2,1)=CHR$ 16
   90 LET D=5
  100 GO SUB 4700
  110 LET S=1
  120 LET D=25
  130 FOR E=1 TO 9E9 STEP 2
  140 GO SUB 5000
  150 GO SUB 4700
  160 LET S=-S
  170 IF Z<>0 THEN NEXT E
  180 LET A$(2)=Z$
  190 LET A$(2,1)=CHR$ 4
  200 LET D=239
  210 GO SUB 4700
  220 LET S=-1
  230 FOR E=1 TO 9E9 STEP 2
  240 GO SUB 5000
  250 GO SUB 4700
  260 LET S=-S
  270 GO SUB 4700
  280 IF Z<>0 THEN NEXT E
  290 GO SUB 6000
  300 NEXT J
  310 STOP 
  320 CLEAR 
  330 LET X$="PI"
  340 SAVE X$
  350 STOP 
 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 GO TO 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$<80 THEN NEXT K
 6070 LPRINT P$
 6080 LET P$=""
 6090 NEXT K
 6100 RETURN 
 6200 STOP 
 9000 SAVE "PI": STOP 
 9005 VERIFY "PI"
 9992 SAVE "PI.B1" LINE 1
 9998 REM  was LKDOS-only

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.