Calculate Square Root (Due 14 July 2011)

This method for calculating the square root of a number n starts by making a (non zero) guess at the square root. It then uses the original guess to calculate a new guess, according to the formula


newGuess = ((n / oldGuess) + oldGuess) / 2.0;

Have two variables oldGuess and newGuess. Initialize oldGuess to n / 2.0 and calculate newGuess according to the above formula. Use a while loop to iterate as long as the absolute value of the difference between the oldGuess and newGuess is greater than 1.0E-06. Do not forget to reset the value of oldGuess to the newGuess value in the while loop.

In your program you will prompt the user to enter a positive number. If the number is negative, print an error message and ask the user to try again. For a positive number, calculate the square root using the above method. Find the difference between the square root you obtained and the value obtained from using the exponentiation operator. Write out the value the user entered, the square root you computed, and the difference (your square root - n ** 0.5). Your session will look like this:

Enter a positive number: 12

Square root is: 3.46410161514 

Difference is: 0.0

There is a piece of trivia associated with this algorithm. Professional mathematicians are familiar with this algorithm as Newton's method. But the algorithm is much older than Newton. In first century AD, the Greek mathematician Heron of Alexandria used this method to determine the square root of 720. There is evidence that this method goes even further back, as far as the Babylonians. In the Yale University Babylonian Collection, there is a cuneiform tablet that seems to use this method to calculate the square root of 2.

The program that you will be writing will be called CalcSqrt. We will be looking at good documentation, and adherence to the coding convention discussed in class. You may use the same variable names used in the problem statement. Your file CalcSqrt.py will have the following header:


#  File: CalcSqrt.py

#  Description:

#  Student Name:

#  Student UT EID:

#  Course Name: CS 303E

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

Use the turnin program to submit your CalcSqrt.py file. The TAs should receive your work by 11 PM on Thursday, 14 July 2011. There will be substantial penalties if you do not adhere to the guidelines.