Created
January 8, 2009 14:23
-
-
Save bartek/44735 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
# 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