Prolog - get the factors for a given number doesn't stop?

后端 未结 3 539
南方客
南方客 2021-01-13 16:21

I need to find the factors of a given number , e.g :

?- divisors2(40,R).
R = [40,20,10,8,5,4,2,1].

The code :

% get all t         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 17:06

    You have a bug here

    divisors1([H|T],S,X):-
       divisors1(T,W,X),
       Z is X mod H,
       Z==0,S=[H|W]. <=== here
    

    If Z is Zero then S = [H|W] else S = W.

提交回复
热议问题