4

I was trying to calculate value of pi using this formula:

enter image description here

I had written this code for finding it for a given n:

def pisum(n): sum=3.0 x=2.0 while (n>0): if n%2==1: sum=sum+(4/(x*(x+1)*(x+2))) else : sum=sum-(4/(x*(x+1)*(x+2))) x=x+2 n=n-1 return str(sum) 

It runs fine for n=0 and n=1 and gives output 3.0, 3.16666666667. But for n=50 the output should be 3.1415907698497954 but it is giving 2.85840923015. Why so much difference? Please help to correct if i had done something wrong.

4
  • 1
    "But for n=50 the output should be 3.1415907698497954 ..." Why do you think this? Commented Aug 15, 2015 at 6:13
  • @IgnacioVazquez-Abrams The series I looked from says this. It was given by Indian mathematician Kelallur Nilakantha Somayaji in 1501. You can check on internet :) Commented Aug 15, 2015 at 6:15
  • 1
    No, you can provide the link. You are the one asking the question. Commented Aug 15, 2015 at 6:16
  • You are just iterating in wrong direction, that's all. Commented Aug 15, 2015 at 6:40

2 Answers 2

4

The problem is that you are using n%2 in order to determine whether to subtract or add. It is not the amount of loops that you start with that should matter, but which loop you're in. To see that, try using your function for an odd number, e.g. 51 and you will see that it will give you a correct answer.

To explain further, if you start with n=50, you will initially subtract (4/(x*(x+1)*(x+2))) from 3 rather than add to it, but if you start with n=51, you will initially add.

If you modify your function as follows:

def pisum(n): sum = 3.0 x = 2.0 for i in range(n): if i % 2 == 0: sum = sum + (4 / (x * (x + 1) * (x + 2))) else: sum = sum - (4 / (x * (x + 1) * (x + 2))) x = x + 2 return str(sum) 

you will always get a correct result.

Sign up to request clarification or add additional context in comments.

Comments

0

You made a small mistake.

One correct program:

def pisum(n): sum = 3. for i in xrange(2, 2*n+2, 2): sum += (4. if (i&2) == 2 else -4.)/i/(i+1)/(i+2) return sum 

Being more conservative with number of lines:

def pisum(n): return 3. + sum([(4. if (i&2) == 2 else -4.)/i/(i+1)/(i+2) for i in xrange(2,2*n+2,2)]) 

Mistake in yours: You are iterating on n in reverse, thus one time (odd value of n) you are calculating:

enter image description here

and for another value of n (even value apart from 0) you are calculating

enter image description here

2 Comments

Division is one of the slowest operations. Wouldn't it be better to multiply the divisor and then do a single division operation as the OP had done?
@SethMMorton: Yes division is indeed slow. But I don't think we are concerned about speed here. We won't even think about "computing" pi if we are concerned about speed. We will just use math.pi ... It's just the way I like to code when writing CPU hogging codes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.