18 June 2009

Prime Number

Program to check whether a given number is prime or not.
---------------prime.erl---------------
-module(prime).
-export([start/1]).

start(Num) ->
prime(2, Num).

prime(_, 1) ->
io:format("Prime number!!!~n", []);
prime(Num, Num) ->
io:format("Prime number!!!~n", []);
prime(N, Num) ->
if((Num rem N) == 0) ->
io:format("Not a prime number!!!~n", []);
true ->
prime(N+1, Num)
end.
---------------------------------------
Save the code with the filename prime.erl
$ erl
Erlang (BEAM) emulator version 5.6.3.2 [source] [async-threads:0]
Eshell V5.6.3.2 (abort with ^G)
1> c(prime).
{ok,prime}
2> prime:start(53).
Prime number!!!
ok
3> prime:start(51).
Not a prime number!!!
ok

No comments:

Post a Comment