# Finding the value of PI

Arjunkumar Shastry

Ranch Hand

Posts: 986

posted 9 years ago

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 ]

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

posted 9 years ago

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?

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

Posts: 986

posted 9 years ago

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.

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.

Namma Suvarna Karnataka

Arjunkumar Shastry

Ranch Hand

Posts: 986

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

Arjunkumar Shastry

Ranch Hand

Posts: 986

posted 9 years ago

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 ]

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 ]

Namma Suvarna Karnataka

posted 9 years ago

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.

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.

Gabriel

Software Surgeon

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 9 years ago

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

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.

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.

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.

**[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.

"I'm not back." - Bill Harding, *Twister*

Arjunkumar Shastry

Ranch Hand

Posts: 986

posted 9 years ago

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 ]

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 ]

Namma Suvarna Karnataka

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 9 years ago

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

**[Arjunkumar Shastry]: Now I really wonder how Euler arrived at this formula?**

e^ipi = -1

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.

"I'm not back." - Bill Harding, *Twister*

Onkar Joshi

Ranch Hand

Posts: 116

posted 9 years ago

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

Heh.

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

Posts: 986

posted 9 years ago

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

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

Namma Suvarna Karnataka

Arjun Shastry

Ranch Hand

Posts: 1901

1

posted 9 years ago

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

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

Gabriel

Software Surgeon

Arjun Shastry

Ranch Hand

Posts: 1901

1

posted 9 years ago

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

MH