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. The program should print a short introduction about its purpose.

  2. The program should prompt the user for the value \(x\) to find \(\sqrt{x}\).

  3. The program should also prompt the user for the number of times to improve the guess.

    • Starting with a guess value of \(x/2\), your program should loop the specified number of times applying Newton’s method and report the final value of guess.
  4. Finally, you should also subtract your final estimate from the value of math.sqrt(x) to show how accurate your estimate is.

Example output:

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

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: submit a file named lab_numbers.py.

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Numbers
    
    This program calculates square root using Newton's Method.
    
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015
    """
    

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): If you code something that is not obvious in your code, or something you’d like to explain more thououghly to the grader, I encourage the use of comments. Unclear parts of your code can receive point deductions, so be judicious about using comments

    Examples:

    # This block comment explains that this
    # part of my code does something interesting.
    # Block comments should not be overused!
    x = 1 + 1
    

    Or

    x = x + 1   # This is an inline comment
    

    Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: In general, follow Pythonic Coding Style, a set of coding guidelines to make your Python code more readable and maintainable. You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.

Grading

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

Here’s a brief grading rubric:
  • Syntax errors: -20 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Program doesn’t print an introduction: -5 points
  • Program doesn’t implement Newton’s method (correctly, or at all): -15
  • Program doesn’t print the estimated result: -10 points
  • Program doesn’t print the difference between the correct result and the estimated one: -10 points
  • Missing main() function: -10 points
Extra Credit:
  • 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\): +10 points

    • 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
      

Note: Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.

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