added prolog e3

This commit is contained in:
Dan Ballard 2011-10-21 12:49:42 -07:00
parent b9fa965567
commit 20a89cc870
1 changed files with 13 additions and 0 deletions

13
e3/e3.pl Normal file
View File

@ -0,0 +1,13 @@
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).