Assignment 3: Square Roots

Overview

One of the oldest methods for computing the square root of a number is the Babylonian Method. The Babylonian Method uses an iterative algorithm to make successively more accurate estimates of a number's square root. The algorithm stops iterating when the estimate shows no further sign of improvement, or when the estimate is within some acceptable margin of error. The acceptable margin of error is often called an epsilon.

Assuming that you need to solve for the square root of x, the algorithm works as follows.

  1. Choose an epsilon value that determines how close your solution should be to the actual square root value before you decide it is "good enough." Because this assignment asks you to solve for the square root to three decimal places, we can safely set the epsilon value to 0.0001 (four decimal places). This guarantees that our solution will be accurate to the precision we need to display to the screen.
  2. Choose an initial estimate e for the square root of x. An easy and perfectly valid approach is to set the initial estimate e=x. For example, you could set the first estimate for the square root of 4 to be 4.
  3. Evaluate the estimate by dividing the value x by your estimate e, and comparing the result of that division to the current estimate e. If your estimate estimate e were to be exactly equal to the square root, then you would find that x/e=e. In practice, unless you are luckly, there will typically be some difference between these two values (x/e and e) even after many iterations of the algorithm.
  4. Determine if the estimate is "good enough" to stop. A smaller difference between x/e and e reflects a more accurate estimate. If the difference is smaller than your epsilon value, then you've found the answer! If not---if the difference is greater than the epsilon value---then you need to proceed to step 5 below.
  5. Revise the esimate (if needed based on Step 4) by setting e to the average of the fraction x/e and your old estimate e. Using this new estimate, go back to Step 3.

For this assignment, you need to write a Python program that solves for square roots using the algorithm outlined above. See the "Basic" and "Advanced" requirements sections below for more detailed instructions.

Please Note: While Python has its own way of calculating square roots, you are not allowed to use it for this assignment. Instead, you must implement your own solution to this classic mathematical problem. To be clear, you should NOT use any pre-defined Python square root function anywhere in your assignment solution.


Basic Requirements

Satisfying all basic requirements perfectly, with no points deducted for any reason, would earn a maximum score of 8 out of 10 for this assignment.

You need to implement the Babylonian Method for computing square roots. Based on that core functionality, you are to write an interactive program that:


Advanced Requirements

Satisfying both the basic and advanced requirements perfectly, with no points deducted for any reason, will result in a full 10 out of 10 score for this assignment.

Expand on the basic requirements by extending your program as follows.

For example, if the user enters 9 as the minimum for the range and 11 as the maximum for the range, your answer should produce a table with three rows (for 9, 10, and 11) and two columns (one for the initial values 9,10,11; and a second for the corresponding square root values).


Sample Output

Examples of the output produced by my solution to this assignment can be found here and here.

Grading Criteria

This assignment will be graded on a 10 point scale. Your grade for this assignment will be based on a combination of factors including:

Seeking Help

For general questions about Python, please use the class forum on Piazza to seek assistance. For questions that are personal in nature or that would reveal a solution to the assignment, you ask for help by email or during office hours. However, please note that emailed questions will not receive an immediate response. It is likely that it will take 24-48 hours for me to respond.


Submitting Your Solution

Please Note: You must name the main python file for your assignment "assignment<number>_<onyen>.py". For example, for assignment 3 I would name my file assignment3_gotz.py because my onyen is gotz.

Using the wrong name for your file will be cost you points on your assignment grade. Please follow this requirement carefully!

Please submit your assignment via Sakai. You should submit a zip file containing your entire project folder. To create the zip file, follow the submission instructions that have been posted at the bottom of the "Other Information" page on our course website.

The due date for this assignment can be found on the course schedule.