Last active
June 22, 2016 17:56
-
-
Save KoderDojo/a5a34508543db9b8378286cbd4f35b28 to your computer and use it in GitHub Desktop.
Babylonian Method of Calculating Square Roots Using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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