Created
February 16, 2023 18:26
-
-
Save thinkphp/760188ca9b0d78782a41fe25ca975a82 to your computer and use it in GitHub Desktop.
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
# | |
# Square Root Babylonian Method | |
# @Adrian Statescu | |
# | |
def sqrt_babylonian(n): | |
x = n | |
y = 1.0 | |
eps = 0.000001 | |
while x - y > eps: | |
x = (x + y) / 2 | |
y = n / x | |
return x | |
def main(): | |
n = int(input("n = ")) | |
r = sqrt_babylonian(n) | |
print("sqrt(%d) = %f" % (n, r)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment