can't solve an exercise-help me with it

Tim Roberts timr at probo.com
Thu Jan 12 02:59:56 EST 2006


"hossam" <nono at hotmail.com> wrote:

>I'm studying python newly and have an exercise that is difficult for me as a 
>beginner.Here it is :
>"Write a program that approximates the value of pi by summing the terms of 
>this series:
>4/1-4/3+4/5-4/7+4/9-4/11+.... The program should prompt the user for n, the 
>number of terms to sum and then output the sum of the first n terms of this 
>series."
>
>any help would be appreciated.

Is this homework?  You should show us what you already have, and we can
show you where may be going wrong.  This isn't a free homework service.

This is not a hard problem, although that's a terrible series for computing
pi.  At 100,000 terms, it still only has 5 digits.

  n = input( "How many terms? " )
  sum = 0
  sign = 4.0
  for i in range(n):
      sum += sign / (i+i+1)
      sign = -sign
    
  print sum
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list