Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!comp.vuw.ac.nz!steve From: steve@comp.vuw.ac.nz (Steve Cassidy) Newsgroups: comp.lang.prolog Subject: Re: HELP!! Need a square root function/predicate in Prolog Message-ID: <1990Aug08.230259.16224@comp.vuw.ac.nz> Date: 8 Aug 90 23:02:59 GMT References: <1990Aug08.001233.6587@hoss.unl.edu> Sender: news@comp.vuw.ac.nz (News Admin) Organization: Dept. of Comp. Sci., Victoria Uni. of Wellington, New Zealand. Lines: 28 Sanjiv K. Bhatia writes: >I am working on some experiments using Prolog and need a square root >function. Will some kind soul mail me a copy of the same if you happen to >have one handy? It's probably nothing like what you wanted but here's a version of square/2 that is reversible - you can use it for square roots as long as the root is integer. Integers are represented as 1 = s(0), 2 = s(s(0)) etc. Enjoy... Steve square(N,M) :- times(N,N,M). times(s(0),N,N). times(N,M,P) :- add(N1,s(0),N), times(N1,M,P1), add(M,P1,P). add(X,0,X). add(X,s(Y),s(Z)) :- add(X,Y,Z). succ(0,0) :- !. succ(N,s(SM)) :- M is N - 1, succ(M,SM).