% Enter your code here. Read input from STDIN. Print output to STDOUT % Your class should be named solution % https://www.hackerrank.com/contests/projecteuler/challenges/euler019 -module(solution). -export([main/0, prof/0]). %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Main %%%%%%%%%%%%%%%%%%%%%%%%%%%% main() -> Data = read_data(), Res = calculate(Data), output_data(Res), ok. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Profiling %%%%%%%%%%%%%%%%%%%%%%%%%%%% prof() -> eprof:start(), eprof:start_profiling([self()]), main2(), eprof:stop_profiling(), eprof:analyze(total). main2() -> StartInput = os:timestamp(), Data = read_data(), io:format("INPUT: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartInput)/1000000]), StartRes = os:timestamp(), Res = calculate(Data), io:format("CALCULATE: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartRes)/1000000]), StartOutput = os:timestamp(), output_data(Res), io:format("OUTPUT: total time taken ~p seconds~n", [timer:now_diff(os:timestamp(), StartOutput)/1000000]), ok. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Calculate %%%%%%%%%%%%%%%%%%%%%%%%%%%% calculate(Ts) -> lists:map( fun(T)-> NP = n_prime(lists:seq(2, T), ceiling(math:sqrt(T)), []), case ((length(NP) + 1) rem 2) of 0 -> "Alice"; 1 -> "Bob" end end, Ts). % Sieve of Eratosthenes % https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes n_prime(List = [I | _], Sqrt, Acc) when I >= Sqrt -> lists:reverse(Acc) ++ List; n_prime([I | Tail], Sqrt, Acc) when I < Sqrt -> NTail = % remove_multiples(I * I, I, Tail, []), lists:filter(fun(X) -> X rem I /= 0 end, Tail), % io:format("Prime: ~p\n", [{I, CN, NTail}]), n_prime(NTail, Sqrt, [I | Acc]); n_prime([], 1, []) -> []. % ********************************* % BEGIN Copied from https://erlangcentral.org/wiki/index.php?title=Floating_Point_Rounding % ********************************* ceiling(X) when X < 0 -> trunc(X); ceiling(X) -> T = trunc(X), case X - T == 0 of true -> T; false -> T + 1 end. % ********************************* % END Copied from https://erlangcentral.org/wiki/index.php?title=Floating_Point_Rounding % ********************************* %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Output %%%%%%%%%%%%%%%%%%%%%%%%%%%% output_data(Ds) -> lists:foreach( fun(D) -> io:format("~s\n", [D]) end, Ds). %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Input %%%%%%%%%%%%%%%%%%%%%%%%%%%% read_data() -> Binary = read(), Res = binary:split(Binary, [<<"\n">>], [global]), [_ | Tests] = [binary_to_list(R) || R <- Res], lists:map(fun str2int/1,  Tests). str2intlist(S) -> [str2int(T) || T <- string:tokens(S, " ")]. str2int(Str) -> element(1,string:to_integer(Str)). -define(BLK_SIZE, 16384). read() -> ok = io:setopts(standard_io, [binary]), read(<<>>). read(Acc) -> case file:read(standard_io, ?BLK_SIZE) of {ok, Data} -> read(<>); eof -> Acc end. ed(T) -> erlang:display(T).