'Almost all of the following code was posted by someone on http://www.codecodex.com/wiki/Calculate_digits_of_pi
'That was taken from American Mathematical Monthly (see below).
'I converted it to also make a .txt file of Pi.txt if the user wishes to.
'PI.BAS 'Prints PI to number of decimal places desired. 19830623tac.
' Ref: American Mathematical Monthly vol#45,1938 p657-667.
' Ref: Best of Micro vol.#1 p85-86.
'
'
' * ---- ----
' ***** \ 16(-1)^(k+1) \ 4(-1)^(k+1)
' * * * --- > ------------ -- > ----------------
' * * --- / (2k-1)5^(2k-1) / (2k-1)239^(2k-1)
' * * ---- ----
' * * k=1 k=1
'
'
PRINT "This program will produce Pi to as many digits as you wish." PRINT "But I do not recommend going over 20000 because it's pretty slow." PRINT "Do you wish to also have it make a .txt file called Pi.txt" PRINT "that can be opened with a program such as Windows Notepad?" PRINT "The program will also open Pi.txt after it is finished making it." PRINT "If you just see it on the screen, you will only see as much as" PRINT "the screen can show at the end." INPUT "Type Yes or No here:"; ff$
main:
TEN = 100
INPUT "Enter number of digits wanted"; SIZE:
PLACES = SIZE - 1
IF SIZE
> 200 THEN TEN
= 10 ELSE SIZE
= (SIZE
+ 1) / 2
DIM POWER
(SIZE
), TERM
(SIZE
), RESULT
(SIZE
) K(1) = 25: K(2) = 239 'Constants
C(1) = 5: C(2) = 239
'Main loop.
Init:
POWER
(L
) = 0: TERM
(L
) = 0:
IF PASS
= 1 THEN RESULT
= 0 POWER(0) = 16 / PASS ^ 2
MODE = 0
DIVISOR = C(PASS)
XPONENT = 1
SIGN = 3 - 2 * PASS
'
copy:
FOR L
= 0 TO SIZE: TERM
(L
) = POWER
(L
):
NEXT L
'
MODE = 1
DIVISOR = XPONENT
MODE
= (SIGN
< 0) + ABS(SIGN
> 0)
XPONENT = XPONENT + 2
SIGN = -SIGN
MODE = 0
DIVISOR = K(PASS)
'
'Print result.
PRINT "The value of PI to"; PLACES;
" decimal places" IF (TEN
= 100) * (RESULT
(L
) < 10) THEN nxt:
divide:
'Division subroutine. mode0 = Power(x)/Div else mode1 = Term(x)/Div.
DIGIT = 0: ZERO = 0
DIGIT = DIGIT + TERM(L1) * MODE + POWER(L1) - POWER(L1) * MODE
QUOTIENT
= INT(DIGIT
/ DIVISOR
) RESIDUE
= DIGIT
MOD DIVISOR
ZERO
= ZERO
OR (QUOTIENT
+ RESIDUE
) IF MODE
THEN TERM
(L1
) = QUOTIENT
ELSE POWER
(L1
) = QUOTIENT
DIGIT = RESIDUE * TEN
MODE = 0
addsub:
'Add/Subtract subroutine. mode - 1 = Subtract else mode1 = Add.
CARRY = 0
SUM = RESULT(L1) + TERM(L1) * MODE + CARRY * MODE: CARRY = 0
IF (MODE
= 1) * (SUM
< TEN
) + (MODE
= -1) * (SUM
>= 0) THEN GOTO t1
SUM = SUM + MODE * -TEN: CARRY = 1
t1:
RESULT(L1) = SUM
MODE = 0