Python implementation of Babylonian Method – method to calculate square root of positive numbers.

IN
3
0.0000000001
OUT
sqrt( 3.0 ) =  0.3
sqrt( 3.0 ) =  5.15
sqrt( 3.0 ) =  2.86626213592233
sqrt( 3.0 ) =  1.956460731776899
sqrt( 3.0 ) =  1.74492093914502
sqrt( 3.0 ) =  1.7320982711195376
sqrt( 3.0 ) =  1.7320508082191837
# © 2018 TheFlyingKeyboard and released under MIT License 
# theflyingkeyboard.net

number = float(input("Enter number to square root: ")) 
precision = float(input("Enter precision of method: "))

if (number < 0):
  print("number must be >= 0")
elif (number == 0): 
  print("sqrt(0) = 0")
else:
  x = number / 10;
  y = number / x;

  counter = 0
  while (x - y > precision or counter <= 1):
    print("sqrt(", number, ") = ", x)
    
    x = (x + y) / 2
    y = number / x

    counter += 1

 



Python Babylonian Method – SQRT()
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close