Skip to main content

functions - Josephus problem


See the Josephus Problem. My code takes in two numbers; one is the number of participants, and the other is supposed to be the number of players skipped between executions.


josephus[m_Integer, n_Integer] :=
If[m == 1,
m,
Mod[josephus[m - 1, n] + n - 1, m] + 1]


The problem is that this code actually takes in the number of participants, and the other number n is actually killing the $n^{th}$ player. I don't want that; I want to skip $n$ players. (I.e., I want to kill every $(n+1)^{th}$ player.)


Why is it that replacing n in my code with n+1 does not correct the problem? It gives me totally different values. Since my code works fine if we are killing every $n^{th}$ player, shouldn't I just simply change n to n+1 and the code then kills every $(n+1)^{th}$ player?


Just so it helps, josephus[40,6] should return 24. josephus[40,5] should return 28. Note that currently, josephus[40,7] returns 24 and josephus[40,6] returns 28, which makes sense. The only difference is that, currently, we kill every $n^{th}$ player. I want to kill every $(n+1)^{th}$ player. Why is it that changing n to n+1 doesn't work?


Edit: the output represents the player that survives the game.




Comments