0

How can using dividing between two numbers by using subtraction instead of (/). This is my code to calculate mod by using subtraction:

predicates
    div(integer,integer,integer)
clauses
    div(X,Y,Z):-X>Y,X1=X-1,div(X1,Y,Z),!.
    div(X,Y,Z):-X<y,X=Z,!.
    div(X,Y,Z):-X=Y,Z=0,!.

i solve it

predicates

div(integer,integer,integer).

clauses
div(1,_,0):-!.
div(0,_,0):-!.
div(X,Y,M):- X1=X-Y,div(X1,Y,M1),M=M1+1.
false
  • 10,264
  • 13
  • 101
  • 209
eleen
  • 191
  • 1
  • 4
  • 11
  • Are you wanting to do mod or division? Your question says division, but you're saying your example code is mod. Also, can out explain, in common language, what each of your predicate means and what each of your clauses means? I think it's unclear. But I think it has been basically solved here, even though nothing was marked as a "solution": http://stackoverflow.com/questions/19954314/divison-and-remainder-in-prolog – lurker Nov 16 '13 at 14:47
  • no,i want Instead of using 4/2=2 i want use like this 4-2=2, 2-2=0.by using sub – eleen Nov 16 '13 at 14:51
  • can u help me please?? – eleen Nov 16 '13 at 14:54
  • And for 11/4 you want 2? Look at the problem/answers I linked and see if that helps. It has the ideas that you need. – lurker Nov 16 '13 at 15:06
  • it is very difficult i want to send (4,2) the out put is 2 or(11,4)=2 – eleen Nov 16 '13 at 15:42
  • You only need to use the `divit` predicate defined in the link I show but remove or ignore the remainder. Give it a try. :) – lurker Nov 16 '13 at 15:44
  • i cant please if u can help me because my exam tomorrow – eleen Nov 16 '13 at 16:48
  • You cannot assign an arithmetic expression with `=`, you need to use `is`. For example, `X1 is X - Y`. Good show on your solution; it is in the right direction! It works if the remainder of `X` divided by `Y` is `0` or `1` but it gives a stack overflow otherwise (e.g., try `div(10, 4, Q)`. You need a clause that just checks for `X < Y` and provides `X` as the quotient (just like the one you had in your first try). – lurker Nov 16 '13 at 21:30

0 Answers0