Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,12 @@ echo "*------------ R -------------*"
sleep 5 # cpu cool down

Rscript ./src/prime.R
echo ""
echo ""

echo "*-------------- erlang ---------------*"
erl -eval 'erlang:display(erlang:system_info(otp_release)), halt().' -noshell
erlc -o src ./src/prime.erl
erl -pa src -noshell -s prime main -s init stop

rm ./src/prime.beam
echo ""
30 changes: 30 additions & 0 deletions src/prime.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-module(prime).
-export([is_prime/1, main/0]).

is_prime(N) when N =< 1 ->
false;
is_prime(N) ->
is_prime_helper(N, 2, round(math:sqrt(N))).

is_prime_helper(_, I, End) when I > End ->
true;
is_prime_helper(N, I, End) ->
case N rem I of
0 -> false;
_ -> is_prime_helper(N, I + 1, End)
end.

main() ->
Start = erlang:system_time(millisecond),
C = count_primes(0, 9000000, 0),
io:format("~p~n", [C]),
End = erlang:system_time(millisecond),
io:format("~pms~n", [End - Start]).

count_primes(I, Max, C) when I < Max ->
case is_prime(I) of
true -> count_primes(I + 1, Max, C + 1);
false -> count_primes(I + 1, Max, C)
end;
count_primes(_, _, C) ->
C.