HP Forums
Can you calculate Pi using a Solver? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Can you calculate Pi using a Solver? (/thread-14132.html)

Pages: 1 2 3


Can you calculate Pi using a Solver? - EdS2 - 12-09-2019 01:12 PM

I'm wondering what approaches there might be for computing Pi using one of the Solvers on the various HP calculators. Of course, no trig functions allowed!

Let's call this a mini-challenge, unless it turns out to be so easy and obvious that it's a nano-challenge.


RE: Can you calculate Pi using a Solver? - SlideRule - 12-09-2019 01:44 PM

Reference:
Using A Minicalculator to Find An Approximate Value for Π, E. J. Bolduc (University of Florida)

"One of the many ways to use a minicalculator in a classroom is in the calculation of an approximate value for n using a variation of the method of Archimedes … if a circle of radius 1 is chosen, then the area of any inscribed polygon is less than Π and the area of any circumscribed polygon is greater than n. He finally arrived at the fact that 3(10/17) < Π < 3(1/7). We can use the idea that, as the numbers of sides of an inscribed polygon increases, the perimeter of the polygon approaches the circumference of the circle and the ratio of the perimeter to the diameter of the circle is an approximation for Π
We now have our iterative formula, S’ = √2r² - r√4r² - S²"

I leave the recursion and subsequent calculation to the reader, but after nine iterations, the equation yields 3.141592…

BEST!
SlideRule


RE: Can you calculate Pi using a Solver? - jthole - 12-09-2019 01:50 PM

This does not answer your question directly, but on the back of my 12C I have a sticker with "355 / 113".

For my purposes, that is good enough Smile


RE: Can you calculate Pi using a Solver? - toml_12953 - 12-09-2019 01:56 PM

(12-09-2019 01:44 PM)SlideRule Wrote:  Reference:
Using A Minicalculator to Find An Approximate Value for Π, E. J. Bolduc (University of Florida)

"One of the many ways to use a minicalculator in a classroom is in the calculation of an approximate value for n using a variation of the method of Archimedes … if a circle of radius 1 is chosen, then the area of any inscribed polygon is less than Π and the area of any circumscribed polygon is greater than n. He finally arrived at the fact that 3(10/17) < Π < 3(1/7). We can use the idea that, as the numbers of sides of an inscribed polygon increases, the perimeter of the polygon approaches the circumference of the circle and the ratio of the perimeter to the diameter of the circle is an approximation for Π
We now have our iterative formula, S’ = √2r² - r√4r² - S²"

I leave the recursion and subsequent calculation to the reader, but after nine iterations, the equation yields 3.141592…

BEST!
SlideRule

Here's a simple BASIC version from Problems for Computer Solution by Stephen J. Rogowski:
Code:
50 PRINT "ARCHIMEDEAN DETERMINATION OF PI!"
60 PRINT
70 PRINT "NO. OF SIDES","INSCR PER","CIRCUM PER"
80 PRINT
100 FOR X=2 TO 15
105   LET N=2^X
110   LET D=360/N
120   LET T=3.1415927*(D/180)
130   LET A=2*N*SIN(T/2)
140   LET B=2*N*TAN(T/2)
150   PRINT N,A/2,B/2
155   IF A=B THEN GOTO 170
160 NEXT X
170 END



RE: Can you calculate Pi using a Solver? - Valentin Albillo - 12-09-2019 03:35 PM

(12-09-2019 01:12 PM)EdS2 Wrote:  I'm wondering what approaches there might be for computing Pi using one of the Solvers on the various HP calculators. Of course, no trig functions allowed!

Let's call this a mini-challenge, unless it turns out to be so easy and obvious that it's a nano-challenge.

If your Solver has Square Root and Factorial, this one from Ramanujan converges very fast and it's quite simple, few bytes to program it and only a few terms to add up for full precision...

[Image: 0993bfb34b91c895c4a55899e71b875dc8d4dd3b]

... and no trigs used, as requested, just basic arithmetic. Once you get 1/Pi just invert it with 1/x

Regards.
V.


RE: Can you calculate Pi using a Solver? - Csaba Tizedes - 12-09-2019 03:53 PM

