Hello, I need some help please..

I need to write a recursive predicate in Prolog that subtracts two numbers until number 2 is higher than the result.

I have completed the logic in C++:

#include <iostream>

using namespace std;
int mod3(int a, int b)
	int intResult = a-b;

	if(intResult > b)
		return mod3(intResult, b);

	return intResult;
int main(int argc, char *argv[]) {
	cout << mod3(23, 5);
	return 0;

BUT putting it into Prolog is doing my brain in.. This is what I've got so far:

modulo(X, M, Y) :-

Y is (X-M),
(Y > M),
modulo(X, M, Y).

Could someone give me some help/pointers on what I'm doing wrong?

Thanks :)

Without actually running it, looks infinite recursion as last predicate is same as lhs of the rule Put another variable there, same variable can not unify with two values (forget assignment same varible name means exactly same value in Prolog) actually thinking about it the last statement is junk, remove it. Only add ground rule when Y <= M

Edited 4 Years Ago by pyTony: n/a

Hello thanks for your reply.

Do you mean something like this:

checkNum(Num1, Num2) :-
    Num1 > Num2.

modulo(A, B, Result) :-

    Result is (A-B),
    checkNum(Result, B),


Result = A-B,
check to see if the result is more than be,

I think you have to put result back modulo, because it takes 5 from the result..

Basic logical definition of mod/3 is

mod(X, Y, X) :- X < Y.
mod(X, Y, Z) :- plus(X1, Y, X),  mod(X1, Y, Z).

Edited 4 Years Ago by pyTony: n/a


Thanks for the reply.

This code:

mod(X, Y, X) :- X < Y.
mod(X, Y, Z) :- plus(X1, Y, X),  mod(X1, Y, Z).

Would I need another predicate called Plus? Wouldn't that just a infinite loop (so to speak).

Edited 4 Years Ago by phorce: n/a

This question has already been answered. Start a new discussion instead.