-module(counter). -export([start/1]). start(StartNumber) -> io:format("Starting counter at ~w~n", [StartNumber]), {_, StartSecs, _} = now(), loop(StartNumber, 0, StartSecs). loop(Current, Count, Secs) -> receive {From, next} -> NextNum = Current + 1, From ! {nextnumber, NextNum}, {_, CurrentSecs, _} = now(), {NewSecs, NextCount} = check_timer(Secs, CurrentSecs, Count + 1), loop(NextNum, NextCount, NewSecs); {gotprime, Node, Prime} -> io:format("~w found that ~w was a prime.~n", [Node, Prime]); Any -> io:format("Received unknown: ~p~n", [Any]), loop(Current, Count, Secs) end. check_timer(Secs, CurrentSecs, Count) when CurrentSecs - Secs >= 10 -> PrimesPerSecond = Count/(CurrentSecs - Secs), PPSRound = round(PrimesPerSecond * 10)/10, %%PPSPerNode = PrimesPerSecond/length(nodes()), io:format("~n~p primes/s. (~p primes in ~p seconds with ~p nodes)~n", [PPSRound, Count, CurrentSecs - Secs, length(nodes())]), %%io:format("Nodes: ~w~n", [nodes()]), %%io:format("Rate is ~p primes per second~n", [Count/(CurrentSecs - Secs)]), %%io:format("Primes per second per node: ~p~n", [PPSPerNode]), {CurrentSecs, 0}; check_timer(Secs, _CurrentSecs, Count) -> {Secs, Count}.