Skip to content

Instantly share code, notes, and snippets.

@KoderDojo
Last active June 22, 2016 17:56
Show Gist options
  • Save KoderDojo/a5a34508543db9b8378286cbd4f35b28 to your computer and use it in GitHub Desktop.
Save KoderDojo/a5a34508543db9b8378286cbd4f35b28 to your computer and use it in GitHub Desktop.
Babylonian Method of Calculating Square Roots Using Python
number = abs(float(raw_input("Calculate square root of? ")))
guess = abs(float(raw_input("Initial guess? ")))
epsilon = 0.001
while True:
difference = guess**2 - number
print('{} : {}'.format(round(guess, 4), round(difference, 4)))
if abs(difference) <= epsilon:
break
guess = (guess + number / guess) / 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment