Results 1 to 9 of 9
Thread: Josephu's Circle
- 08-01-2010, 04:49 AM #1
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
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
- 08-01-2010, 05:01 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you really want to know? Do you have any errors, or you are looking for an alternative?
- 08-01-2010, 05:28 AM #3
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
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.
- 08-01-2010, 07:28 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Here's a different approach:
kind regards,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); } } }
Jos
- 08-01-2010, 08:12 AM #5
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
- 08-01-2010, 08:30 AM #6
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
- 08-01-2010, 08:32 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 08-01-2010, 08:38 AM #8
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
- 08-01-2010, 08:44 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Circle arrays
By n00b in forum New To JavaReplies: 15Last Post: 05-05-2010, 05:04 PM -
Circle and line
By c_walker in forum New To JavaReplies: 1Last Post: 01-27-2010, 03:56 AM -
how to add a circle or rectangle in a jtextarea
By nicnicnic in forum Java 2DReplies: 10Last Post: 10-17-2009, 04:09 PM -
drawing a dashed circle
By Alarmmy in forum SWT / JFaceReplies: 0Last Post: 07-13-2009, 09:46 AM -
In which circle is the Point lying?
By nidhirastogi in forum New To JavaReplies: 1Last Post: 07-02-2008, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks