I think the easiest way is still to write the repetition (pretty much what Wikipedia says, give upvotes to Jan Dvorak):
T(1) = 0 T(c) = (T(c-1)+n) mod c
Or, writing as C (without recursion):
int non_fatal_josephus(int children, int n) { int result = 0; for(int i=2; i<=children; i++) result = (result + n) % i; return result + 1;
Repetition Explanation:
T (c) means "which child will win if we start with c children."
T (1) = 0, because if we have only 1 child, she already won (1st child, index 0).
The general case is that we always exclude the nth child (index n-1), as soon as we eliminate it, we start counting again with (c-1) children, but this time instead of starting with index 0, we start with index n. This explains that + n: T (c-1) assumes that the count starts at 0. We use + n to offset the child index, as if we started with index n.
Juan lopes
source share