Scheme: (gcd 14 30): 30 != 0, gcd 30 14 (gcd 30 14): 14 != 0, gcd 14 2 (gcd 14 2): 2 != 0, gcd 2 0 (gcd 2 0): 0 == 0, return 2 Prolog: gcd(14, 30, U) -> 30 != 0, not binding gcd(14, 30, X) -> 30 != 0 (good), Y is 14, gcd(30, 14, X) gcd(30, 14, U) -> 14 != 0, not binding gcd(30, 14, X) -> 14 != 0 (good), Y is 2, gcd(14, 2, X) gcd(14, 2, U) -> 2 != 0, not binding gcd(14, 2, X) -> 2 != 0 (good), Y is 0, gcd(2, 0, X) gcd(2, 0, U) -> 0 == 0, U = 2 so result is 2(Edited: 2021-08-25)
Define: (gcd 14 30) Since 30 != 0 (14 mod 30) = 14 Then (gcd 30 14) Since 14 != 0 Then (gcd 30 2) Since 2!= 0 Then (gcd 2 0) Since 0 == 0 Return 2
l? gcd(14 30 0) Since -30 != 0 (14 mod 30) = 14 Then gcd (30, 14, x) Since -14 != 0 Then gcd (14, 2, x) Since -2 != 0 Then gcd (2, 0, x) Since -0 == 0 Return 2
30 != 0 so recursive gcd(30, 14 mod 30)gcd(30, 14)
V = 30, doesn't = 0, first rule not satisfied Y=14 mod 30=14, |?-gcd(30, 14, X)
V = 0 = 0 so the first rule is satisfied and return U=2
--> gcd(30, 14)
(gcd 14 30) 30 = 0 is false. 14 modulo 30 = 14. Return (gcd 30 14) (gcd 30 14) 14 = 0 is false. 30 modulo 14 = 2. Return (gcd 14 2) (gcd 14 2) 2 = 0 is false. 14 modulo 2 = 0. Return (gcd 2 0) (gcd 2 0) 0 = 0 is true. Return 2.
gcd(14, 30, U) 30 = 0 is false. gcd(30, 14, x) not(30 = 0) is true. Y = 14 modulo 30 = 14. gcd(30, 14, x) gcd(30, 14, U) 14 = 0 is false. gcd(30, 14, x) not(14 = 0) is true. Y = 30 modulo 14 = 2. gcd(14, 2, x) gcd(14, 2, U) 2 = 0 is false. gcd(14, 2, x) not (2 = 0) is true. Y = 14 modulo 2 = 0. gcd(2, 0, x) gcd(2, 0, U) 0 = 0 is true. Return U = 2.
(define (gcd 14 30 ) (if (= 30 0) 14 (gcd 30 14) (gcd 14 16) (gcd 16 14) (gcd 14 2) (gcd 2 0)Prolog 1? - gcd(14, 30, U)
gcd(U, V, X) :-not(V=0), Y is U mod V gcd(14, 30, X) :-not(V=0), 14 is 14 mod 30 gcd(V, Y, X) = gcd (30, 14, X)
gcd(14,30). 14 = u and 30 = v v is not 0 so we pass gcd(30,modulo(14,30) 14 mod 30 = 14, so gcd(30,14) gcd(14, modulo(30,14) so gcd(14, 2) gcd(2, modulo(14,2)) so gcd(2,0) since v = 0, we return 2 which is the gcd of 14 and 30.
gcd(14,30,U) U=14, V=30, V is not 0 so go to next gcd(14,30,X) U=14, V=30, V is not 0 so we solve Y = 14 mod 30 = 14. gcd(30,14,X) gcd(30,14,X) 14!=0 so next, U=30, V=14, Y = 30 mod 14 = 2. gcd(14,2,X) gcd(14,2,X) 2!=0 so next, U=14, V=2, Y = 14 mod 2 = 0. gcd(2,0,X) gcd(2,0,X) since v = 0, result is 2 which is the gcd of 14 and 30.
14,30 30 14