title: Prolog - get the factors for a given number doesn't stop?
body: <p>I need to find the factors of a given number , e.g : </p>
  
  <pre><code>?- divisors2(40,R).
  R = [40,20,10,8,5,4,2,1].
  </code></pre>
  
  <p>The code : </p>
  
  <pre><code>% get all the numbers between 1-X 
  range(I,I,[I]).
  range(I,K,[I|L]) :- I &lt; K, I1 is I + 1, range(I1,K,L).
  % calc the modulo of each element with the given number :
  % any x%y=0 would be considered as part of the answer 
  divisors1([],[],_).
  divisors1([H|T],S,X):-divisors1(T,W,X),Z is X mod H,Z==0,S=[H|W].
  divisors1([_|T],S,X):-divisors1(T,S,X).
  divisors2(X,Result) :-range(1,X,Result1),divisors1(Result1,Result,X).
  </code></pre>
  
  <p>But when I run <code>divisors2(40,RR).</code> I get infinite loop and nothing is presented to the screen.</p>
  
  <p>Why ? </p>
  
  <p>Regards</p>