Post Reply 
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
   20 REPEAT
   30   IF (q * 4 + r - t < n * t) ELSE GOTO 90
   40   PRINT ;n;
   50   nr = (r - n * t) * 10
   60   n = INT((q * 3 + r) * 10 / t) - n * 10
   70   q = q * 10: r = nr
   80 UNTIL FALSE
   90 nr = (q * 2 + r) * l
  100 nn = INT((q * k * 7 + 2 + r * l)/(t * l))
  110 q = q * k: t = t * l
  120 l = l + 2: k = k + 1
  130 n = nn: r = nr
  140 UNTIL FALSE

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.)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Pi digits (again) - an unbounded spigot program - EdS2 - 05-12-2024 05:03 PM



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