Code:
120   LET T=    3 . 1 4 1 5 9 2 7      *(D/180)

WHAT?

Code:
120   LET T=3.1415927*(D/180)
130   LET A=2*N*SIN(T/2)
140   LET B=2*N*TAN(T/2)

155   IF A=B THEN GOTO 170

170 END

Err... this looks like a checking how precise the arithmetics of that computer...

BTW: HP32SII - a fraction finder

Code:
---------------------
LBL Q
  RCL P
  RCL / Q
  RCL - K
  STO D        //D=P/Q-K
  ABS
  RCL E
  X>=Y?        //E>=ABS(D)?
RTN            //Yes, end of the program
  RCL D        
  X<0?         //P/Q<K ?
  GTO P        //Yes, increase P
  1            // No, increase Q
  STO + Q
  GTO Q        
---------------------
LBL P          // Increase P
  1
  STO + P
  GTO Q
---------------------

Checksums:
==========
LBL Q: CK=9676 / 22.5 byte
LBL P: CK=C1C6 / 6.0 byte

Variables:
==========
P, Q, K, E, D

Timing:
=======
| Number (K) | Error (E) | P    | Q   | Number of steps (P-1+Q-1) | Running time | Steps/secs |
| PI         | 1E-5      |  355 | 113 |  466                      |   41.5s      | 11.2/s     |
| SQRT(2)    | 1E-5      |  577 | 408 |  983                      |   86.1s      | 11.4/s     |
| e          | 1E-5      | 1071 | 394 | 1463                      |  129.7s      | 11.3/s     |

Csaba


RE: Can you calculate Pi using a Solver? - jthole - 12-09-2019 06:38 PM

(12-09-2019 03:35 PM)Valentin Albillo Wrote:  If your Solver has Square Root and Factorial, this one from Ramanujan converges very fast and it's quite simple, few bytes to program it and only a few terms to add up for full precision...

[Image: 0993bfb34b91c895c4a55899e71b875dc8d4dd3b]

... and no trigs used, as requested, just basic arithmetic. Once you get 1/Pi just invert it with 1/x

Regards.
V.

Very nice! Here is the equation for the 17Bii:

