/* generate integers */ is_int(0). is_int(X) :- is_int(Y), X is Y+1. /* generate pairs with sum X */ pair(X,X,0). pair(X,N1,N2) :- X>0, X1 is X-1, pair(X1,N1,N3), N2 is N3+1. /* generate Pythagorean triples */ pythag(X,Y,Z) :- is_int(A), pair(A,Y,Z), % get a pair of numbers Y and Z Z>0, Y>Z, % use only those where Y > Z S1 is Y*Y+Z*Z, % find the sum of their squares sqrt(S1,X), % find the integer nearest its root S2 is X*X, S1 = S2. % is it a pythagorean triple? /* squareroot finder */ sqrt(A,R) :- is_int(R), % generate a possible answer X is R*R, Y is (R+1)*(R+1), X =< A, Y > A, !.