In looking for a 'natural' way to compute pi on a 16C I came across
the following formula attributed to Newton:
pi/6 = 1/2 + (1)/(2*3)/(2^3) + (1*3)/(2*4*5/(2^5) + (1*3*5)/(2*4*6*7)/(2^7) + ...
Which can be written in a more algorithmic-like form:
pi * 1000....0 = 3 * 1000...0 * (x(0) + x(1) + x(2) + ...)
where X(0) = 1; i=1
X(n) = X(n-1) * (i*i)/(i+1)/(i+2)/2/2; i=i+2
Here it is implemented on a 16C using no registers other than 'I'
and no assumptions about number base, word size or complement mode.
The first 20 lines of the program are just to get the largest power of 10
(times 3) that will fit into the current word size without knowing the
current number base.
LBL A
1 ;
STO I ; starting value of iSL ;
LST x ; 3 (base 10)
+ ;LBL 9
1 ;
SL ;
ENTER ; 10 (base 10)
SL ;
SL ;
+ ;X<>Y ; SWAP
x ; times
F? 5 ; Check for overflow - overflow is used to determine max word length
GTO 8
GTO 9 ; keep multiplying by 10LBL 8
LST x ; Restore maximal value 300....0LBL 7 ; Top of main loop
LST x ; X(n-1)RCL I ;
x ;
RCL I ;
x ; *(i*i)ISZ ;
RCL I ;
/ ; /(i+1)
ISZ ;
RCL I ;
/ ; /(i+2)SR
SR ; /2/2x=0
GTO 6 ; check for end, nothing more to add in+ ; accumulate X(n)
GTO 7 ; keep going...LBL 6
RDN ; roll down to result
RTN
Convergence is so-so at about .6 digits per iteration. With a 64-bit
word size, unsigned mode the calculator gives 19 digits of pi as
3141592653589793226 (the last 2 are wrong due to truncation) in about
90 seconds. It's not fast, but it is portable and fairly simple.
Edited: 30 Oct 2006, 1:23 a.m.