It's not a secret anymore!*
The moose likes Programming Diversions and the fly likes Finding the value of PI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Finding the value of PI" Watch "Finding the value of PI" New topic
Author

Finding the value of PI

Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
Supercomputers are able to calculate the digits of Pi upto trillion digits.What are the various methods to calculate the value of PI?There are many methods described in this book.
Problem:Choose a square as large as possible and circle inscribed in it.Arrow is randomly hit.Either it hits inside or outside the circle.Ratio of area of square to circle is same as total hits to the hits inside/on the circle.so primary calculation gives the value of PI as 4*(total hits/total hits inside/on the circle)
Write a program to find the value of PI.
[ August 05, 2007: Message edited by: Arjunkumar Shastry ]

Namma Suvarna Karnataka
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Pi is 3.14144588
error is 4.6719484661825556E-5
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

Studying the Montecarlo method?
If I recall correctly, you need to multiply by 4 if you're using a 1/4 th of a circle to do the calculation ( wich makes things simpler). Anyway, you need to discuss how acurate is your random number generator.
On the other hand, you can always write a program who opens a http connection, queries google for the value of PI and parses the result ...
What is your goal?


Gabriel
Software Surgeon
Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
Originally posted by Gabriel Claramunt:
Studying the Montecarlo method?
If I recall correctly, you need to multiply by 4 if you're using a 1/4 th of a circle to do the calculation ( wich makes things simpler). Anyway, you need to discuss how acurate is your random number generator.
What is your goal?

Yes,thats Monte Carlo method.Goal is to find how accurately you generate the value of PI.The larger square you have and more points you select ,more accurate will be the value of PI.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Trivia:

355/113 is much better estimate than 22/7.

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
Write a program to find that ratio.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Anand Hariharan:
Trivia:

355/113 is much better estimate than 22/7.

- Anand


Trivia:
1570787692 / 500000000 is much better estimate than 355/113
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As a matter of fact, no it isn't. Was there a typo in there?


