-2

I have written the following code:

nat(0).
nat(s(X)) :- nat(X).
divide(0,_,0).
divide(X,Y,D) :- X@<Y, D is 0.
divide(X,s(0),X).
divide(_,0,undefined) :- !.

Everything is right up to here. but what should i write, to calculate the division of two other naturals? for example

divide(s(s(s(s(s(s(s(s(0)))))))),s(s(0)),D).???
false
  • 10,264
  • 13
  • 101
  • 209
  • This is a follow-up of the PO question http://stackoverflow.com/questions/29995806/dividing-two-integers-with-an-alternative-way. It is a pity the advice I gave there was not considered and that someone else gave an answer here to something that most probably is homework. This does not help the PO in learning anything: books are full of examples. – migfilg May 04 '15 at 11:43
  • @migfilg: you can flag such a post as a duplicate – false May 04 '15 at 14:15
  • @false: I do have the close vote privilege but I do not see the *close* link in any answer; what should I do? – migfilg May 04 '15 at 16:25
  • @migfilg: (not sure, but I think you should) flag it as a duplicate – false May 04 '15 at 16:30
  • @false: you are right, the option is under *flag*; probably the help entry in http://stackoverflow.com/help/privileges/view-close-votes should be updated – migfilg May 04 '15 at 16:41
  • @migfilg: MSO, please – false May 04 '15 at 16:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76912/discussion-between-migfilg-and-false). – migfilg May 04 '15 at 16:47
  • So what if it's a homework question? Stackoverflow is not a learning resource. It's a place where you can find answers to questions. I was also under the impression that it was not obligatory that every user be aware of every question ever asked before answering. In case you can't catch my sarcasm, I disagree with your downvote of my answer (which is correct, by the way, as far as I know) on a disagreement you have with OP or a distaste for the question itself – vmg May 05 '15 at 04:13

2 Answers2

0

I think the easiest way would be to define subtraction and then apply it recursively with a counter. I'm not able to run this right now but I assume it'd look something like this:

nat(0).
nat(s(X)) :- nat(X).

divide(_,0,_):- !, fail.
divide(X,Y,R):-
    iter_subtract(X,Y,0,R).

iter_subtract(X,Y,N,N):- X@<Y, !.
iter_subtract(X,Y,C,R):-
    subtract(X,Y,N),
    D = s(C),
    iter_subtract(N,Y,D,R).

subtract(A,0,A):-!.
subtract(s(X),s(B),R):-
    subtract(X,B,R).
vmg
  • 4,176
  • 2
  • 20
  • 33
-2

you can easily divide two numbers as following:

mydiv(0,_,0).
mydiv(_,0,undefined):- !.
mydiv(X,Y,D) :- X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1  .

output:

?- mydiv(10,4,Q).
false.

?- mydiv(100,2,Q).
Q = 50 .

to run with debug info:

mydiv(0,_,0) :- write('mydiv(0,_,0)\n').
mydiv(_,0,undefined):- write('mydiv(_,0,undefined)\n'),!.
mydiv(X,Y,D) :- write('mydiv('),write(X) , write(','), write(Y) , write(',') , write(D) , write(')') , nl,
                X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1  .

output:

?- mydiv(20,2,Q).
mydiv(20,2,_G3941)
mydiv(18,2,_L1420)
mydiv(16,2,_L1435)
mydiv(14,2,_L1450)
mydiv(12,2,_L1465)
mydiv(10,2,_L1480)
mydiv(8,2,_L1495)
mydiv(6,2,_L1510)
mydiv(4,2,_L1525)
mydiv(2,2,_L1540)
mydiv(0,__,0)
Q = 10 .

with natural function:

natural(0).
natural(X) :- X < 0 , !, fail.
natural(X) :- Y is X - 1 , natural(Y).
mydiv(0,_,0) :- write('mydiv(0,_,0)\n').
mydiv(_,0,undefined):- write('mydiv(_,0,undefined)\n'),!.
mydiv(X,Y,D) :- write('mydiv('),write(X) , write(','), write(Y) , write(',') , write(D) , write(')') , nl,
                natural(X), natural(Y), X >= Y , Z is X - Y , mydiv(Z,Y,M) , D is M + 1  .
houssam
  • 1,823
  • 15
  • 27