You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

23 lines
678 B

% 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().