title: Factors of a number
body: <p>So I am relatively new to Prolog, and while this problem is easy in many other languages I am having a lot of trouble with it. I want to generate a List of factors for a number N. I have already built a predicate that tells me if a number is a factor:</p>
  
  <pre><code>% A divides B
  % A is a factor of B
  divides(A,B) :- A =\= 0, (B mod A) =:= 0.
  
  % special case where 1 // 2 would be 0
  factors(1,[1]) :- !.
  
  % general case
  factors(N,L):- N &gt; 0, factor_list(1, N, L).
  factor_list(S,E,L) :- S =&lt; E // 2, f_list(S,E,L).
  
  f_list(S,E,[])    :- S &gt; E // 2, !.
  f_list(S,E,[S|T]) :- divides(S,E), !, S1 is S+1, f_list(S1, E, T).
  f_list(S,E,L)     :- S1 is S+1, f_list(S1,E,L).
  </code></pre>
  
  <p>Any help would be appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I pretty much changed my entire solution, but for some reason predicates like <code>factors(9, [1])</code> return true, when I only want <code>factors(9, [1,3])</code> to return true. Any thoughts? </p>