Script started on Mon Mar 30 20:21:47 1998 xavier[12]% cat Hmwk8.pp /* Webber Assignment 8 Dan J. Fraser (1219229) */ list_of_k_integers(X, List) :- counter(X,0,List). counter(Size, Count, List) :- list_of_k_integers_that_sum_to_S(Size,Count,List). counter(Size,Count,List) :- plus(Count,1,New_Count), counter(Size,New_Count,List). an_integer_less_than_or_equal_to(X, X) :- . an_integer_less_than_or_equal_to(Int, X) :- plus(Z, 1, Int), an_integer_less_than_or_equal_to(Z,X). list_of_k_integers_that_sum_to_S(0, 0, nil) :- . list_of_k_integers_that_sum_to_S(Size, 0, 0:Tail) :- not Size = 0, plus(New_Size,1,Size), list_of_k_integers_that_sum_to_S(New_Size, 0, Tail). list_of_k_integers_that_sum_to_S(Size, Sum, Y:X ) :- not Size = 0, not Sum = 0, an_integer_less_than_or_equal_to(Sum,Y), plus(New_Size,1,Size), plus(New_Sum,Y,Sum), list_of_k_integers_that_sum_to_S(New_Size, New_Sum, X). xavier[13]% cat Hmwk8.Given.pp pythagorean_triples(X, Y, Z) :- list_of_k_integers(3, X:Y:Z:nil), plus(2, Something, X), /* X >= 2 */ plus(X, SomethingElse, Y), /* Y >= X */ times(X,X,Xsquared), times(Y,Y,Ysquared), times(Z,Z,Zsquared), plus(Xsquared,Ysquared,Zsquared). sum_of_four_squares(S, W, X, Y, Z) :- list_of_k_integers(4, W:X:Y:Z:nil), times(W,W,Wsquared), times(X,X,Xsquared), times(Y,Y,Ysquared), times(Z,Z,Zsquared), plus(Wsquared, Xsquared, FirstSubTotal), plus(Ysquared,Zsquared, SecondSubTotal), plus(FirstSubTotal, SecondSubTotal, S). xavier[14]% pprolog Hmwk8.pp Hmwk8.Given.pp Welcome to picoProlog Reading Hmwk8.pp Reading Hmwk8.Given.pp # :- sum_of_four_squares(159, W, X, Y, Z). W = 11 X = 6 Y = 1 Z = 1 ? . yes # :- pythagorean_triples(X, Y, Z). X = 3 Y = 4 Z = 5 ? X = 6 Y = 8 Z = 10 ? X = 5 Y = 12 Z = 13 ? X = 9 Y = 12 Z = 15 ? X = 8 Y = 15 Z = 17 ? X = 12 Y = 16 Z = 20 ? X = 7 Y = 24 Z = 25 ? X = 15 Y = 20 Z = 25 ? X = 10 Y = 24 Z = 26 ? . yes # :- list_of_k_integers(5, X). X = 0:0:0:0:0:nil ? X = 1:0:0:0:0:nil ? X = 0:1:0:0:0:nil ? X = 0:0:1:0:0:nil ? X = 0:0:0:1:0:nil ? X = 0:0:0:0:1:nil ? X = 2:0:0:0:0:nil ? X = 1:1:0:0:0:nil ? X = 1:0:1:0:0:nil ? X = 1:0:0:1:0:nil ? X = 1:0:0:0:1:nil ? X = 0:2:0:0:0:nil ? X = 0:1:1:0:0:nil ? X = 0:1:0:1:0:nil ? X = 0:1:0:0:1:nil ? X = 0:0:2:0:0:nil ? X = 0:0:1:1:0:nil ? X = 0:0:1:0:1:nil ? X = 0:0:0:2:0:nil ? X = 0:0:0:1:1:nil ? X = 0:0:0:0:2:nil ? X = 3:0:0:0:0:nil ? . yes # :- ^D xavier[15]% exit script done on Mon Mar 30 20:32:15 1998