Musings and comments about our common interest
I have now noticed that my practice programming in any other language than RPN is lost. The Prime has a myriad of loop types, and I was lost trying to find the one that would suit better to this small program to calculate pi through Pythagoras Theorem for the Prime. Also I was not able to find the right means to stop the program for as long as needed, without resorting to ugly methods. But well, the program does what it intends to do, while showing all the values at each iteration.
LOCAL LADO, POLY, C, R;
FOR I FROM 2 TO 25 DO
I'm sure you can make it much better - can you help me?
Last week we put forward a quiz: how to calculate Pi with a calculator. The initial proposition was to prepare a program for HP Prime. I gave as alternative the possibility to create it for other HP calculator; how can it be more appropriated than the HP41c family? As it has been a long time since I last used some modules, I preferred to use only standard operations - not the solve application of the Advantage pack.
As stated, I used the polygon circumscribed in the circumference, doubling each time the number of sides. This method will approach pi “from below”, being its limit when the number of iterations tend to infinite. An alternative solution, that we’ll use in a further installment, will use the “outside” polygon, which will approach the solution “from above”.
The program first find the first polygon side (a square) through a rough Pythagoras’ theorem: given r = ½ of diameter (d), then the side is l12 = r2+r2 = 2r2 => l1 = r*sqrt(2),
From there, if you see the picture, the next polygon side can be calculated as the hypothenuse of a triangle with a side that is half of the previous side, and the other, the difference between the radius and the side of another triangle, this one with the previous half-side and the radius as hypothenuse. This is easier than it sounds:
note that (li/2)^2 appears twice - the only clever thing of this program will be to calculate it only once per iteration, and use the stack to keep the other copy. Well, another one-key saving is based on the fact that the square of (li/2)^2-r2 (what we can calculate without exchanging x and y) is equal to the square of r2-(li/2)2.
The program for HP41c is besides (please notice the beautiful printout generated in my iPad's i41cx+ program). I have ointroduced a stop at the beginning of each loop, so that you can see on screen the value at each iteration. From iteration 15, you're only adding rounding errors: the sucession crosses the value of pi and actually diverges! :
Do you dare to attack the “outside polygon” approach?
I have been reading recently a novel about Pythagoras: “Pythagoras killing”, from Marcos Chicot. It depicts a thriller in Pythagoras’ time, where some of the mathematical themes of that era were discussed.
At that time, Pi was not known among the greek. They knew that the relation between the circumference and its diameter was slightly greater than three, but they did not know how to calculate it - even less to understand that it was an irrational number. Moreover, the Pythagorean school thought that all magnitudes in nature came from exact ratios between integer numbers.
In the story, there is a rich man versed in maths that offers a huge amount of gold to whom is able to tell him the ratio between the diameter and the circumference with 4 decimals. The villain (that happens to be a former Pythagoras disciple) wins it with a method based (to add injury to Pithagoras) on the Pythagorean theorem. In principle, these were the only available means to mathematicians of that era to calculate PI - none of the modern methods were available.
A light clicked and I set to calculate PI as well as I could with my Prime. I started doing it with this small graph, and then checked it with a spreadsheet. It is based on calculated more and more sides of the circle, based on the previously calculated side. Good! with 25 iterations, it reaches the 15 decimals available in Excel - the iteration 26 shows 0 difference with Pi.
Will any of you be able to program it with the HP Prime? If you don’t feel comfortable with it, you can try with any other HP calculator, either in RPN or RPL
This Friday I will show the solution. You have 3 days to solve it!