Sunday, November 13, 2016

TI-89 and HP Prime: Approximation Digits of π to Lots of Digits

TI-89 and HP Prime:  Approximation Digits of π to Lots of Digits 

Ways to find digits of π

Four steps with a calculator with CAS, which allows computation with large integers:

1.  Take a series and multiply each term by 10^n, where n is the number of digits desired. The exact form of each calculation 
2. Take the integer portion of the sum. Make any adjustments when necessary. 
3. Convert the sum to a string.  Insert a decimal point when appropriate. 
4. Return the answer as a string. 

There are three methods that are demonstrated, one routine is presented for the TI-89 and the HP Prime.  All programs presented takes two arguments: n number of digits, m is the number of iterations. Remember that m must be sufficiently high to get an accurate computation of π. 

In the TI-89 programs, (C) marks a comment.  All HP Prime programs on this blog entry are to be run in CAS mode (make sure CAS is checked when creating the program).  

Keep in mind the HP Prime is significantly faster than the TI-89.  

Newton/Euler - newpi

π = 2 * Σ ( 2^k * (k!)^2 / (2*k + 1)!, k, 0, infinity)

TI-89 Program newpi
newpi(n,m)
(C) Newton/Euler π 
Local s,k,t
(C) initialize
0 → t
(C) loop
For k,0,m
t + exact( 10^n * 2^k * (k!)^2 / ( (2*k + 1)! ) ) → t
EndFor
iPart(2*t) → t
string(t) → s
left(s,1) & "." & right(s,n) → s
Disp s
EndPrgm

HP Prime CAS Program newpi
#cas
newpi(n,m):=
BEGIN
LOCAL t,k,s;
// Netwon/Euler π 
// 2016-11-13 EWS
t:=0;
FOR k FROM 0 TO m DO
t:= t + exact( 10^n * 2^k * (k!)^2 /  ( (2*k + 1)! ));
END;
t:= IP(2*t);
// string, left, right 
// must be in lower case
s:= string(t);
s:= left(s,1) + "." + right(s,n);
return s;
END;
#end

newpi(20, 175) returns "3.14159265358979323846"

Arctangent - arctanpi

π = Σ ( 2^(k+1) / ( COMB(2*k, k) * (2*k +1) ), k, 0, infinity)

TI-89 Program arctanpi
arctanpi(n,m)
(C) Arctan π 
Local s,k,t
(C) initialize
0 → t
(C) loop
For k,0,m
t + exact( 10^n * 2^(k + 1) / ( nCr(2*k,k) * (2*k+1) ) ) → t
EndFor
iPart(t) → t
string(t) → s
left(s,1) & "." & right(s,n) → s
Disp s
EndPrgm

HP Prime CAS Program arctanpi
#cas
arctanpi(n,m):=
BEGIN
LOCAL t,k,s;
// Arctan π 
// 2016-11-13 EWS
t:=0;
FOR k FROM 0 TO m DO
t:= t + exact( 10^n * 2^(k+1)/(COMB(2*k,k) * (2*k + 1)));
END;
t:= IP(t);
// string, left, right 
// must be in lower case
s:= string(t);
s:= left(s,1) + "." + right(s,n);
return s;
END;
#end

arctanpi(25, 200) returns "3.1415926535897932384626433"

Bailey-Borwein-Plouffe Formula (BBP) - bbppi

π = Σ ( 1/16^k * ( 4/(8*k +1) - 2/(8*k + 4) - 1/(8*k + 5) - 1/(8*k + 6) ), k, 0, infinity)

TI-89 Program bbppi
bbppi(n,m)
(C) BBP π 
Local s,k,t
(C) initialize
0 → t
(C) loop
For k,0,m
t + exact( 10^n/16^k * ( 4/(8*k + 1) - 2/(8*k + 4) - 1/(8*k + 5) - 1/(8*k + 6) ) ) → t
EndFor
iPart(t) → t
string(t) → s
left(s,1) & "." & right(s,n) → s
Disp s
EndPrgm

HP Prime CAS Program bbppi
#cas
bbppi(n,m):=
BEGIN
LOCAL t,k,s;
// Netwon/Euler π 
// 2016-11-13 EWS
t:=0;
FOR k FROM 0 TO m DO
t:= t + exact( 10^n/16^k * ( 4/(8*k + 1) - 2/(8*k + 4) - 1/(8*k + 5) 
- 1/(8*k + 6) ) );
END;
t:= IP(t);
// string, left, right 
// must be in lower case
s:= string(t);
s:= left(s,1) + "." + right(s,n);
return s;
END;
#end

bbppi(25,200) returns "3.1415925635897932846"


Source:  Wikipeida.  "Approximations of π"  https://en.m.wikipedia.org/wiki/Approximations_of_π. Retrieved November 12, 2016

This blog is property of Edward Shore, 2016. 

1 comment:

  1. Interesting.
    Regarding the HP PRIME and the command "string", it's important to have it in lowercase. That's what you wrote, you're right. But me when I entered it instead of typing it letter by letter I took it from the command catalog and it was all in uppercase, like "STRING". So I shrugged my shoulders and took the version in uppercase thinking it would do the same... But not! It looks like "string" in lower case will return result in a kind of exact mode, with all the digits which is not the case with "STRING" where it will use 10 exponents...
    It's interesting to see that for "left" and "right" function they do have both versions of lowercase/uppercase flavor of the command in the catalog but for the "STRING", in the catalog, there is only the uppercase version. You need really to it manually in lower case to get your program work.
    It's interesting that in CAS in the command line when you type things to computer, if you select "STRING", the single one offered from the catalog in uppercase, when it types it in the command line it will type it in lowercase! But while typing a program, at that moment it type is "STRING" in uppercase.
    (I'm using Software Version:2016 08 29(10637))

    ReplyDelete

HP Prime and TI-84 Plus: Basic Wheatstone Full Bridge Circuit

HP Prime and TI-84 Plus:  Basic Wheatstone Full Bridge Circuit The program WHEATSTONE (HP Prime) and WHTSTONE (TI-84 Plus CE) deals wi...