"I'm not back." - Bill Harding, Twister
Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
I was surprised to read in the book that Bible contains references to PI.
There are many formulas to find the value of PI.Most people used value of PI given by Archimedes.(who constructed inscribed and circumscribed 96-gon).(3 + 10/71) < PI < (3+ 1/7).
When we say value x of PI is better than y,which value of PI you are considering? The above one ?
[ August 07, 2007: Message edited by: Arjunkumar Shastry ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Jim Yingst:
As a matter of fact, no it isn't. Was there a typo in there?


Maybe, I'll run it again.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Oh well, but at least mine came from my own coded estimate...
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

Originally posted by Arjunkumar Shastry:

Yes,thats Monte Carlo method.Goal is to find how accurately you generate the value of PI.The larger square you have and more points you select ,more accurate will be the value of PI.


Not really, if I recall correctly, when you are dealing with floating numbers in a computer, the range 0 to 1 is as dense as the range 1 to infinite, so a square of size 1 is better than a bigger one (also, simplifies the calculations).
So, the accuracy of the result will depend on how good is your random number sequence. A poor random number generator will skew your result.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[DOM]: Oh well, but at least mine came from my own coded estimate...

Well, there's something to be said for that.

[Gabriel]: So, the accuracy of the result will depend on how good is your random number sequence. A poor random number generator will skew your result.

True, but for most reasonably acceptable generators, including java.util.Random, the accuracy will depend much more on how long you're willing to wait for a result. The error in any given run will be roughly proportional to 1/sqrt(N), so to get just one more digit of accuracy, you need to increase the run time by a factor of 100. Not a very efficient way to compute pi.

[Arjunkumar Shastry]: I was surprised to read in the book that Bible contains references to PI.

Sort of. Not by name, just an implicit approximation - which is approximately 3, but that's close enough since the numbers are rather obviously rounded off anyway.

[Arjunkumar Shastry]: When we say value x of PI is better than y,which value of PI you are considering? The above one ?

No, the real one. Yeah, I know that technically all decimal representations are approximations rather than the "true" value, but in practice in this day & age it's easy enough to just look up the value to more than enough digits for any practical application. In my case I just remember the first nine digits, and it's been ages since I had any need for more than that.

355/113 is nice because it only uses three digits in the numerator and three in the denominator, yet the result is accurate to seven digits. Of all the approximations you can make using fractions of 3-digit numbers, that just happens to be particularly accurate.
Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
Thanks.When I took math classes,I never thought of PI beyond 3.14 and its ratio of circumference to diameter .Now I really wonder how Euler arrived at this formula?
e^ipi = -1 where e is natural logarithm(which is again irrational number) and i is squre root of -1 and PI again is irrational!!
From the same book:
"Gentlemen, that is surely true, it is absolutely paradoxical; we cannot understand it, and we don't know what it means. But we have proved it, and therefore we know it must be the truth." �Benjamin Pierce, a Harvard mathematician, after proving Euler's equation, eiπ = �1, in a 19th-century lecture.

[ August 07, 2007: Message edited by: Arjunkumar Shastry ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Arjunkumar Shastry]: Now I really wonder how Euler arrived at this formula?

e^ipi = -1


Well, Euler was the one who discovered the Taylor series for the exponential function. I'm not sure who found the Taylor series for sine and cosine, but once you know those and compare them to the exponential series, it's clear they are similar. The concept of imaginary numbers was also around prior to Euler. It doesn't take that much genius to see that plugging imaginary numbers into Euler's exponential power series gives power series for sine and cosine. I'm not saying Euler wasn't a genius - he certainly was - but this particular step seems easy enough in hindsight. More remarkable is the process of figuring out but what does it mean and discovering that those silly imaginary numbers actually had useful applications in common real-world problems like damped harmonic oscillators.
Onkar Joshi
Ranch Hand

Joined: Mar 01, 2007
Posts: 116
Originally posted by Jim Yingst:

Well, Euler was the one who discovered the Taylor series for the exponential function. I'm not sure who found the Taylor series for sine and cosine,...


Perhaps Mr.Taylor? Gotta give that dude some credit.

Heh.


SCJP 5 - 95% | SCWCD 1.4 - 88% | SCBCD 5 - 93%
Onkar Joshi's blog | LinkedIn profile
Arjunkumar Shastry
Ranch Hand

Joined: Feb 28, 2005
Posts: 986
Originally posted by Jim Yingst:
[b] It doesn't take that much genius to see that plugging imaginary numbers into Euler's exponential power series gives power series for sine and cosine.

I thought about this:
e^ix = cos(x) + i sin(x) and then ix = ln(cos(x) +i sin(x)) and then put x = PI,you get the above one.But without this plugging,formula doesn't seem obivious to me.One irrational number raised to the power of another irrational and sqrt(-1) is -1?
As per Gauss- "if this formula was not immediately obvious, the reader would never be a first-class mathematician".
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
The elegant formula is by Ramanujan:
Math.sqrt(1+ Math.sqrt(1+ 2*Math.sqrt(1+3*Math.sqrt(1+4*Math.sqrt(...... )))))
Now its a time to write recursive function for this.!!!


MH
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

Originally posted by Arjun Shastry:
The elegant formula is by Ramanujan:
Math.sqrt(1+ Math.sqrt(1+ 2*Math.sqrt(1+3*Math.sqrt(1+4*Math.sqrt(...... )))))
Now its a time to write recursive function for this.!!!

Maybe I'm little rusty on recursion, but
recursivePi(stepN):
Math.sqrt(1+(stepN)*recursivePi(stepN+1)); if (stepN<limit)
0 if (stepN>=limit)
(starting with step 1) converges to 2 very soon...



Also I found I can fill the stack in less than 5000 steps... probably I can get more using the heap (recursive calls to constructors?)

Interesting, though
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
Sorry,I put it wrongly.
It should be Math.sqrt(1+2*Math.sqrt(1+3*Math.sqrt(1+4*Math.sqrt(.....)))));
something like,

This still gives value of 3.0,and above N=5000,its stack overflow error.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding the value of PI