Question

In: Computer Science

Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use...

Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55).

Activity 7. Write a Prolog program to find the last item in a list.

give me the screenshot pls

Solutions

Expert Solution

Activity 6:- Prolog definition of the greatest common divisor of two numbers.

gcd = greatest common divisor

The predicate =/2 is for unification, not arithmetic assignment

The expression X1 = X-Y doesn't subract Y from X and store the result in X1.Rather , it unifies X1 with the term ,(X,Y). If for example, X= 5and Y=3 , then the result would be, X1 = 5-3 , not X1 = 2. The solution is to use is /2 which assigns evaluated arithmetic expressions: X1 is X-Y.

Other predicates, besides the base case predicate, successfully match the base case

The clause , gcd(0,X,X):- X>0 is a reasonable base case, but it is never attempted because the second clause(gcd(X,Y,Z):-X<Y,...) will always successfully match the same conditions first, leading to infinite recursion and a stack overflow.

One way to fix this is to move the base case to the first clause and use a cut to avoid backtracking after it successfully executes:

gcd(0, X, X):- X>0,!.

gcd(X,Y,Z):- X>= Y, X1 is X- Y, gcd(X1 , Y, Z).

gcd(X, Y, Z):- X<Y, X1 is Y - X , gcd(X1, X, Z).

Now,

  1. gcd(4 , 10)  

gcd(4, 10, X).

X = 2?;

2. gcd(15, 36)

gcd(15 , 36 , X).

X = 3?;

3.gcd(25, 55)

gcd(25, 55, X).

X = 5?;

Activity 7:- Prolog Program to find the last item in a list


Related Solutions

Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
A. 271 and 516 1.  Find the greatest common divisor, d, of the two numbers from part...
A. 271 and 516 1.  Find the greatest common divisor, d, of the two numbers from part A using the Euclidean algorithm. Show and explain all work. 2.  Find all solutions for the congruence ax ? d (mod b) where a and b are the integers from part A and d is the greatest common divisor from part A1. Show and explain all work.
Write the greatest common divisor over the given filed as a linear combination of the given...
Write the greatest common divisor over the given filed as a linear combination of the given polynomials. That is, given f(x) and g(x), find a(x) and b(x) so that d(x) = a(x)f(x) + b(x)g(x), where d(x) is the greatest common divisor of f(x) and g(x). (a) x^10 − x^7 − x^5 + x^3 + x^2 − 1 and x^8 − x^5 − x^3 + 1 over Q. (b) x^5 + x^4 + 2x^2 − x − 1 and x^3 +...
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1....
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1. Prove that there is a natural number k such that n divides ((m^k) − 1).
4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13,...
4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13, and the greatest common divisor of 34 and 21. (b) It turns out that 21 and 13 is the smallest pair of numbers for which the Euclidean algorithm requires 6 steps (for every other pair a and b requiring 6 or more steps a > 21 and b > 13). Given this, what can you say about 34 and 21? (c) Can you guess...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf)...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers. The greatest common divisor of a and b is written as gcd (a, b), or sometimes simply as (a, b). For example, gcd (12, 18) = 6, gcd (−4, 14) = 2 and gcd (5, 0) = 5. Two numbers are called co-prime or relatively prime...
Use logical language Prolog. Use SWISH (SWI Prolog for Sharing) Write a Prolog program that finds...
Use logical language Prolog. Use SWISH (SWI Prolog for Sharing) Write a Prolog program that finds the maximum of a list of numbers. Submission file should include a screenshot with tracing a program execution when a list only has 2 members and execution without tracing when a list has 8 elements.
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its...
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its coefficients is 1. Prove the product of two primitive polynomials is primitive. [Hint: Use proof by contradiction.]
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y):     5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and...
JAVA CLASS Greatest Common Divisor Finder Enter first number: 12 Enter second number: 8 Greatest common...
JAVA CLASS Greatest Common Divisor Finder Enter first number: 12 Enter second number: 8 Greatest common divisor: 4 Continue? (y/n): y Enter first number: 77 Enter second number: 33 Greatest common divisor: 11 Continue? (y/n): y Enter first number: 441 Enter second number: 252 Greatest common divisor: 63 Continue? (y/n): n The formula for finding the greatest common divisor of two positive integers x and y follows the Euclidean algorithm as follows: 1.   Subtract x from y repeatedly until y...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT