Skip to content

Instantly share code, notes, and snippets.

@zohaad
Last active July 29, 2016 01:16
Show Gist options
  • Save zohaad/f19a994d9a7f52be8eaf6c39baa949e7 to your computer and use it in GitHub Desktop.
Save zohaad/f19a994d9a7f52be8eaf6c39baa949e7 to your computer and use it in GitHub Desktop.
from math import sqrt
epsilon = 0.000000000001
def estimate(a):
x = 0.5 * a
while True:
y = (x + a / x) / 2
if abs(y - x) < epsilon:
return y
x = y
def test_square_root():
"""This only works when your IDLE window is a specific width, so you
will have to fiddle around with that a little bit"""
z = []
for i in range(1,10):
x = []
for g in (float(i), sqrt(i), estimate(i), abs(sqrt(i) - estimate(i))):
str_length = len(str(g)) # string length
blanks = (30 - str_length) * " "; # blank space needed
row_ = str(g) + blanks # adding them together
x.append(row_) # appending to the list
z.append(x) # appending the 9 lists to a big list
print(z)
test_square_root()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment