Last active
January 6, 2017 08:03
-
-
Save jaccarmac/314fcf62e1d308382e27337f6510a587 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
nSquareRoots(N) :- | |
numlist(1, N, Squares), | |
maplist(customSqrt(0.001), Squares, SquareRoots), | |
writeln("Squares:"), | |
writeln(Squares), | |
writeln("Square roots:"), | |
writeln(SquareRoots). | |
customSqrt(E, A, Y) :- | |
X is A * 0.5, | |
Y0 is (X*X + A)/(2*X), | |
sqrtLoop(E, A, X, Y0, Y). | |
sqrtLoop(E, A, X, Y0, Y) :- | |
(abs(X - Y0) < E, !, | |
Y is Y0) ; | |
(abs(X - Y0) > E, !, | |
X0 is Y0, | |
Y1 is (X0*X0 + A)/(2*X0), | |
sqrtLoop(E, A, X0, Y1, Y)). | |
nSquareRoots(100). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment