Skip to content

Instantly share code, notes, and snippets.

@jaccarmac
Last active January 6, 2017 08:03
Show Gist options
  • Save jaccarmac/314fcf62e1d308382e27337f6510a587 to your computer and use it in GitHub Desktop.
Save jaccarmac/314fcf62e1d308382e27337f6510a587 to your computer and use it in GitHub Desktop.
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