## Approximation of pi

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

evinda
Posts: 32
Joined: Fri May 03, 2013 10:16 pm UTC

### Approximation of pi

Hey!
I am writing a code in C that finds an approximation of pi,with single and double precision, using the relation the sum Σ{1/k^2, k=1....n}, knowing that pi2/6=Σ{1/k^2, k=1....oo}.

I used a for loop to calculate the sum s=Σ{1/k^2, k=1....n}.

For n=100, when I declared the variables as float I found:3.1320766431412150. And when the variables are double, my output is: 3.1320765318091053
Could you tell me if these results are right?

z4lis
Posts: 767
Joined: Mon Mar 03, 2008 10:59 pm UTC

### Re: Approximation of pi

Since Google says that the first few digits of pi are 3.14159265359 and you didn't post any of your code for us to look at, the only reasonable answer I can think to give you is... no. But I do wonder why it's so far off. What happens for higher value of n?
What they (mathematicians) define as interesting depends on their particular field of study; mathematical anaylsts find pain and extreme confusion interesting, whereas geometers are interested in beauty.

Derek
Posts: 2181
Joined: Wed Aug 18, 2010 4:15 am UTC

### Re: Approximation of pi

Wolfram Alpha says that the approximation you got is about right for your method. The sum of inverse squares approximation converges very slowly, 100 terms just isn't enough to get the first three digits. I suggest using a different method to calculate the digits.

fishfry
Posts: 135
Joined: Wed Dec 21, 2011 6:25 am UTC

### Re: Approximation of pi

I did this once with the series pi/4 = 1/3 - 1/5 + 1/7 - 1/9 ...

The convergence is astonishingly slow. I ran my program for days just to get 10 or 12 decimal places.

Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

### Re: Approximation of pi

Because of the way computers round floats, you might get more accurate results if you start with the greatest value of k (so the least value of 1/k2).
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

### Re: Approximation of pi

Python agrees.
>>> n=100;s=sum(1.0/(i*i) for i in xrange(1,n+1));(6*s)**0.5
3.1320765318091053

>>> n=1000000;s=sum(1.0/(i*i) for i in xrange(1,n+1));(6*s)**0.5
3.1415916986605099

fishfry wrote:I did this once with the series pi/4 = 1/3 - 1/5 + 1/7 - 1/9 ...

The convergence is astonishingly slow. I ran my program for days just to get 10 or 12 decimal places.

Well that's the slowest converging arctan Taylor series, so one shouldn't expect it to run quickly. And with so many terms, you tend to get a lot of rounding error, although the alternating terms do help a bit.

If you want to calculate pi using arctan series, it's a Good Idea to use one of the Machin-Like formulae.

FWIW, I once used arctan Taylor series and
arctan(1) = 4 * (2*arctan(1/10) - arctan(1/515)) - arctan(1/239)
to compute pi to 10 decimals, by hand.

Also see http://turner.faculty.swau.edu/mathemat ... forms.html

jedelmania
Posts: 75
Joined: Tue Jan 15, 2013 5:48 pm UTC

### Re: Approximation of pi

fishfry wrote:I did this once with the series pi/4 = 1/3 - 1/5 + 1/7 - 1/9 ...

The convergence is astonishingly slow. I ran my program for days just to get 10 or 12 decimal places.

You can convert the series pi/4 = 1/3 - 1/5 + 1/7 - 1/9 ...

into

pi/2 = sum(n=0 to infinity) { [(2n)! (2n-2)!] / [ (2n+1)! (2n-1)!] x [1/2]^n } [for notational convenience, write (-1)!=(-2)!=1].

This converges pretty fast, although the large factorials may blow up your computer.

Qaanol
The Cheshirest Catamount
Posts: 3069
Joined: Sat May 09, 2009 11:55 pm UTC

### Re: Approximation of pi

On a tangentially-related note, I recently learned a neat proof that 22/7 exceeds π:

[; \int_0^1 \frac{x^4(1-x)^4}{1+x^2} dx ;]

The integrand is positive on (0, 1), and by polynomial division reduces to a polynomial plus a remainder term that integrates to a nice arctan value, and the result evaluates to 22/7 - π.

Also, less related, I have memorized some best-rational-approximations that are within half a millionth of the correct values (digits in black agree, the rest are grey):

355/113 = 3.14159292
π = 3.14159265

2721/1001 = 2.71828171
e = 2.71828182

1597/987 = 1.61803444
φ = 1.61803398

1393/985 = 1.41421319
√2 = 1.41421356

228/395 = 0.57721518
γ = 0.57721566

Of course, for most purposes I am still a big fan of sevenths:

22/7 = 3.14285714
19/7 = 2.71428571
11⅓/7 = 1.61904761
9.9/7 = 1.41428571
4/7 = 0.57142857

Edit: fixed integrand (thanks Nicias)
Last edited by Qaanol on Thu Oct 31, 2013 7:07 pm UTC, edited 2 times in total.
wee free kings

Nicias
Posts: 168
Joined: Tue Aug 13, 2013 4:22 pm UTC

### Re: Approximation of pi

Qaanol wrote:On a tangentially-related note, I recently learned a neat proof that 22/7 exceeds π:

[; \int_0^1 \frac{x^4(1-x)^4}{1+x} dx ;]

You forgot a ^2.

PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

### Re: Approximation of pi

jedelmania wrote:You can convert the series pi/4 = 1/3 - 1/5 + 1/7 - 1/9 ...

into

pi/2 = sum(n=0 to infinity) { [(2n)! (2n-2)!] / [ (2n+1)! (2n-1)!] x [1/2]^n } [for notational convenience, write (-1)!=(-2)!=1].

This converges pretty fast, although the large factorials may blow up your computer.

I don't think that's right. And the factorials in the terms of that series can be canceled.