• + 1 comment

    Get the error now (at least I hope) So I originally ended up with

    return (m  + s - 1) % n;
    

    And didn't understand what the error really was, it seemed right. So to help out others here's a quick explanation, if I got this right the issue is when (m + s - 1) % n evaluates to 0. The prisoners have a base of 1 not 0. Per the example you count 1 2 3 4 1 2 3 4, not 1 2 3 0 1 2 3 0, never is there a 0. Therefore % 4 returning 0 should actually still be 4, in the example, n.

    So given:

    return (m  + s - 1) % n || n;
    

    If (m + s - 1) % n evaluates to 0 we will be FALSEY so we use the || operator and return n instead (4 in the example case)