Factors of an integer: Difference between revisions

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


:- func factor(int::in) = (list(int)::out) is det.
:- func factor(int::in) = (list(int)::out) is det.
factor(N) = Factors :- factor(N, Factors).
factor(N) = Factors :- factor(N, Factors).


:- end_module fac.</lang>
:- end_module fac.
</lang>


===Use and output===
===Use and output===