Results 1 to 2 of 2
  1. #1
    trinity is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Exclamation 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. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    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.

Similar Threads

  1. Understanding this recursion
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 01-05-2011, 09:39 PM
  2. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  3. Replies: 5
    Last Post: 06-26-2009, 03:03 AM
  4. Ring Shaped Shell in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:28 PM
  5. Unclear Requirement from client ... Ring a bell?
    By galford23 in forum New To Java
    Replies: 7
    Last Post: 01-08-2008, 05:31 AM

Posting Permissions

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