1 ÷ PIVAL = ( ( 2 x SQRT( 2 ) ) ÷ 9801 ) x sigma ( I : 0 : N : 1 : ( FACT( 4 x I ) x ( 1103 + 26390 x I ) ÷ ( ( FACT( I ) ^ 4 ) x ( 396 ^ ( 4 x I ) ) ) )

I only tested it on the iPhone emulator, but it goes to full precision very quickly (N=2) indeed.


RE: Can you calculate Pi using a Solver? - mfleming - 12-10-2019 02:48 AM

Needs one more closed parenthesis at the end. Works on the 27S!


RE: Can you calculate Pi using a Solver? - JurgenRo - 12-10-2019 07:45 PM

(12-09-2019 03:35 PM)Valentin Albillo Wrote:  
(12-09-2019 01:12 PM)EdS2 Wrote:  I'm wondering what approaches there might be for computing Pi using one of the Solvers on the various HP calculators. Of course, no trig functions allowed!

Let's call this a mini-challenge, unless it turns out to be so easy and obvious that it's a nano-challenge.

If your Solver has Square Root and Factorial, this one from Ramanujan converges very fast and it's quite simple, few bytes to program it and only a few terms to add up for full precision...

[Image: 0993bfb34b91c895c4a55899e71b875dc8d4dd3b]

... and no trigs used, as requested, just basic arithmetic. Once you get 1/Pi just invert it with 1/x

Regards.
V.

(Pi^2)/6=sum_{n=1}^{infinity}1/n^2 might fall into the same category.
Juergen


RE: Can you calculate Pi using a Solver? - jthole - 12-10-2019 09:07 PM

(12-10-2019 07:45 PM)JurgenRo Wrote:  
(12-09-2019 03:35 PM)Valentin Albillo Wrote:  If your Solver has Square Root and Factorial, this one from Ramanujan converges very fast and it's quite simple, few bytes to program it and only a few terms to add up for full precision...

[Image: 0993bfb34b91c895c4a55899e71b875dc8d4dd3b]

... and no trigs used, as requested, just basic arithmetic. Once you get 1/Pi just invert it with 1/x

Regards.
V.

(Pi^2)/6=sum_{n=1}^{infinity}1/n^2 might fall into the same category.
Juergen

Converges much more slowly though. I just let it run from n=0 to 1 million (on the 17Bii simulator on my iPhone), and the difference with the Pi constant is still -9,5x10E-7. In contrast, the one suggested by Valentin already equals the Pi constant for n=2.

I'll test it on the 17Bii later, to see how much slower than the iPhone 7 it is Smile

Edit: in order not to pollute this thread, I posted the results here: https://www.hpmuseum.org/forum/thread-14101.html


RE: Can you calculate Pi using a Solver? - Albert Chan - 12-10-2019 09:35 PM

Pi via AGM, Emu71 HP-71B basic

Code:
10 A=1 @ B=SQRT(.5) @ S=1 @ P=1
20 C=A-B @ B=SQRT(A*B) @ A=A-C/2 @ S=S-P*C*C @ P=P+P
30 DISP A*B/(.25*S)
40 IF A<>B THEN 20

>RUN
3.14040401902
3.1415926456
3.1415926536
3.14159265359


Comment: Apr 14, 2023

Above code started with S=1, and lowered it bit by bit.
It may be more accurate to avoid this cancellation errors.
Just click the green arrow for Free42 AGM code.

(06-20-2020 04:23 PM)Albert Chan Wrote:  To improve agm2 accuracy, I redefined agm2 returns:

x, y = agm2(a, b)
→ x = converged GM of agm(a, b)
→ y = -Σ(2k (½gapk)² , k = 1 .. n), n = number of iterations to converge GM

With this new setup, ellipse_perimeter(a,b) = 4 a E(1-(b/a)²) = pi (y + b² + a²)/x

With above setup, x, y = agm2(1, sqrt(0.5)) --> S = 1+2*y --> pi = 2*x^2 / (y+1/2)

.5 SQRT 1 XEQ "AGM"

X = 8.472130847939790866064991234821916e-1
Y = -4.305341895553637462503337745231669e-2

X^2 2 * SWAP .5 + /

3.141592653589793238462643383279503

Or, less keystrokes, x, y = agm2(1, sqrt(2)) --> pi = 2*x^2 / (y+1)


RE: Can you calculate Pi using a Solver? - EdS2 - 12-11-2019 10:29 AM

Ah, always interesting to get some unexpected results - thanks everyone!

In some, then, we see a 'sigma' function being used to do most of the work, and the solver is only needing to cope with a simple transformation like reciprocal or square root. I hadn't realised a 'sigma' function might be to hand - of course these offerings are valid, for the machines which offer 'sigma'.

For the first submission, the iterative one posted by sliderule, is there a way to render this as a Solver problem?
Quote:We now have our iterative formula, S’ = √2r² - r√4r² - S²

Likewise, for the AGM method posted by Albert Chan, is there a way to get a Solver to do the work, rather than a program?


RE: Can you calculate Pi using a Solver? - toml_12953 - 12-11-2019 11:50 AM

(12-09-2019 03:53 PM)Csaba Tizedes Wrote:  
Code:
120   LET T=    3 . 1 4 1 5 9 2 7      *(D/180)

WHAT?

Code:
120   LET T=3.1415927*(D/180)
130   LET A=2*N*SIN(T/2)
140   LET B=2*N*TAN(T/2)

155   IF A=B THEN GOTO 170

170 END

Err... this looks like a checking how precise the arithmetics of that computer...

It shows that both an inscribed and circumscribed polygon approach PI as the number of sides increases. I agree it was poorly named.


RE: Can you calculate Pi using a Solver? - Albert Chan - 12-11-2019 01:09 PM

(12-11-2019 11:50 AM)toml_12953 Wrote:  It shows that both an inscribed and circumscribed polygon approach PI as the number of sides increases.

We can do this with right triangles, starting from a hexagon ("radius" = side = 2S = 1)

10 N=6 @ S=.5
20 H=SQRT(1-S*S) @ A=N*S @ B=A/H
30 DISP N,A,B
40 N=N+N @ S=.5*SQRT(S^2+(1-H)^2)
50 IF A<B THEN 20

Code:
>RUN
 6                    3                    3.46410161514
 12                   3.10582854122        3.21539030916
 24                   3.13262861328        3.1596599421
 48                   3.13935020304        3.14608621512
 96                   3.14103195089        3.14271459965
 192                  3.14145247229        3.14187304999
 384                  3.14155760792        3.14166274706
 768                  3.14158389215        3.1416101766
 1536                 3.14159046322        3.14159703431
 3072                 3.141592106          3.14159374877
 6144                 3.1415925167         3.14159292739
 12288                3.14159261938        3.14159272205
 24576                3.14159264504        3.14159267071
 49152                3.14159265146        3.14159265788
 98304                3.14159265306        3.14159265467
 196608               3.14159265346        3.14159265386
 393216               3.14159265356        3.14159265366
 786432               3.14159265357        3.1415926536
 1572864              3.14159265359        3.1415926536
 3145728              3.14159265359        3.14159265359

Edit: line 40 may be replaced with simpler formula (approx. same accuracy)
40 N=N+N @ S=S/SQRT(H+H+2)


RE: Can you calculate Pi using a Solver? - Roland57 - 12-11-2019 05:49 PM

Hi,

May be it is too easy, but what about this

pi = 2 * integral(1 / sqr(1-x*x), x, 0, 1)

It gives 3.141592(48348) on the iOS prime-emulator

Roland


RE: Can you calculate Pi using a Solver? - Albert Chan - 12-11-2019 08:00 PM

(12-11-2019 05:49 PM)Roland57 Wrote:  pi = 2 * integral(1 / sqr(1-x*x), x, 0, 1)

It gives 3.141592(48348) on the iOS prime-emulator

Some calculators have problem with singularity at the limits.
We can do this instead:

pi = 6 asin(.5) = integrate(6 / sqrt(1-x*x), x = 0 .. 0.5)

or, to speed it up a bit ...

pi = 4 atan(1) = integrate(4 / (1+x*x), 0, 1)


RE: Can you calculate Pi using a Solver? - EdS2 - 12-12-2019 08:18 AM

ah yes, like summation, integration certainly has ways to reach pi.

I'm still curious about the solver though. If any HP calculator's solver worked in the complex domain (and I imagine none do) then Euler's identity would give a route to pi.

(By the solver, I mean the facility which takes a function of one variable and finds a value of that variable which makes the function zero.)

As we know that pi isn't algebraic, it might be that we know there's no way to do this. But finding a fixed point of a recurrence feels a bit like finding a zero of a function, so I wonder if that could be a way.


RE: Can you calculate Pi using a Solver? - EdS2 - 12-12-2019 11:24 AM

I found a couple of not-great examples of the sort of thing I might hope exists...

There's a good approximation for pi as a root of an 8th order polynomial:

0=1−1635840576x−343853312x^2 +60576043008x^3 +1865242664960x^4 −16779556159488x^5 +37529045696512x^6 −29726424956928x^7 +6181548457984x^8

And there's a bad approximation as a solution to
x=(1+1/x)^(x+1)

Here's a quartic:
0=16-160x+168600x^2-841000x^3+250625x^4


RE: Can you calculate Pi using a Solver? - SlideRule - 12-12-2019 02:12 PM

(12-12-2019 11:24 AM)EdS2 Wrote:  I found a couple of not-great examples of the sort of thing I might hope exists...

An excerpt from PI a source book, Springer (2e), 96-40196, © 2000
[attachment=7919]

The PDF title says it all.

BEST!
SlideRule


RE: Can you calculate Pi using a Solver? - EdS2 - 12-12-2019 03:02 PM

Thanks! Also of interest (and relevant) is The Quest for Pi, a 16 page paper by BBP.

(I'm still not sure how to get the solver to make use of this kind of iterative approach. Merely evaluating an expression doesn't feel like it's making proper use of a solver.)