Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. You have seen that the math library contains a function that computes the square root of
  2. numbers. In this exercise, you are to write your own algorithm for computing square roots.
  3. One way to solve this problem is to use a guess-and-check approach. You first guess what
  4. the square root might be and then see how close your guess is. You can use this information
  5. to make another guess and continue guessing until you have found the square root (or a
  6. close approximation to it). One particularly good way of making guesses is to use Newton’s
  7. method. Suppose x is the number we want the root of and guess is the current guessed
  8. answer. The guess can be improved by using guess+ x
  9. guess
  10. 2
  11. as the next guess.
  12. Write a program that implements Newton’s method. The program should prompt the user
  13. for the value to find the square root of (x) and the number of times to improve the guess.
  14. Starting with a guess value of x/2, your program should loop the specified number of times
  15. applying Newton’s method and report the final value of guess. You should also subtract your
  16. estimate from the value of math.sqrt(x) to show how close it is.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement