Happy Pi Day!
03-14-2020, 12:17 PM (This post was last modified: 06-20-2020 10:53 PM by Gerson W. Barbosa.)
Post: #3
 Gerson W. Barbosa Senior Member Posts: 1,584 Joined: Dec 2013
RE: Happy Pi Day!
314 decimal places might be a more adequate number of places, but it takes 3613 seconds on the HP 50g, or roughly one hour. 100 places takes only 70 seconds, but that's too few digits -- there are people who know those by heart -- so I chose 200, which takes reasonable 10 minutes. Well, reasonable time considering we are using Wallis Product here.

For more digits, you might want to try the algorithm on programming languages that have arbitrary precision extension. Here are 997 digits of pi using Decimal BASIC, which is limited to 1000 digits:

Code:
 OPTION ARITHMETIC DECIMAL_HIGH INPUT  PROMPT "number of digits: ":nd LET tm = TIME  LET n = 2*INT((3*nd + 4)/8) PRINT "n = ";n LET pr = 1 LET n1 = n + 1 LET n2 = n1 - 4 LET d = 4*n LET c = 0 FOR i = 1 TO n/2    LET t = 256*i*(i - 1) + 64    LET pr = pr*i*i*t/(i*(i*(t - 32) + 16) - 3)    LET c = n2/(d + n1/(2 - c))    LET n2 = n2 - 2    LET n1 = n1 - 2 next i LET c = 2 - 2*c LET p = pr*c LET r = TIME - tm LET r$= STR$(p - INT(p)) PRINT PRINT STR$(INT(p)); PRINT r$(0:1); FOR i = 2 TO nd + 1    PRINT r$(i:i); IF MOD((i - 1),10) = 0 THEN PRINT " "; IF MOD((i - 1),50) = 0 THEN PRINT PRINT REPEAT$(" ",1 + LEN(STR$(INT(p)))); END IF NEXT i IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT PRINT PRINT "Runtime: "; PRINT USING "##.##": r; PRINT " seconds" END number of digits: 997 n = 748 3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912 9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132 0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235 4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859 5024459455 3469083026 4252230825 3344685035 2619311881 7101000313 7838752886 5875332083 8142061717 7669147303 5982534904 2875546873 1159562863 8823537875 9375195778 1857780532 1712268066 1300192787 6611195909 2164201 Runtime: .47 seconds Edited to fix a typo PS: Some algorithm and program optimizations ----------------------------------- HP 50g: 314 %%HP: T(3)A(R)F(.); \<< PUSH RAD -105 CF -3 CF 1 DUP PICK3 3 * 8 + 8 IDIV2 DROP DUPDUP + 1 + OVER 8 * 0 \-> m d c \<< FOR i i DUP 16 * 8 - * DUP SQ SWAP DUP 2 - * 3 - / * m 4 - d m 2 c - / + / 'c' STO 'm' 2 STO- NEXT 2 * 1 c - * \>> EXPAND FXND DUP SIZE R\->I ALOG OVER - PICK3 * SWAP IQUOT + \->STR DUP HEAD 0 I\->R \->STR TAIL + SWAP TAIL + 1 ROT 2 + SUB POP \>> TEVAL -> 3.14159265358979323846264338327950288419716939937510 58209749445923078164062862089986280348253421170679 82148086513282306647093844609550582231725359408128 48111745028410270193852110555964462294895493038196 44288109756659334461284756482337867831652712019091 45648566923460348610454326648213393607260249141273 72458700660631 s:3265.5887 (real HP 50g) 337 bytes, CK = # F36Bh ----------------------------------- TurboBCD: Code:  Program Wallis; var i, k, m, n: integer; c, d, t, w: real; begin ClrScr; WriteLn('+---+---------------------+---------------------+'); WriteLn('| N | 2*W | 2*W*C |'); WriteLn('+---+---------------------+---------------------+'); for k := 1 to 7 do begin n := 2*k; w := 1; c := 0; d := 4*n; m := n + 1; for i := 1 to k do begin t := i*(16*i - 8); w := w*t*t/(t*(t - 2) - 3); c := (m - 4)/(d + m/(2 - c)); m := m - 2 end; c := 1 - c; WriteLn('|',n:2,' |',2*w:20:17,' |',2*w*c:20:17,' |') end; WriteLn('+---+---------------------+---------------------+') end. +---+---------------------+---------------------+ | N | 2*W | 2*W*C | +---+---------------------+---------------------+ | 2 | 2.84444444444444444 | 3.14385964912280701 | | 4 | 2.97215419501133786 | 3.14158816337302932 | | 6 | 3.02317019200136082 | 3.14159266276745771 | | 8 | 3.05058999605551092 | 3.14159265357083669 | |10 | 3.06770380664349896 | 3.14159265358983256 | |12 | 3.07940134316788626 | 3.14159265358979314 | |14 | 3.08790206983111306 | 3.14159265358979321 | +---+---------------------+---------------------+ ----------------------------------- Decimal BASIC: Code:  OPTION ARITHMETIC DECIMAL_HIGH INPUT PROMPT "number of digits: ":nd LET tm = TIME LET n = 2*INT((3*nd + 8)/8) POPTION ARITHMETIC DECIMAL_HIGH INPUT PROMPT "number of digits: ":nd LET tm = TIME LET n = 2*INT((3*nd + 8)/8) PRINT "n = ";n LET w = 1 LET c = 0 LET d = 4*n LET m = n + 1 FOR i = 1 TO n/2 LET t = i*(16*i - 8) LET w = w*t*t/(t*(t - 2) - 3) LET c = (m - 4)/(d + m/(2 - c)) LET m = m - 2 next i LET c = 1 - c LET p = 2*w*c LET r = TIME - tm LET r$ = STR$(2*w*c - INT(2*w*c)) PRINT PRINT STR$(INT(p)); PRINT r$(0:1); FOR i = 2 TO nd + 1 PRINT r$(i:i);    IF MOD((i - 1),10) = 0 THEN PRINT " ";    IF MOD((i - 1),50) = 0 THEN        PRINT       PRINT REPEAT$(" ",1 + LEN(STR$(INT(p))));    END IF NEXT i IF MOD (i - 2,50) <> 0  OR nd = 0 THEN PRINT PRINT  PRINT "Runtime: "; PRINT  USING "##.##": r; PRINT " seconds" END
 « Next Oldest | Next Newest »

 Messages In This Thread Happy Pi Day! - Gerson W. Barbosa - 03-14-2020, 02:08 AM RE: Happy Pi Day! - pinkman - 03-14-2020, 06:19 AM RE: Happy Pi Day! - Gerson W. Barbosa - 03-14-2020, 02:53 PM RE: Happy Pi Day! - rprosperi - 03-14-2020, 05:04 PM RE: Happy Pi Day! - Jim Horn - 03-14-2020, 05:15 PM RE: Happy Pi Day! - Gerson W. Barbosa - 03-14-2020 12:17 PM RE: Happy Pi Day! - Dave Britten - 03-14-2020, 12:47 PM RE: Happy Pi Day! - BruceH - 03-15-2020, 11:32 AM RE: Happy Pi Day! - Eddie W. Shore - 03-15-2020, 07:29 PM RE: Happy Pi Day! - DM48 - 12-12-2021, 06:44 PM RE: Happy Pi Day! - Albert Chan - 12-12-2021, 11:04 PM RE: Happy Pi Day! - Gerson W. Barbosa - 12-13-2021, 12:41 AM RE: Happy Pi Day! - DM48 - 12-13-2021, 01:54 AM RE: Happy Pi Day! - Gil - 12-13-2021, 01:21 AM RE: Happy Pi Day! - Gerson W. Barbosa - 12-13-2021, 03:57 AM

User(s) browsing this thread: 1 Guest(s)