Skip to content

Instantly share code, notes, and snippets.

@bartek
Created January 8, 2009 14:23
Show Gist options
  • Save bartek/44735 to your computer and use it in GitHub Desktop.
Save bartek/44735 to your computer and use it in GitHub Desktop.
# section1.1.7 from SICP written in python .. was just making sure I understood how it all worked.
def square(x):
return x * x
def sqrt_iter(guess, x):
if good_enough(guess, x):
return guess
else:
return sqrt_iter(improve(guess, x), x)
def improve(guess, x):
return average(guess, (x / guess))
def average(x, y):
return (x + y) / 2
def good_enough(guess, x):
return abs( square(guess) - x) < 0.001
def sqrtc(x):
return sqrt_iter(1.0, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment