Question: Consider the Prolog gcd program in Figure 1.2. Does this program work backward as well as forward? (Given integers d and n, can you use

Consider the Prolog gcd program in Figure 1.2. Does this program work “backward” as well as forward? (Given integers d and n, can you use it to generate a sequence of integers m such that gcd(n, m) = d?) Explain your answer.

Figure 1.2:

int gcd(int a, int b) { // C while (a != b) { if (a > b) a = a - b; else b = b - a; return a; (* OCaml *) let rec gcd a b = if a = b then a else if a > b

int gcd(int a, int b) { // C while (a != b) { if (a > b) a = a - b; else b = b - a; return a; (* OCaml *) let rec gcd a b = if a = b then a else if a > b then gcd b (a - b) else gcd a (b a) gcd (A, B,G) :- A = B, gcd (A,B,G) :- A > B, C is A-B, gcd(C,B,G). gcd (A,B,G) :- B > A, C is B-A, gcd(C,A,G). G = A. % Prolog %3D

Step by Step Solution

3.36 Rating (180 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

No it only works forwa... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!