Results 1 to 9 of 9
  1. #1
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default Josephu's Circle

    Here is my attempt at Josephu's Circle problem. Curious to see if anyone had a different approach.

    Best Regards.
    PK

    Java Code:
    /*
     * Titus Flavius Josephus was an important first-century historian.
     * Having survived the siege and destruction of Jerusalem in 70 AD,
     * he authored several works on Jewish history, including The Jewish War
     * and Antiquities of the Jews. Not only have his writings given valuable
     * insight into first century Judaism, they provide an extra-Biblical
     * account of early Christianity. But aside from the books, and the writings,
     * and all of his other invaluable contributions to history, Josephus also told
     * the story of how he had escaped death by quickly standing in the "safe spot"
     * of what is now called Josephus’ Circle.
    
     * As the legend goes, Josephus and forty of his fellow soldiers retreated
     * from the siege of Yodfat to a small cave in the hills.
     * Surrounded by the Roman legion with no chance of escape, the soldiers saw
     * no other choice but to commit suicide. Like the people of Masada,
     * they would rather die than face capture.
    
     * In order to decide who would die in which order,
     * the soldiers stood in a circle and,
     * starting with the top of the circle and continuing clockwise, counted to three.
     * The third man got the ax and the counting resumed at one.
     * The process continued until there was no one left.
     * Josephus, who didn't quite agree with
     * the whole "we should all kill ourselves" idea,
     * figured out the perfect way to avoid death: be the last man standing.
     *
     * Your challenge: write the Josephus function.
     * There should be two inputs (number of soldiers, soldiers to skip)
     * and one output (index of the safe spot).
     *
     * Original Post:
     * http://thedailywtf.com/Articles/Programming-Praxis-Josephus-Circle.aspx
     */
    
    package JosephusCircle;
    import java.util.Scanner;
    
    public class  JosephusCircle{
    
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
    
            //Initialize variables
            int solders, counting_index, numAlive;
            int counter = 0;
            boolean lastOneStanding = false;
    
            //Input Values
            System.out.print("Please Enter the Number of Soilders: ");
            numAlive = solders = console.nextInt();
            System.out.print("Please Enter How Many to skip: ");
            counting_index = console.nextInt();
    
            //Initialize array, solders alive are considered "True"
            boolean unit[] = new boolean[solders];
            for (int i = 0; i < unit.length; i++){
                unit[i] = true;
            }
    
    
            //loop iterates while lastOneStanding = false
            while(!lastOneStanding){
                  // iterate through the array
                  for( int i = 0 ; i < unit.length; i++){
                    //Check if solder at position i is alive
                    if (unit[i]){
                        counter++;
                        // counter == counting index kill that soldier
                        if (counter == counting_index){
                            counter = 1;
                            numAlive--;
                            unit[i] = false;
                        }
                    }
                }
                //if numAlive == 1 set lastOneStanding to true to exit loop
                if (numAlive == 1){
                    lastOneStanding = true;
                }
            }
    
            //Find location for last living solder
            int i = 0;
            for ( ; !unit[i]; i++ ){}
            System.out.print("Best Location to Stand: " + (i + 1) + "\n");
        }
    
    }
    Last edited by Pyrexkidd; 08-01-2010 at 04:59 AM. Reason: comments

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    No, No errors.

    Just curious if any one had a more elegant and/or efficient way of implementing this.

    I kinda have a brute force method of coding and would like to improve my code.

    So really I'm looking for ways to improve my code.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Pyrexkidd View Post
    Here is my attempt at Josephu's Circle problem. Curious to see if anyone had a different approach.
    Here's a different approach:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class T {
    
    	public static void main(String[] args) throws Exception {
    
    		List<Integer> people= new ArrayList<Integer>();
    		
    		for (int i= 0; i < 41; i++)
    			people.add(i);
    		
    		int axe= 0;
    		while (people.size() > 1)  {
    			axe+= 3;
    			axe%= people.size();
    			people.remove(axe);
    			System.out.println(people);
    		}
    		
    	}
    }
    kind regards,

    Jos

  5. #5
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Try this. It's practically the same as Jos' version, but a little more object-oriented and flexible.

    Java Code:
    import java.util.ArrayList;
    
    public class JosephusCircle {
        public static void main(String[] args) {
    	int totalSoldiers = 0, numberToCount = 0;
    
    	try {
    		totalSoldiers = Integer.valueOf(args[0]);
    		numberToCount = Integer.valueOf(args[1]) - 1;
    	} catch (Exception ex) {
    		System.err.println("syntax: JosephusCircle [totalSoldiers] "
    				+ "[numberToSkip]");
    		System.exit(1);
    	}
    	
    	if (totalSoldiers < 1) {
    		System.err.println("There must be at least one soldier!");
    		System.exit(1);
    	}
    
    	if (numberToCount == 0) {
    		printSafeSpot(totalSoldiers);
    	}
    
    	ArrayList<DeadManWalking> soldiers =
    			new ArrayList<DeadManWalking>(totalSoldiers);
    
    	for (int spot = 1; spot <= totalSoldiers; spot++) {
    		soldiers.add(new DeadManWalking(spot));
    	}
    
    	int handOfDeath = 0;
    	while (soldiers.size() > 1) {
    		handOfDeath += numberToCount;
    		handOfDeath %= soldiers.size();
    		soldiers.remove(handOfDeath);
    	}
    
    	printSafeSpot(soldiers.get(0).getInitialSpot());
        }
    
        private static void printSafeSpot(int spot) {
    	System.out.format("Safe spot is position %d.%n", spot);
    	System.exit(0);
        }
    
    }
    
    class DeadManWalking {
        private int initialSpot;
    
        public DeadManWalking(int initialSpot) {
    	this.initialSpot = initialSpot;
        }
    
        public int getInitialSpot() {
    	return initialSpot;
        }
    }
    Last edited by rgrant222; 08-01-2010 at 08:16 AM. Reason: It's silly, but a lot of the code was incorrectly aligned!
    Learn something new... wiser-today.blogspot.com

  6. #6
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Also, I noticed that you specified that the first person in the circle should be counted as number one, and the third person should be the first to "get the axe." Thus using 3 as the number of people to skip results in the fourth person being the first one to die (0 + 3 = 3, which references the fourth element in the list), which is why handOfDeath is incremented by one less than the number of people to skip.
    Learn something new... wiser-today.blogspot.com

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rgrant222 View Post
    Try this. It's practically the same as Jos' version, but a little more object-oriented and flexible.
    What is the 'more object-oriented' part? I see a superfluous class for the rank of each soldier but ...

    kind regards,

    Jos

  8. #8
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    Yeah, that would be it. I did say "a little more," and you're right, it's arguably superfluous. Your solution was quite elegant! I didn't mean to give the impression that I thought negatively of it, so I'm sorry if I did.
    Learn something new... wiser-today.blogspot.com

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rgrant222 View Post
    Yeah, that would be it. I did say "a little more," and you're right, it's arguably superfluous. Your solution was quite elegant! I didn't mean to give the impression that I thought negatively of it, so I'm sorry if I did.
    Ok, now I understand; I simply stripped my solution down to the bare essentials (Occams razor); if I had some moving .gif images of exploding soldiers I might even have included a gui ;-)

    kind regards,

    Jos

Similar Threads

  1. Circle arrays
    By n00b in forum New To Java
    Replies: 15
    Last Post: 05-05-2010, 05:04 PM
  2. Circle and line
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:56 AM
  3. how to add a circle or rectangle in a jtextarea
    By nicnicnic in forum Java 2D
    Replies: 10
    Last Post: 10-17-2009, 04:09 PM
  4. drawing a dashed circle
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 09:46 AM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-02-2008, 11:12 PM

Tags for this Thread

Posting Permissions

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