% $Id: PythTriples.oz,v 1.2 2007/12/08 17:33:14 leavens Exp leavens $ \insert 'SolveAll.oz' declare % Find all Pythagorean triples whose elements are strictly less than Lim proc {PythTriples Lim ?A ?B ?C} {GenTriple Lim A B C} ( (A*A) + (B*B) == (C*C) ) = true end proc {GenTriple Lim ?A ?B ?C} %% Choosing C first and using that as a limit %% for choosing A and B makes the search space smaller. {UpTo Lim C} {UpTo C A} {UpTo C B} end proc {UpTo Lim ?N} %% We assume tht Lim is defined, %% which allows us to cut off the search %% and do arithmetic on Lim without suspending. (Lim >= 0)=true choice N=Lim [] {UpTo Lim-1 N} end end