24 lines
678 B
Erlang
24 lines
678 B
Erlang
|
% Erlang version of a Prime Number Finder
|
||
|
% haplo@mindstab.net
|
||
|
-module(primes).
|
||
|
-author('haplo@mindstab.net').
|
||
|
-export([primes/1]).
|
||
|
|
||
|
check(I, Cnt, Max) when I < Max, (Cnt rem I) == 0 -> 0;
|
||
|
check(I, Cnt, Max) when I < Max -> check(I+2, Cnt, Max);
|
||
|
check(I, Cnt, Max) -> 1.
|
||
|
|
||
|
primes([Max]) -> primes(3, list_to_integer(atom_to_list(Max)), 2, 4);
|
||
|
primes(Max) -> primes(3, Max, 2, 4).
|
||
|
|
||
|
primes(Cnt, Max, Sq, Sqm) when Cnt < Max, Cnt >= Sqm ->
|
||
|
primes(Cnt, Max, Sq+1, (Sq+1)*(Sq+1));
|
||
|
primes(Cnt, Max, Sq, Sqm) when Cnt < Max ->
|
||
|
case check(3, Cnt, Sq) of
|
||
|
0 -> 0;
|
||
|
C -> io:format("~p\n", [Cnt])
|
||
|
end,
|
||
|
primes(Cnt+2, Max, Sq, Sqm)
|
||
|
;
|
||
|
primes(Cnt, Max, Sq, Sqm) -> init:stop().
|