473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can't solve an exercise-help me with it

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
Jan 12 '06 #1
3 4016
In article <43**********************@dread14.news.tele.dk>,
"hossam" <no**@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.

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

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Jan 12 '06 #2
On Thu, 12 Jan 2006 05:51:10 +0100, "hossam" <no**@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?
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
Jan 12 '06 #3
"hossam" <no**@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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Simon Mansfield | last post by:
I have been given a list of exercises to do by my tutor, one of which is this: http://www.cems.uwe.ac.uk/~amclymer/Software%20Design%20and%20C++/Exercises/Exercise%208/Exercise.html Which i am...
46
by: Herrcho | last post by:
Hi~ i've studied C for a few months myself, and i'd appreciate it if anyone could improve my coding or correct it. the following is my solution to the K&R exercise 2-3 "Write the function...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
1
by: otupia | last post by:
Exercise : Given the diagram below. Transform the diagram into C++ program. Class Grade Private members :char letter;float score; Public Members :void SetScore( float );float GetScore(...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
26
by: arnuld | last post by:
this is the programme i created, for exercise 2, assignment 3 at http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html it runs fine. i wanted to know if it needs any improvement: ...
0
by: liukaiyuan | last post by:
American Made Circuit Exercise Equipment Come look and see the circuit traning exercise equipment that is fully ajustible to fit all your work out needs. Start your own center today....
14
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.