a3_1.py

epsilon = 0.0000001
def square_root(a):
	x = a / 2.0
	while True:
		y = (x + a / x) / 2
		if abs(y - x) < epsilon:
			break
		x = y
	return y

def test_square_root():
	import math
	for i in range(1, 10):
		mine = square_root(i)
		theirs = math.sqrt(i)
		delta = abs(mine - theirs)
		print "%3.1f %-13.11g %-13.11g %.11g" % (float(i),  mine, theirs, delta)

test_square_root()