Numbers

Let’s do some numerical calculations in Python!

You have seen that the math library contains a function that computes the square root of numbers (math.sqrt(x)). In this lab, you are to write your own algorithm for computing square roots. One way to solve this problem is to use a guess-and-check approach. You first guess what the square root might be, and then see how close your guess is. You can use this information to make another guess and continue guessing until you have found the square root (or a close approximation to it).

One particularly good way of making guesses is to use Newton’s method. Suppose \(x\) is the number we want the root of and guess is the current guessed answer. The guess can be improved by using \(\frac{guess + \frac{x}{guess}}{2}\) as the next guess.

Your Assignment

Write a program that implements Newton’s method, with the requirements:

  1. Print a short introduction about the program’s purpose.
  2. Prompt the user for the value \(x\) to find \(\sqrt{x}\).
    • Start with a guess value of \(x/2\).
  3. Prompt the user for the number of times to improve the guess.
    • Loop the specified number of times applying Newton’s method, and report the final value of your calculation.
  4. Subtract your final estimate from the value of math.sqrt(x) to show how accurate is your final estimate.

Output

If you wanted to find the square root of 100 using 10 iterations of Newton’s method, it should appear like this:

This program calculate the square root of a given number using the Newton's method
What is the number for which you want to calculate the square root? 100
How many iterations do you want to use? 10
My guess for the square root of 100.0 is 10.0
The difference between my guess and the real result is 0.0

Submission Requirements

Your file is required to be named lab_numbers.py.

Please read and follow the Submission Requirements to avoid point deductions specified below.

Grading

The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Note: There may be additional criteria for grading, this is just a summary of the major rubric items.

Grading Rubric
Type Description Point Loss (Max)
  [General]  
SyntaxError Exceptions A program that doesn’t even start due to syntax errors. -30
Docstrings Missing proper lab header (e.g. no name, lab section, etc) -10
main() Your program should have main() as the first-called function -10
PEP 8 Style Egregious problems with your code styling -10
Incorrect Filename Follow the submission instructions -5
Commenting Insufficient or excessive comments -5
  [Lab Specific]  
Introduction Program doesn’t print an introduction: -5
Implementation Program doesn’t implement Newton’s method correctly -15
Results Program doesn’t print the estimated result -5
Result Accuracy Program doesn’t print the difference between the correct result and the estimated one -10

Extra Credit

Extra credit work must be submitted in what would be your original submission file for the assignment. All extra credit code must be clearly commented in order to receive any points.

  • +10 points

    Print a simple table showing the guess number (starting at 1!), the current estimate, and the difference between the current estimate and the true value of square root of \(x\):

    For example:

    This program calculate the square root of a given number using the Newton's method
    What is the number for which you want to calculate the square root? 100
    How many iterations do you want to use? 10
    1 26.0 16.0
    2 14.923076923076923 4.923076923076923
    3 10.812053925455988 0.812053925455988
    4 10.030495203889796 0.030495203889795874
    5 10.000046356507898 4.63565078980821e-05
    6 10.000000000107445 1.0744471978796355e-10
    7 10.0 0.0
    8 10.0 0.0
    9 10.0 0.0
    10 10.0 0.0
    My guess for the square root of 100.0 is 10.0
    The difference between my guess and the real result is 0.0