Erlang
15 Jan 2010
 
 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-module(simple).
-export([collect_simple/1]).
try_div(_,[]) -> true;
try_div(N,_) when N>0, N<3 -> true;
try_div(N,[1|T]) -> try_div (N, T);
try_div(N,[H|T]) ->
Lim = 0.1+math:sqrt(N),
if
H>Lim -> true;
N rem H == 0 -> false;
true -> try_div (N,T)
end.
find_simple (I, N, Prev) when I > N -> Prev;
find_simple (I, N, Prev) ->
DivRes=try_div (I,Prev),
if
DivRes -> NewPrev=lists:append(Prev,[I]);
true -> NewPrev = Prev
end,
find_simple (I+1, N, NewPrev).
collect_simple(N) when N<1 -> [];
collect_simple(N) -> find_simple (1, N, []).