Gerson's Pi Program
|
12-28-2020, 11:20 AM
Post: #1
|
|||
|
|||
Gerson's Pi Program
Did anyone else have a go at de-obfuscating and running Gerson's short pi-calculating program from the festive greetings thread?
(12-25-2020 11:45 PM)Gerson W. Barbosa Wrote: I'd be interested to know how it works! I made a plain copy in BBC Basic, which you can run in your browser here. And here it is below anti-spoiler scroll filler: Code:
|
|||
12-28-2020, 01:15 PM
(This post was last modified: 12-28-2020 01:15 PM by Allen.)
Post: #2
|
|||
|
|||
RE: Gerson's Pi Program
Quote:I'd be interested to know how it works! Looks like a nicely compact convergence based on Wallis' Product 17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-29-2020, 01:59 AM
Post: #3
|
|||
|
|||
RE: Gerson's Pi Program
(12-28-2020 11:20 AM)EdS2 Wrote: I'd be interested to know how it works! Hello, Ed! Thank you very much for your interest! That’s an MSX BASIC listing. The MSX computer was very popular in Brazil and England in the mid 80’s (but not in the USA). That was my second computer. I liked its 14 significant digits, much better than my first computer, an Apple II clone. The program is based on this formula: \(\pi \approx \left ( \frac{4}{3} \times \frac{16}{15}\times \frac{36}{35}\times\frac{64}{63} \times \cdots \times \frac{ 4n ^{2}}{ 4n ^{2}-1}\right ) \left ( 2+\frac{4}{8n+3+\frac{3}{8n+4+\frac{15}{8n+4+ \frac{35}{8n+4 + \frac{63}{\dots\frac{\ddots }{8n+4+\frac{4n^{2}-1}{8n+4}}} }}} } \right )\) That’s the third Wallis-Wasicki formula, as I called it, rather jokingly. Notice the reuse of the denominators in the Wallis product as the numerators in the continued fraction. Here’s the thread in which I presented it, in case you missed it: https://www.hpmuseum.org/forum/post-1394...#pid139434 Best regards, Gerson. |
|||
12-29-2020, 09:15 AM
Post: #4
|
|||
|
|||
RE: Gerson's Pi Program
Thanks! I will surely have seen that post before, but evidently failed to recognise the approach. I think I might blame my ever-increasing age.
|
|||
12-29-2020, 12:53 PM
(This post was last modified: 12-29-2020 12:53 PM by Gerson W. Barbosa.)
Post: #5
|
|||
|
|||
RE: Gerson's Pi Program
(12-29-2020 09:15 AM)EdS2 Wrote: Thanks! I will surely have seen that post before, but evidently failed to recognise the approach. I think I might blame my ever-increasing age. My pleasure! Time goes forward for us all. I have to update my avatar so that it reflects my current age, but I don't remember how I did that twenty years ago. Actually, this is the post where I first presented that formula, along with an explanation how I found it (not a proof, though). https://www.hpmuseum.org/forum/post-1345...#pid134502 It appears to produce 25*n/12 correct digits (I have tested the algorithm to 1000 digits only). The previous program is optimized for speed as it avoids two extra multiplications per iteration. For the few digits provided by most computer languages, it should be better to use this even more compact program: Code:
|
|||
02-28-2022, 09:59 PM
(This post was last modified: 03-10-2022 08:39 PM by Gerson W. Barbosa.)
Post: #6
|
|||
|
|||
RE: Gerson's Pi Program
Here is an arbitrary precision version for the MSX computer. It shouldn't be difficult to convert it to the HP-BASIC (HP-75C and HP-71B), but the base should be lowered to 1E6 or 1E5.
Code:
Because full-precision multiplications and inversions are required in the main loop, this is not meant to be fast. It took eight minutes and eleven seconds on an emulator running ten times as fast compared to the real machine for just the first 100 digits. The program may return a few extra digits, but only the ones you ask for can be trusted. Number of decimal digits: 100 3 1415926 5358979 3238462 6433832 7950288 4197169 3993751 582097 4944592 3078164 628620 8998628 348253 4211706 7982103 512.45s Edited to fix a typo in line 720 and to update the new timings. |
|||
03-01-2022, 09:39 AM
(This post was last modified: 03-12-2022 09:13 AM by EdS2.)
Post: #7
|
|||
|
|||
RE: Gerson's Pi Program
Very nice! I'd like to port it to BBC Basic, where we have 4 byte integers and 5 byte floats. But it's not working... any ideas?
Minor notes: The DEFINT are ignored, because in BBC Basic we need to decorate integer variables with % suffix. I was hoping that would merely be a speed advantage as the 5 byte floats are very nearly as good as the 4 byte ints for accuracy and range. I took the ! off the end of the B=100000000 but I don't know what it means. I changed the \ operators to DIV, guessing that this is what's meant. I changed the printing to print an integer rounded version of the number. Here's the link. (Edit: typo for 4 byte ints) |
|||
03-01-2022, 11:41 AM
(This post was last modified: 03-01-2022 04:12 PM by Gerson W. Barbosa.)
Post: #8
|
|||
|
|||
RE: Gerson's Pi Program
(03-01-2022 09:39 AM)EdS2 Wrote: Very nice! I'd like to port it to BBC Basic, where we have 4 byte integers and 5 byte floats. But it's not working... any ideas? B stands for base. Default variable types in MSX are double precision (8 bytes, 14 significant digits), which allows for bases as high as 10000000. With 9-digit precision, you should choose base = 10000 or perhaps 100000. The \ operator is the integer division, as you have correctly guessed. The exclamation sign says it's a single precision number. Try changing lines 80 and 440: 80 B = 10000: N=ND DIV 4 + 2: K=ND*12 DIV 25 + 1 440 M=10*LOG(ND)/LOG(1000) For 30 digits the output should be 3 1415 9265 3589 7932 3846 2643 3832 7941 P.S.: For a much faster program, you might want to try Katie Wasserman's program for the HP-71B: CALCULATING MANY DIGITS OF PI |
|||
03-02-2022, 11:17 AM
Post: #9
|
|||
|
|||
RE: Gerson's Pi Program
Hmm, thanks for the suggested edits, but unfortunately something isn't quite right...
12 digits gives 3 1416 448 2514 8212 (Thanks also the link back to the earlier thread. I'll see about porting that too.) (Sorry my link to owlet is not working well: something about url-escaping. My posted text works, the rendered link doesn't. If you open a draft which quotes my post you'll get the good link text.) |
|||
03-03-2022, 03:01 AM
Post: #10
|
|||
|
|||
RE: Gerson's Pi Program
(03-02-2022 11:17 AM)EdS2 Wrote: Hmm, thanks for the suggested edits, but unfortunately something isn't quite right... I have a working version for the BeebEm emulator, but I don't know how to get a listing. Anyway, basically you should delete line 40 and use MOD instead: 520 T=(B-BR%-C(I)) MOD B 890 W(I)=INT(X/G): Y=X MOD G 960 W(I)=X MOD B: Y=INT(X/B) 1030 C(I)=X MOD B: Y=INT(X/B) Also, lines 30, 740 and 740 should be changed to 30 DEF FNFRAC(X) = X+5E-6 - INT(X+5E-6) 740 AC=INT(B*FNFRAC(CT)) 810 C(I)=INT(B*FNFRAC(T)) I've used only real variables, including the one in line 520, but you can change the types of the variables accordingly for speed and memory saving. On the MSX I cannot define AC and C() to integer, because its integer type is only 2-byte long. For the same reason MOD cannot be used on the MSX for bases greater than 10000. For 12 digits you should get 3 1415 9265 3589 8142 |
|||
03-04-2022, 04:50 PM
(This post was last modified: 03-04-2022 04:51 PM by EdS2.)
Post: #11
|
|||
|
|||
RE: Gerson's Pi Program
Thanks! With your changes, my suboptimal port produces the 12 correct digits as it should, in 53 seconds.
For reference, my simplistic and inefficient port of Katie's program produced 92 correct digits in 71 seconds. |
|||
03-06-2022, 01:25 AM
Post: #12
|
|||
|
|||
RE: Gerson's Pi Program
(03-04-2022 04:50 PM)EdS2 Wrote: With your changes, my suboptimal port produces the 12 correct digits as it should, in 53 seconds. As stated earlier, the WWF is not intended to be fast. According to an article by the Borwein Brothers in Scientific American (February 1988), not even 100 years of computing on a supercomputer programmed to evaluate the Wallis Product or the Gregory’s Series would yield 100 digits of π. So, getting 100 digits in 100 minutes on an 8-bit personal computer of that era using the Wallis Product as a basis is an unexpected surprise. |
|||
03-08-2022, 05:17 PM
(This post was last modified: 03-08-2022 05:19 PM by Gerson W. Barbosa.)
Post: #13
|
|||
|
|||
RE: Gerson's Pi Program
(03-04-2022 04:50 PM)EdS2 Wrote: Thanks! With your changes, my suboptimal port produces the 12 correct digits as it should, in 53 seconds. When porting the program to the HP-75C I noticed an error in line 720. 720 FOR J=N-1+2 TO -1 STEP -1 The first 1 should be an I: 720 FOR J=N-I+2 TO -1 STEP -1 Please correct it if you haven’t done so yet. This should make the program a little less slow. I am away from home and have brought only the HP-75C along. Also, I can’t make your links work. Sorry for the mistake! |
|||
03-08-2022, 05:42 PM
Post: #14
|
|||
|
|||
RE: Gerson's Pi Program
.
Hi, Gerson, (03-06-2022 01:25 AM)Gerson W. Barbosa Wrote: According to an article by the Borwein Brothers in Scientific American (February 1988), not even 100 years of computing on a supercomputer programmed to evaluate the Wallis Product or the Gregory’s Series would yield 100 digits of π. Perhaps, but then again there's this: Speeding it up ! Regards. V. . . All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
03-09-2022, 01:40 AM
Post: #15
|
|||
|
|||
RE: Gerson's Pi Program
(03-08-2022 05:42 PM)Valentin Albillo Wrote:(03-06-2022 01:25 AM)Gerson W. Barbosa Wrote: According to an article by the Borwein Brothers in Scientific American (February 1988), not even 100 years of computing on a supercomputer programmed to evaluate the Wallis Product or the Gregory’s Series would yield 100 digits of π. Hello, Valentin, Yes, I remember that particular HP-15C Mini-challenge of yours. It was linked by Thomas Klemm in the beginning of a thread I started. As the thread progressed, I ended up with what could have been another solution to your challenge, which I didn’t participate in: https://www.hpmuseum.org/forum/post-9194.html#pid9194 This is a correction term to the Gregory’s Series in continued-fraction form, similar to the correction factor to the Wallis Product. Best regards, Gerson. P.S.: This is a closer quotation of the Scientific American Article: Quote:WALLIS PRODUCT (1665) |
|||
03-09-2022, 08:12 AM
(This post was last modified: 03-09-2022 08:14 AM by Ángel Martin.)
Post: #16
|
|||
|
|||
RE: Gerson's Pi Program
Hi Gerson (and All),
Obviously pi continues to be a top-subject on the forum, with fascinating contributions being added every few months - and that happening for years already. Your latest ones have made me decide to put together a PI/E ROM as an attempt to gather in a common place (i.e. an HP-41 Module) some of the material the forum members have been producing. Since my math prowess is limited and won't allow for more substantial contributions here, this ROM is my tribute to the forum activity on the pi subject. Obviously the selection is biased by my own preferences, but hopefully I didn't miss the more original or intriguing ones. Besides your latest articles, the sources used obviously include Valentín's wondrous challenges and frequent articles, a treasure trove on the subject. The hyperlinks and forum archives have also been very helpful to compile the material, of course. To add some value to the project I've made MCODE versions of some algorithms, making them faster and slightly more accurate (due to the 13-digit routines) - though this is a negligible feature in the transcendental pi-world... Anyway, be as limited as it may, the ROM should be ready in a few more days - ideally on the 14th. but no assurances are made ;-) Thanks for the contributions, a lot of fun came from working on it. Best, ÁM "To live or die by your own sword one must first learn to wield it aptly." |
|||
03-10-2022, 07:47 AM
Post: #17
|
|||
|
|||
RE: Gerson's Pi Program
(03-08-2022 05:17 PM)Gerson W. Barbosa Wrote: Please correct it if you haven’t done so yet. This should make the program a little less slow.Thanks! There is indeed a speedup. I'm now going through to change to integer variables to see what difference that can make. Quote:Also, I can’t make your links work.Sorry about that - I will publish some updated ones, perhaps on the 14th! (03-09-2022 08:12 AM)Ángel Martin Wrote: Hi Gerson (and All), Excellent news! (I hope you find a spigot approach to include: such a thing will produce digits successively from left to right.) (03-08-2022 05:42 PM)Valentin Albillo Wrote: ... there's this:Thanks for the link, Valentin, and again to Gerson for all the ideas and links. |
|||
03-10-2022, 11:23 AM
Post: #18
|
|||
|
|||
RE: Gerson's Pi Program
(03-10-2022 07:47 AM)EdS2 Wrote:(03-08-2022 05:17 PM)Gerson W. Barbosa Wrote: Please correct it if you haven’t done so yet. This should make the program a little less slow.Thanks! There is indeed a speedup. I'm now going through to change to integer variables to see what difference that can make. Thank you! I’ve updated the listing (will update the timings later). Times on the HP-75C: 100 digits: 8083.154 s ( ~ 135 min ) 152 digits: 24386.212 s ( ~ 406 min ) |
|||
03-10-2022, 11:48 AM
Post: #19
|
|||
|
|||
RE: Gerson's Pi Program
.
Hi, EdS2, EdS2 Wrote:Excellent news! (I hope you find a spigot approach to include: such a thing will produce digits successively from left to right.) You're welcome. I know you like spigot algorithms for \(\pi\), for instance you ported my multiprecision BASIC spigot program for a SHARP pocket computer to the BBC micro, which ran flawlesly (and faster when using integer variables throughout), while I also ported it to the HP-71B, which could produce many thousands of digits (one at a time, mind you) if you had enough RAM and time. Curiously enough, using INTEGER variables on the 71B results in a slower program than using REAL variables. As for what Ángel might include in his forthcoming PI/E ROM, who knows ... Best regards. V. All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
03-11-2022, 12:42 PM
(This post was last modified: 03-11-2022 01:38 PM by EdS2.)
Post: #20
|
|||
|
|||
RE: Gerson's Pi Program
Indeed I do like a pi program, and a spigot program, and anything multiprecision.
About porting to BBC Basic - in the case of your spigot, a very naive initial port would produce 50 digits in 118 seconds. With a change to integer variables, it becomes 66 seconds, and then by changing / to DIV, it becomes 54 seconds. We can even save another second by setting D% to 10 and using that instead of the literal in a couple of places. So by recoding in the fastest idiom for BBC Basic, we get a 2x speedup. (The above timings are for Basic 2 on a 2MHz 6502 - later Basics were faster, and of course CPU clock speeds also improved.) Other changes which can be beneficial: - using only single-letter variables, especially A% to Z% which are special-cased - removing the loop variable from NEXT - using FOR, REPEAT, PROC and FN instead of line numbers - inlining functions and subroutines - removing blank spaces and consolidating multiple lines Of course, some of these re-codings make the program a bit less clear, and one gets diminishing returns. It's a pity that all the % signs for integer variables are a bit ugly especially if you're not used to them: BBC Basic has no DEF INT. In the case of Gerson's program, the version below produces 12 digits in 14 seconds, compared to 43 seconds for the initial port. This version produces 36 digits in 186s. (Edit: and 100 digits in 2510s.) I was quite pleased to do without both FN definitions and especially the 5E-6 which I didn't understand or feel comfortable with. I could use DIV and MOD in a natural way. Code:
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)