50 likes | 236 Views
King Saud University College of Computer and Information Sciences Information Technology Department IT327 - Intelligent systems. Prolog And FOL Exercises. Question 1. Write a Prolog Program to find Fibonnaci Number defined as follows:
E N D
King Saud University College of Computer and Information Sciences Information Technology Department IT327 - Intelligent systems Prolog And FOL Exercises
Question 1 • Write a Prolog Program to find Fibonnaci Number defined as follows: f(1) = 1, f(2) = 1, ∀ n > 2 : f(n) = f(n − 1) + f(n − 2) fibonacci(1,1). fibonacci(2,1). fibonacci(N,F) :- N>2, N1 is N-1, fibonacci(N1,F1), N2 is N-2, fibonacci(N2,F2), F is F1+F2.
Question 2 • Write a Prolog program to find the greatest common divisor using the following algorithm gcd(a,b) = a if a=b gcd(a,b) = gcd(a-b,b) if a>b gcd(a,b) = gcd(a,b-a) if b>a gcd(A,A,A). gcd(A,B,Result) :- (A>B -> (Temp is A-B, gcd(Temp,B,Result)); B>A -> (Temp is B-A, gcd(A,Temp,Result))).
Question 3 Solve the following Prolog queries ?- op(X) = op(1). X = 1 ?- op(op(Z),Y) = op(X, op(1)). Y = op(1) Z = _G157 X = op(_G157) ?- op(X,Y) = op(op(Y),op(X)). X = op(op(op(op(op(op(op(op(op(op(...)))))))))) Y = op(op(op(op(op(op(op(op(op(op(...))))))))))
Question 4 Solve the following Prolog queries using lists ?-[1,2,3] = [1|X]. X = [2,3] ?- [1,2,3] = [1,2|X]. X = [3] ?- [1 | [2,3]] = [1,2,X]. X = 3 ?- [1 | [2,3,4]] = [1,2,X]. No ?- [1 | [2,3,4]] = [1,2|X]. X = [3,4]