# Thread: Josephus Ring , need help understanding

1. Member
Join Date
Apr 2011
Posts
5
Rep Power
0

## 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

Java 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:)

2. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
9
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•