'Subject: Calculation of pi with Gauss's equation
'         pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)
'         using James Gregory's inverse tangent series.
'Author : Sjoerd.J.Schaper
'Date   : 09-27-2004
'Code   : QuickBasic, PDS, VBdos
REM $LANG: "qb"
REM $INCLUDE: 'largeint.bi'
CONST sum = 0, one = 1, pwd = 2, sqd = 3, pi = 4
n = LargeInit(pi, "pi_atan")
CLS

begin:
LOCATE 1, 1
PRINT "   +------------------------+"
PRINT "   |  Pi-digits [1..5587],  |"
PRINT "   |  0 to exit.            |"
PRINT "   |                        |"
PRINT "   +------------------------+"
LOCATE 4, 7: INPUT ; "e"; g$
IF INSTR(g$, "'") THEN GOTO begin
IF VAL(g$) < 1 OR VAL(g$) > 11176 THEN
   PRINT "value out of range": GOTO eind
END IF
LOCATE 7, 2
e = VAL(g$): tim# = TIMER

REDIM d(2) AS LONG, m(2) AS LONG
d(0) = 18: d(1) = 57: d(2) = 239 '     inverse cotangent arguments
m(0) = 12: m(1) = 8: m(2) = -5 '       coefficients

Letf one, 10: Powr one, e + 5 '        initialize unit
FOR n = 0 TO 2
   od = 1: s = 1: PRINT n + 1;
   Letf pwd, d(n): Squ pwd, sqd '      initialize power series,
   Dup one, t0: Divd t0, pwd, sum '    arctan series
   Dup sum, pwd
   DO
      Divd pwd, sqd, t0 '              pwd[t]:= pwd[t-1] / d(n)^2
      Dup t0, pwd: od = od + 2
      r = Divint(t0, od): Sets t0, s ' t0:= pwd[t] / (2t + 1)
      IF Isf(t0, 0) THEN EXIT DO '     precision exhausted
      Subt sum, t0: s = -s '           sum[t]:= sum[t-1] - sgn[t-1] * t0
   LOOP
   Letf t0, m(n): Mult sum, t0, t1
   IF n THEN
      Add pi, t1 '                     pi:= pi + m(n) * sum
   ELSE
      Swp pi, t1
   END IF
NEXT n
PRINT
Lsft pi, 2
Printr pi, one, "pi = ", e
Prints STR$(e) + " digits", 2

eind:
g$ = STR$(CSNG(TIMER - tim#))
Prints "Timer:" + g$ + " s", 1
CALL Term
SYSTEM

