Skip to content

Instantly share code, notes, and snippets.

@valexey
Created January 5, 2012 10:43
Show Gist options
  • Save valexey/1564676 to your computer and use it in GitHub Desktop.
Save valexey/1564676 to your computer and use it in GitHub Desktop.
define method newtons-sqrt (x :: <number>)
local method sqrt1 (guess)
// note call to other local method
if (close-enough? (guess))
guess
else
sqrt1 (improve (guess)) // note self-recursive call
end if
end sqrt1,
method close-enough? (guess)
abs (guess * guess - x) < .0001
end close-enough?,
method improve (guess)
(guess + (x / guess)) / 2
end improve;
sqrt1 (1)
end method newtons-sqrt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment