/approximate-square-root

Approximating Square Roots computationally

Primary LanguageShell

Approximate a square root

Description

Write a program that asks the user for a positive number and then outputs the approximated square root of the number. Use Newton's method to find the square root, with epsilon = 0.01. (Epsilon is the allowed error, plus or minus, when you square your calculated square root and compare it to your original number.)

Objectives

Learning Objectives

After completing this assignment, you should understand:

  • How variables work
  • How while works
  • The concept of successive approximation

Performance Objectives

After completing this assignment, you should be able to:

  • Write a Python script
  • Ask for input
  • Print output

Details

Deliverables

  • A GitHub repo called approximate-square-root containing at least:
    • This README.md file
    • a file called square_root.py

Requirements

Here is an example of the program running correctly:

$ python square_root.py
Enter a positive number: 4
The square root of 4 is 2.0.

$ python square_root.py
Enter a positive number: 20
The square root of 20 is 4.472137791286727.

Your program will be tested with the script test.sh. To run this script, run brew install roundup first.

Normal Mode

Newton's method of successive approximations says that whenever we have a guess y for the value of the square root of a number x, we can get a better guess (one closer to the actual square root) by averaging y with x/y. If we do this over and over, we should be able to get a very accurate guess.

You have to write a script that asks the user for a positive number and then compute the square root with a maximum error of 0.01. You then print out your answer to the user.

Hard Mode

In each iteration of your loop, print out the current number of iterations and the current guess.

Examine the input into your program and give an appropriate error message if a non-number or negative number is given.

Extra Hard Mode

Look up complex numbers in Python. Allow negative numbers as input into your program.

Additional Resources

Credit

This assignment is adapted from SICP by Abelson and Sussman.