programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Finding the value of PI

Ranch Hand
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Rancher
Posts: 13459
• Number of slices to send:
Optional 'thank-you' note:
Pi is 3.14144588
error is 4.6719484661825556E-5

Ranch Hand
Posts: 376
• Number of slices to send:
Optional 'thank-you' note:
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?

Arjunkumar Shastry
Ranch Hand
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:

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.

Rancher
Posts: 280
• Number of slices to send:
Optional 'thank-you' note:
Trivia:

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

- Anand

Arjunkumar Shastry
Ranch Hand
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:
Write a program to find that ratio.

David O'Meara
Rancher
Posts: 13459
• Number of slices to send:
Optional 'thank-you' note:

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

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
As a matter of fact, no it isn't. Was there a typo in there?

Arjunkumar Shastry
Ranch Hand
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 13459
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 13459
• Number of slices to send:
Optional 'thank-you' note:
Oh well, but at least mine came from my own coded estimate...

Gabriel Claramunt
Ranch Hand
Posts: 376
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
[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
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
[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.

Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:

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.

Arjunkumar Shastry
Ranch Hand
Posts: 999
• Number of slices to send:
Optional 'thank-you' note:

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.

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".

Ranch Hand
Posts: 1907
1
• Number of slices to send:
Optional 'thank-you' note:
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.!!!

Gabriel Claramunt
Ranch Hand
Posts: 376
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 1907
1
• Number of slices to send:
Optional 'thank-you' note:
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.

 Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com