Factors of an integer: Difference between revisions

m (→‎{{header|REXX}}: split a joined line. -- ~~~~)
Line 1,324:
factor(N, Factors) :-
Limit = float.truncate_to_int(math.sqrt(float(N))),
factor(N, 2, Limit, [], Unsorted),
list.sort_and_remove_dups([1, N | Unsorted], Factors).
:- pred factor(int::in, int::in, int::in, list(int)::in, list(int)::out) is det.
factor(N, X, Limit, !Accumulator) :-
( if X > Limit
then Accumulator = []true
else ( if 0 = N mod X
then !:Accumulator = [X, N / X | Remaining!.Accumulator]
else Accumulator = Remainingtrue ),
factor(N, X + 1, Limit, Remaining!Accumulator) ).
 
:- func factor(int::in) = (list(int)::out) is det.
factor(N) = Factors :- factor(N, Factors).
 
:- end_module fac.</lang>
</lang>
 
===Use and output===