Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from math import sqrt
  4.  
  5. # Get Input from User (number)
  6. number = 16
  7.  
  8. mathlibresult = sqrt(number)
  9.  
  10. iteration = 1
  11. newguess = number/2
  12. difference = newguess - mathlibresult
  13.  
  14. while (difference > abs(1e-10)):
  15. print('Iteration: ', iteration, ' Guess: ', newguess, ' Difference: ', difference)
  16. oldguess = newguess
  17. newguess = 0.5 * (oldguess + number/oldguess)
  18. iteration = iteration + 1
  19. finaldifference = difference
  20. difference = newguess - mathlibresult
  21.  
  22. print('The square root from the math lib is: ', mathlibresult)
  23. print('The difference between math lib and Newton method is ', finaldifference)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement