Pi digits (again) - an unbounded spigot program
|
05-12-2024, 05:03 PM
(This post was last modified: 05-12-2024 07:42 PM by EdS2.)
Post: #1
|
|||
|
|||
Pi digits (again) - an unbounded spigot program
There's a previous mention of Jeremy Gibbons' unbounded spigot not too far away (1, 2) but I don't think I've seen a running version here.
Anyone care to convert the following Basic code, or something close to this JavaScript code, to an HP-71B, or perhaps to a Free42 based calculator, or a Prime, or perhaps a Sharp Pocket Computer? Strictly, for this spigot to work properly, we need a bignum package, but it turns out to do something interesting even if you just use ordinary floating point numbers. We might get 40 digits of pi on Free42, for example - or we might not! Because this code will only print some number of correct digits in any given environment, it might be worth reformulating with a FOR NEXT loop that counts out the correct digits and then stops. (Ideally, also print the first incorrect digit too, so we know we've reached the right point.) Code: 10 q = 1: r = 0: t = 1: k = 1: n = 3: l = 3 You can see this code running in a BBC Micro context as a one-liner here. (Edit: this link works as expected in Chrome, not in Safari, sorry about that. Not sure about other browsers. If you don't see a pi program running, just copy the text from the box above and paste it in as a replacement to the left pane of owlet.) |
|||
05-12-2024, 06:03 PM
Post: #2
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 05:03 PM)EdS2 Wrote: You can see this code running in a BBC Micro context as a one-liner here. Well, close but not the same program; this runs a multi-colored HELLO WORLD, but I the way you can send the actual code to the site via the link is clever! --Bob Prosperi |
|||
05-12-2024, 07:43 PM
(This post was last modified: 05-12-2024 07:54 PM by EdS2.)
Post: #3
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
Bother - if the owlet link doesn't work for you (it works for me) please paste the program into the left pane.
Edit: or perhaps try https://bbcmic.ro/?t=9SPqt |
|||
05-12-2024, 08:23 PM
Post: #4
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 07:43 PM)EdS2 Wrote: if the owlet link doesn't work for you It is a well known issue that MyBB breaks long links: Image hyperlink insertion - LTR marks (?) automatically inserted then http 404 |
|||
05-12-2024, 08:25 PM
Post: #5
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
It works right with the new link, but it halts after 31459265 with "Too big at line 10", where it exceeded what the old machine could handle. Funny that with all the commands in a single line, that error message is not really so helpful, lol...
--Bob Prosperi |
|||
05-12-2024, 08:50 PM
(This post was last modified: 05-12-2024 09:00 PM by Thomas Klemm.)
Post: #6
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 05:03 PM)EdS2 Wrote: Anyone care to convert the following Basic code, or something close to this JavaScript code, to an HP-71B, or perhaps to a Free42 based calculator, or a Prime, or perhaps a Sharp Pocket Computer? Cf. (35S) spigot algorithm for the digits of PI Cf. Many Digits of Pi (HP32sii, HP-16c, HP-12c, HP-12cp, HP-19c, HP-30b, HP-15c) Quote:based on: I could be wrong, but I assume that Katie Wasserman used the same spigot algorithm. |
|||
05-12-2024, 08:57 PM
Post: #7
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
Thanks Thomas! I think that's the earlier Rabinowitz (and Wagon) bounded spigot (although I'm not sure) - this on the other hand is Gibbons' unbounded spigot. Different, in some sense, except that we don't have bignums here. So it will in fact come out less capable than the bounded spigot...
|
|||
05-13-2024, 11:57 AM
Post: #8
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 05:03 PM)EdS2 Wrote: Anyone care to convert the following Basic code, or something close to this JavaScript code, to an HP-71B, or perhaps to a Free42 based calculator, or a Prime, or perhaps a Sharp Pocket Computer? I can't even run this in JavaScript. I get an error on the q in line 10: Expected ";" but found "q" Tom L Cui bono? |
|||
05-13-2024, 07:21 PM
(This post was last modified: 05-14-2024 02:05 AM by toml_12953.)
Post: #9
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 05:03 PM)EdS2 Wrote: Anyone care to convert the following Basic code, or something close to this JavaScript code, to an HP-71B, or perhaps to a Free42 based calculator, or a Prime, or perhaps a Sharp Pocket Computer? Here's the code in ANSI BASIC: Code: 100 DECLARE EXTERNAL SUB digits Tom L Cui bono? |
|||
05-14-2024, 11:32 AM
Post: #10
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program | |||
05-14-2024, 01:45 PM
(This post was last modified: 05-14-2024 01:55 PM by toml_12953.)
Post: #11
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
Here's a translation of a FORTRAN program. It will give you as many digits as you want with no bignum needed. It runs in a small memory machine with no large array needed. Just increase the 50000 in the FOR statement to increase the number of digits. Be warned, though, it takes quite a while to calculate 200,000 digits!
Code: DIM vect(3350) Tom L Cui bono? |
|||
05-14-2024, 02:42 PM
(This post was last modified: 05-14-2024 02:44 PM by EdS2.)
Post: #12
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-13-2024 07:21 PM)toml_12953 Wrote: Here's the code in ANSI BASIC:Thanks - just converted back to BBC Basic and it checks out! (05-14-2024 01:45 PM)toml_12953 Wrote: Here's a translation of a FORTRAN program. It will give you as many digits as you want with no bignum needed. It runs in a small memory machine with no large array needed. Hmm - that's a Rabinowitz spigot, I think, and it uses an array for the base conversion. Granted, I expect it's much more memory efficient than the Gibbons spigot, but I haven't tried to measure that. (Bearing in mind, the point of my initial post here was to show an unbounded spigot, rather than the usual bounded spigot.) |
|||
05-17-2024, 04:58 PM
(This post was last modified: 05-17-2024 05:01 PM by John Keith.)
Post: #13
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-12-2024 08:57 PM)EdS2 Wrote: ... this on the other hand is Gibbons' unbounded spigot. Different, in some sense, except that we don't have bignums here. We do if we are using the HP 49/50 or Prime. The spigot programs use only integer math and thus do not require arbitrary-precision floating-point software, making them a good fit for the newer HP's. Here is a straightforward RPL implementation. The program stores the digits in a string for convenience but could be easily adapted to display each digit if desired. Given an integer s on the stack, the program will return approximately 0.3*s digits. This is because only about 1 in 4 iterations actually return a new digit. Code:
If we assume that the conjecture in section 6 of Gibbons' paper is correct, we can use the last program in the paper called 'piG3' which is much more efficient. Not only does it return a new digit for every iteration, it also has features that make the program run faster. It only requires one integer divide per digit, and those are slow in RPL. Also, since there is no test clause, the control structure is simpler and we can replace the WHILE loop with a FOR loop which is faster. Here, then, is an RPL implementation of 'piG3'. It runs about 7 times faster than the previous program and is about 100 bytes smaller. Given an integer on the stack, the program will return that many digits. Code:
|
|||
05-18-2024, 08:03 AM
(This post was last modified: 05-19-2024 02:39 PM by EdS2.)
Post: #14
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
Thanks John, that's splendid! I never had a clue how to rewrite Haskell into something I could understand.
I quite like a pi program which needs an unproved conjecture (but is known to be correct for at least a thousand digits (Edit: and I've since verified it to 100k digits) (Edit: and now to a million digits)) In BBC Basic: Code: BBC BASIC for MacOS Console v0.46 In Owlet (an 8 bit Basic with 5 byte floats) we get Code: 314159265358 How fast is a Prime, or an HP49 or HP50, in computing say 1000 digits of pi this way? |
|||
05-18-2024, 01:01 PM
Post: #15
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(12-12-2021 11:04 PM)Albert Chan Wrote: PI = 4/(1+1/(3+2^2/(5+3^2/(7+4^2/(9+5^2/(11 ... Translated for HP71B 10 P=1 @ Q=3 @ A=4 @ B=1 @ A1=12 @ B1=4 20 P=P+Q @ Q=Q+2 @ A0=A @ B0=B @ A=A1 @ B=B1 @ A1=P*A0+Q*A1 @ B1=P*B0+Q*B1 30 D=A DIV B @ R=A1-D*B1 @ IF R<0 OR R>=B1 THEN 20 40 DISP D; @ A=10*(A-D*B) @ A1=10*R @ GOTO 30 >run 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 5 6 2 ... 12 decimals float give 20 good pi digits. |
|||
05-18-2024, 01:28 PM
(This post was last modified: 05-18-2024 03:19 PM by Thomas Klemm.)
Post: #16
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-18-2024 08:03 AM)EdS2 Wrote: I never had a clue how to rewrite Haskell into something I could understand. Here's the Haskell program for Gosper's series rewritten in Python: Code: def main(n, k): It runs with MicroPython. Therefore, it should run on the Prime as well. (05-18-2024 08:03 AM)EdS2 Wrote: How fast is a Prime, or an HP49 or HP50, in computing say 1000 digits of pi this way? I don't have a Prime to test it, but it takes 0.053s on a MacBook M2 Pro: Code: 3 |
|||
05-18-2024, 01:55 PM
Post: #17
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-18-2024 08:03 AM)EdS2 Wrote: How fast is a Prime, or an HP49 or HP50, in computing say 1000 digits of pi this way? Not very fast on the HP 50. I haven't tested up to 1000 digits on a physical calculator but 302 digits (the number returned by Gibbons' original program with input of 1000) takes about 198 seconds. I timed 1000 digits on EMU48 but I'm not at home now and i don't remember the exact time. By way of comparison, the LongFloat function FPI is between 25 and 50 times as fast depending on the number of digits. Gibbons' paper does mention that spigot algorithms are not competitive with state-of-the-art conventional algorithms. |
|||
05-18-2024, 03:19 PM
(This post was last modified: 05-18-2024 03:21 PM by Guenter Schink.)
Post: #18
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
How fast is PRIME?
I copied Thomas' Python code to the Prime. Some adjustments for timing: Code: from hpprime import * output is the same as Thomas' time is between 5 and 6.5 seconds with several runs Günter |
|||
05-18-2024, 03:35 PM
Post: #19
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-18-2024 03:19 PM)Guenter Schink Wrote: How fast is PRIME? Not sure if it helps, but I remembered Writing fast and efficient MicroPython and made the functions local to main: Code: def main(n, k): |
|||
05-19-2024, 03:47 AM
(This post was last modified: 05-19-2024 07:16 AM by komame.)
Post: #20
|
|||
|
|||
RE: Pi digits (again) - an unbounded spigot program
(05-18-2024 03:19 PM)Guenter Schink Wrote: output is the same as Thomas' If performance is important, this code can be optimized by buffering the result in a variable and then outputting it to the terminal all at once, instead of outputting partial results multiple times. Code: from hpprime import * This reduces the execution time of this task on the HP Prime to below 1 second (significant acceleration). Piotr Kowalewski |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)