Program 1 for Comp 150

Assignment Index  Course Home Page

This is a pair assignment worth 20 points.

The program should begin with a comment including your names, a brief description of what the program does and information on any help you received on the project.  If it is entirely your own work, say so.  If anyone helped either of you, identify them by name and describe how they helped.  

If you helped anyone else, separately identify them. 

Submit a single file to Blackboard, with the naming convention prog1_ID.py  where ID is the blackboard ID of the submitter.  For instance I would submit prog1_aharrin.py.

Remember to have each team member submit a log to Blackboard also.  Your grade will not be final until the log is passed in.  The log format is described in the Pairs Administration page:  The log should indicate your hours working on the homework and an assessment of your contribution and an assessment of your partner's contribution.  Please name the file 150log1ID.doc, where ID is your blackboard ID, and the 1 refers to Program 1, so if I were submitting a log for this assignment, it would be called 150log1aharrin.doc.

The Problem: 

This program will implement a well known technique for approximating the square root of a number.  In general, a positive number can be written as the product of two positive numbers in an infinite number of ways.  For example, 

36 = 1 * 36 = 2*18 = 3* 12 = 4* 3 = 5 * 7.2 = 6 * 6 = 10*3.6 = 16 * 2.25 etc.

If the factors are the same, then use have found the square root.  Notice that when one factor is larger than the square root, the other one is smaller.  So one way to proceed is to choose one factor of a number, calculate the other by dividing.  Generally they will be different.  Since we want to find two equal factors, the next choice for the first factor can be the average of the two we found.  Each time through this loop we get a better approximation.  You can start your first guess with 1.0 if you want.  (The decimal point is important to avoid integer arithmetic.)

For example, if we try 5 steps on the number 36, starting from the initial guess 1.0, then the next five approximations are
1.0 and  36/1.0 have average 18.5
18.5 and 36/18.5 have average 10.222972973
10.222972973 and 36/10.222972973 have average 6.87222673764
6.87222673764 and 36/6.87222673764 have average 6.05535174485
6.05535174485 and 36/6.05535174485 have average 6.00025298412
The above steps describe what happens internally in this example, not what is printed out, (except for the last number).

Your program should

Note 1:  This is the best we can do with the amount of Python introduced so far.  It would make more sense to automatically repeat this process until the answers do not change, but we have not studied that kind of loop yet.

Note 2:  Math may not be your favorite thing, but at least it is better to have Python doing the calculations rather than you!   We are doing numerical things first because they are particularly easy to introduce.  As we introduce more of Python, we will go on to processing text and producing graphics, web pages, ....
Assignment Index  Course Home Page