I wanted to make a predicate that returns a list of a number dividers. Example: 72 = 2*2*2*3*3.
prdel(A,[],_):-
A is 1.
prdel(P,[D|L],D):-
0 is mod(P,D),
P1 is P/D,
prdel(P1,L,D).
prdel(P,L,D):-
D1 is D+1,
prdel(P,L,D1).
This works and returns the right list. The problem is that it does not stop after that but returns the same list over and over again if I press space (I am sorry I don't know the term in English when you use the same predicate to get different answer). I want it to stop after the first time.
I tried to edit the last one like that,
prdel(P,L,D):-
D1 is D+1,
D1<P,
prdel(P,L,D1).
but now it returns only false and not the list.
EDIT:
I am looking for an answer without cut.
One problem in your code is that it keeps trying to divide the number
P
byD
even when it is clear that the division is not going to succeed becauseD
is too high. This letsD
"run away" without a limit.Adding a check for
D1
to be below or equal toP
fixes this problem:This produces all combinations of divisors, including non-prime ones (demo).
If you do not want that, add the condition that
mod(P,D) > 0
in the last clause:This produces only
[2, 2, 2, 3, 3]
(demo).