70 likes | 191 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) is op(1). ERROR: Arithmetic: ‘op/1’ is not a function ?- 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]
FOL question Express the following sentences in first-order logic: (a) All students love AI (b) Some students love AI (c) At least two students love AI (d) Exactly two students love AI (e) At most two students love AI
FOL question • All students love AI • ∀ x Student(x) => Loves(x, AI) • Some students love AI • Ǝ x Student(x) Loves(x, AI) • At least two students love AI • Ǝ x,y (x≠y) Student(x) Loves(x,AI) Student(y) Loves(y,AI) • Exactly two students love AI • Ǝ x,y ∀ z x≠yx≠zy≠z Student(x) Loves(x,AI) Student(y) Loves(y,AI) Student(z) ┐Loves(z,AI) • Or • Ǝ x,y (x≠y Student(x) Loves(x,AI) Student(y) Loves(y,AI) ∀ z (Student(z) Loves(z,AI) < --> (x=z V y=z)) ) • At most two students love AI ∀ x,y ,z Student(x) Loves(x,AI) Student(y) Loves(y,AI) Student(z) Loves(z,AI) (x=y Vx=z V y=z)