mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Excercise 003
This commit is contained in:
parent
1cb2bacbbd
commit
625ee578b6
1 changed files with 36 additions and 0 deletions
36
e003.erl
Normal file
36
e003.erl
Normal file
|
@ -0,0 +1,36 @@
|
|||
% Find the largest prime factor of a composite number.
|
||||
|
||||
% The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
% What is the largest prime factor of the number 600851475143 ?
|
||||
|
||||
-module(e003).
|
||||
-export([
|
||||
main/1,
|
||||
pfactor/1
|
||||
]).
|
||||
|
||||
pfactor(N) ->
|
||||
pfactor_(N, []).
|
||||
pfactor_(N, F) ->
|
||||
Next = pfactor_next(N, 2),
|
||||
if
|
||||
Next == N ->
|
||||
[N | F];
|
||||
true ->
|
||||
pfactor_(trunc(N / Next), [Next | F])
|
||||
end.
|
||||
|
||||
pfactor_next(N, Factor) ->
|
||||
if
|
||||
Factor == N ->
|
||||
Factor;
|
||||
N rem Factor == 0 ->
|
||||
Factor;
|
||||
true ->
|
||||
pfactor_next(N, Factor + 1)
|
||||
end.
|
||||
|
||||
|
||||
main(_) ->
|
||||
io:format("Prime Factors of 13195: ~w~n", [pfactor(13195)]),
|
||||
io:format("Prime Factors of 600851475143: ~w~n", [pfactor(600851475143)]).
|
Loading…
Reference in a new issue