%%%----------------------------------------------------------------
%%% @author Genesislive <genesislive@126.com>
%%% [https://]
%%% @copyright 2013
%%% @doc josephus problem
%%% @end
%%%----------------------------------------------------------------
-module(josephus).
-export([josephus_it/2, josephus/2]).
%%-----------------------------------------------------------------
%% @doc solve josephus problem
%% fun(1) = 0, 一个人;
%% fun(n) = (fun(n - 1) + m) % n, n个人
%% @spec josephus_it(N, M) -> integer()
%% where
%% N:integer() number of people
%% M:integer() mth people
%% @end
%%-----------------------------------------------------------------
josephus_it(1, _) -> 0;
josephus_it(N, M) ->
(josephus_it(N - 1, M) + M) rem N.
%%-----------------------------------------------------------------
%% @doc
%% @end
%%-----------------------------------------------------------------
josephus(N, M) ->
lists:foldl(fun(I, S) ->
% io:format("~p~n", [((S + M) rem I)]),
((S + M) rem I)
end, 0, lists:seq(2, N)).