HP Forums
(35S) spigot algorithm for the digits of PI - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (35S) spigot algorithm for the digits of PI (/thread-4025.html)



(35S) spigot algorithm for the digits of PI - Thomas Klemm - 05-30-2015 05:03 PM

Program:
Code:
P001 LBL P      P018 FS? 0      P035 INT÷
P002 STO N      P019 GTO P022   P036 DSE J
P003 4          P020 RCL(J)     P037 GTO P017
P004 INT÷       P021 GTO P023   P038 ENTER
P005 STO I      P022 2E3        P039 CF 0
P006 CLx        P023 RCL× F     P040 RCL F
P007 STO D      P024 +          P041 INT÷
P008 STO E      P025 1          P042 RCL+ E
P009 1E4        P026 RCL J      P043 STO P
P010 STO F      P027 ENTER      P044 VIEW P
P011 SF 0       P028 +          P045 x<>y
P012 14         P029 x<>y       P046 RCL F
P013 RCL× I     P030 −          P047 RMDR
P014 STO J      P031 RMDR       P048 STO D
P015 DSE J      P032 STO(J)     P049 STO E
P016 RCL D      P033 x<>y       P050 DSE I
P017 RCL× J     P034 LASTx      P051 GTO P012

Usage:
Calculate the first 40 digits of \(\pi\): 3.141592653589793238462643383279502884197

40 XEQ P001

For the next digits to appear just keep hitting the [R/S] button:
Code:
P=
      3141
      5926
      5358
      9793
      2384
      6264
      3383
      2795
       288     <<< mind the missing 0
      4197

This is a translation of the C-program from:
Pi Unleashed.
Jörg Arndt, Christoph Hänel

Rediscovered in: A Sigma Function in the 35s Solver!


RE: (HP-35s) spigot algorithm for the digits of \(\pi\) - Steve Simpkin - 05-30-2015 09:33 PM

Thanks for the fun program!

A couple of notes:

Set display format to ALL for best results.

On my HP35s, 40 XEQ P001 takes about 33 seconds to execute. The next press of R/S takes about 26 seconds. The execution time of each subsequent press of R/S gets progressively smaller with about 3 seconds for the final calculation.

Do not press R/S after the final calculation. The execution time will be very long.


RE: (HP-35s) spigot algorithm for the digits of \(\pi\) - Thomas Klemm - 05-31-2015 03:11 AM

Instead of 4 digits a time we can display 5 or 6 digits together.
For this the magic numbers have to be adjusted accordingly.

5 digits:
Code:
P003 5
P009 1E5
P012 17
P022 2E4

6 digits:
Code:
P003 6
P009 1E6
P012 21
P022 2E5

Make sure the GTO-commands still point to the correct lines after changing the program.


RE: (HP-35s) spigot algorithm for the digits of \(\pi\) - Thomas Klemm - 05-31-2015 03:39 AM

(05-30-2015 09:33 PM)Steve Simpkin Wrote:  The execution time of each subsequent press of R/S gets progressively smaller with about 3 seconds for the final calculation.

Quote:   The second improvement is really a textbook tip which, however,
was evidently forgotten in the original formulation of the spigot algo-
rithm. Namely, with a radix conversion, after each result place, one can
shorten the remainder that still has to be converted, by the number of
bits or the result place. Hence after each calculation of one place con-
sisting of 4 decimal digits, the length of f[] field can be reduced
by \(\left \lfloor 10 \cdot 4 / 3 + 1 \right \rfloor = 14\) places. This improvement speeds up the program
by an additional factor of 2.
Pi - Unleashed, p. 83


RE: (HP-35s) spigot algorithm for the digits of \(\pi\) - pbnelson - 03-24-2017 10:25 PM

This was a lot of fun. I used it to compute the first 100 digits of PI in about 15 minutes. It in fact returned 105 digits, the first 103 being correct, and the 104th & 105th digits being wrong (according to Google). I am pretty well flabbergasted that such a small program can give 103 digits of PI correctly in only 15 minutes. Thanks for putting this together.