# Josephus Ring , need help understanding

• 04-14-2011, 03:36 PM
trinity
Josephus Ring , need help understanding
Hi there I am working on the josephus ring:

The Josephus Ring Sequence is obtained by
.
removing the nth number remaining

in a ring of numbers 1..m. For example if n is 3 and M is 8 then the ring is:

1

8 2

7 3

6 4

5

the sequence is 1,4,7,3,8,6,2,5

Code:

```import tcdIO.*; public class Josephus {         public static void main(String arg[]) {                 int[] jo;                 int n;                 int m;                 Terminal t;                 t = new Terminal("");                 m = t.readInt("Enter the value for m: ");                 n = t.readInt("Enter the value for n: ");                 jo = new int[m];                 for (int i = 0; i < jo.length; i++) {                         jo[i] = i + 1;                 }                 for (int i = 0; i < jo.length; i++) {                         t.println("" + jo[i]);                 }                 int index = 0;                 for (int i = 0; i < jo.length; i++) {                         t.println("" + jo[index] + ",");                         jo[index] = 0;                         for (int j = 0; j < n; j++) {                                 index++;                                 index = index % m;                                 while (jo[index] == 0) {                                         index++;                                         index = index % m;                                 }                         }                 }         } }```
This the code i got , could anyone please help explaining the double for-loop and why you need to do modulo m ....:confused:
THANKS:)
• 04-14-2011, 04:54 PM
dlorde
I can't make sense of your 'explanation', but I guess the modulo is to set the index back to zero when it reaches m.