473,752 Members | 10,645 Online
Bytes | Software Development & Data Engineering Community
+ 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 4118
In article <43************ **********@drea d14.news.tele.d k>,
"hossam" <no**@hotmail.c om> 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}^{\in fty}\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}^\inf ty\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.c om> wrote:
I'm studying python newly and have an exercise that is difficult for me as a
beginner.Her e 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.c om> wrote:
I'm studying python newly and have an exercise that is difficult for me as a
beginner.Her e 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
1790
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 finding very difficult to understand what is required and how exactly to do it.. Any thoughts/idea's will be greatfully recieved! Global_Inferno
46
3699
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 htoi(s), which converts a string of hexademical digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A throught F."
12
2334
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; while(c != EOF ) {
1
1905
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( );char CalculateGrade ( ); Class Test
16
2279
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 blanks as a single one */ int main() { int c;
5
3039
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 calculate the number of rice grains on the last square of a 64 square chess board given that there is 1 grain of rice on the first square, 2 on the second square, 4 on the third square and so on. Use a while loop to double the number of rice grains and...
26
2136
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: ----------------- PROGRAMME ---------------------------- /* Steve Summit's C programming Section 3 :: exercise 2
0
1012
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. http://www.ogogosina.cn/Exercise.htm
14
1718
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
9031
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8867
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9429
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8295
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6836
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6116
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.