can't solve an exercise-help me with it

H

hossam

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.

benni
 
M

Michael J. Fromberger

"hossam said:
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.

benni

The series you're describing is basically expanding the Taylor series
for 4 * atan(1), where

i
oo -1
atan(1) = SUM ------
i = 0 2i + 1

(or in LaTeX: \sum_{i=0}^{\infty}\frac{-1^i}{2i+1})

To enumerate the terms of this series, you can treat i as a counter, and
compute the numerator and denominator either directly or from their
previous values. The sum can be accumulated into a separate variable.
So, for instance,

.. sum = 0. ; num = 1. ; den = 1.
.. for i in xrange(n):
.. sum += num / den
.. num = -num
.. den += 2
..
.. pi = 4 * sum # approximately...

Keep in mind that this approximation for atan(1) converges very slowly,
so you will need to do quite a lot of terms before it will converge (you
need about a thousand terms to get 2 significant figures after the
decimal point). You might have better results using Machin's formula,

atan(1) = 4 * atan(1/5) - atan(1/239)

Or, pi = 16 * atan(1/5) - 4 * atan(1/239). The Taylor series will
converge more quickly for atan(1/5) and atan(1/239).

(LaTeX: \mathrm{atan}(x) = \sum_{i=0}^\infty\frac{(-1^i)x^i}{2i+1})

Cheers,
-M
 
B

Bengt Richter

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?
What have you tried so far?
Hints: raw_input, int. And sum, if you want a one-liner.
Hint2: try 4/7 and 4.0/7 interactively

Regards,
Bengt Richter
 
T

Tim Roberts

hossam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top