[erlang-questions] learning Erlang: factoring primes
Martin Ankerl
martin.ankerl@REDACTED
Fri May 4 09:20:01 CEST 2007
Hello everybody, I have just started learning Erlang and wrote one of
my first programs. It is a very simple prime factoring program, but it
works. I would appreciate comments about the programming style, is
this how Erlang code should be written? Any way to make it simpler?
Here is the code:
-module(factor).
-export([fact/1]).
% c('factor').
% factor:fact(123456723456789).
fact(1) ->
[];
fact(N) ->
% io:format("~w\n", [N]),
M = next_factor(N, 2, N rem 2),
[ M | fact(N div M) ].
% find the next factor
next_factor(_, M, 0) ->
M;
next_factor(N, N, _) ->
N;
next_factor(N, M, _) ->
next_factor(N, M+1, N rem (M+1)).
--
Martin Ankerl | http://martin.ankerl.com
More information about the erlang-questions
mailing list