История изменений
Исправление loz, (текущая версия) :
Надо параллельное решение еще
-module(moroz).
-compile([export_all]).
go(Workers, Start) when is_integer(Workers), is_integer(Start) ->
{NewInit, Size} = make_workers(Workers, Start),
main_loop(NewInit, Size).
main_loop(CurrentInit, Size) ->
receive
{stop, Pid} ->
To = CurrentInit+Size,
Pid ! {CurrentInit, To},
main_loop(To, Size);
{found, Hash, N} ->
io:format("hash found!!: ~p / ~p~n", [N, Hash]),
main_loop(CurrentInit, Size)
end.
make_workers(N, Start) -> make_workers(N, Start, 500000).
make_workers(N, Start, Size) ->
Init = lists:foldl(fun(_, Acc) ->
To = Acc+Size,
make_worker(Acc, To),
To
end, Start, lists:seq(0, N-1)),
{Init, Size}.
make_worker(Init, Stop) -> spawn(moroz, worker_init, [self(), Init, Stop]).
worker_init(Parent, Init, Stop) ->
%% io:format("worker started: ~p -> ~p~n", [Init, Stop]),
worker_loop(Parent, Init, Stop).
worker_loop(Parent, Stop, Stop) ->
Parent ! {stop, self()},
receive
{NewCurrent, NewStop} ->
%% io:format("new task: ~p -> ~p", [NewCurrent, NewStop]),
worker_loop(Parent, NewCurrent, NewStop)
end;
worker_loop(Parent, Current, Stop) ->
Hash = crypto:hash(md5, "moroz" ++ integer_to_list(Current)),
case binary:part(Hash, {0, 4}) of
<<0,0,0,X:8,_/binary>> when X < 16#10 ->
Parent ! {found, Hash, Current};
_ -> ok
end,
worker_loop(Parent, Current+1, Stop).
Исходная версия loz, :
Надо параллельное решение еще
-module(moroz).
-compile([export_all]).
go(Workers, Start) when is_integer(Workers), is_integer(Start) ->
{NewInit, Size} = make_workers(Workers, Start),
main_loop(NewInit, Size).
main_loop(CurrentInit, Size) ->
receive
stop ->
To = CurrentInit+Size,
make_worker(CurrentInit, To),
main_loop(To, Size);
{found, Hash, N} ->
io:format("hash found!!: ~p / ~p~n", [N, Hash]),
main_loop(CurrentInit, Size)
end.
make_workers(N, Start) -> make_workers(N, Start, 500000).
make_workers(N, Start, Size) ->
Init = lists:foldl(fun(_, Acc) ->
To = Acc+Size,
make_worker(Acc, To),
To
end, Start, lists:seq(0, N-1)),
{Init, Size}.
make_worker(Init, Stop) -> spawn(moroz, worker_init, [self(), Init, Stop]).
worker_init(Parent, Init, Stop) ->
%% io:format("worker started: ~p -> ~p~n", [Init, Stop]),
worker_loop(Parent, Init, Stop).
worker_loop(Parent, Stop, Stop) -> Parent ! stop;
worker_loop(Parent, Current, Stop) ->
Hash = crypto:hash(md5, "moroz" ++ integer_to_list(Current)),
case binary:part(Hash, {0, 4}) of
<<0,0,0,X:8,_/binary>> when X < 16#10 ->
Parent ! {found, Hash, Current};
_ -> ok
end,
worker_loop(Parent, Current+1, Stop).