challenges/e3/e3.pl

14 lines
569 B
Prolog

append([],[],[]).
append([],[H|T], [H|Acc]) :- append([], T, Acc).
append([H|T], L, [H|Acc]) :- append(T, L, Acc).
factor(N, Divisor, [N]) :- N is 1; Divisor >= N - 1.
factor(N, Divisor, Res) :- 0 is N rem Divisor, N1 is N/Divisor, factor(N1, 2, N1F), factor(Divisor, 2, DF), append(N1F, DF, Res).
factor(N, Divisor, Res) :- N rem Divisor > 0, D2 is Divisor + 1, factor(N, D2, Res).
max([], TMax, TMax).
max([H|T], TMax, M) :- H > TMax, max(T, H, M).
max([H|T], TMax, M) :- max(T, TMax, M).
e3(Highest) :- factor(600851475143, 2, Factors), max(Factors, 0, Highest).