The Josephus Flavius Problem

Total Page:16

File Type:pdf, Size:1020Kb

The Josephus Flavius Problem

Computer Science AP The Josephus Flavius Problem Josephus Flavius was a Jewish general in the Jewish-Roman War in the 1st century. During the war, he and forty of his soldiers became trapped in a cave by Roman soldiers. They decided to choose suicide over capture, fearing torture from their Roman enemies. They believed that actual suicide is a sin, so they decided stand in a circle, pick a starting spot, and that every third man will be killed by the man nearest him. Perhaps because Josephus was a scholar, he may have been able to mentally calculate what place to stand in the circle to be the lone survivor! Assignment Create a program that is given two integers from the user: one for the number of people in the circle, and one for the skip number. The program should then calculate and print out what position in the circle you should stand in to be the last man standing. Example: In a circle of 10 people where every 3rd is killed, you want to be standing in the 4th spot! How to Calculate the Spot by Hand The only way to know what spot you should be standing in is to run a simulation of the game and see which spot is the survivor is in. You can play the game quickly with pencil and paper, by simply drawing tallies for the number of people in the game and then beginning to slash every nth tally until there is only one left. The answer to the problem is the final remaining tallies index number. How to Write a Program to Calculate the Spot An easy way to solve this problem is to create a boolean array called “people”. Assign every value in the array “true” so that everyone is alive, and then begin the game by looping through the elements and marking every nth element as “false”. Stop the game once there is only one true left in the array and return that index! Suggested Breakdown I suggest breaking up the problem into the following sub-problems: 1. Use the scanner class to input the number of people present and the skip number. 2. Create a boolean array using the number of people present as the size. 3. Assign every value in the array as true so that everyone is alive. 4. Write a while loop with the condition that more than 1 person is still alive. 5. Inside of the loop, keep track of the current place, and jump ahead using the skip number. 6. Don’t forget to wrap around the array or to skip over marked tallies when necessary. 7. Assign that location to false and let the loop continue. 8. Once the loop is finished, return the location of the only true value inside of the array. Grading and Submission 25 Points: Use the scanner class to receive two integers from the terminal. 25 Points: Create an appropriately sized boolean array and assign all of the values as true. 50 Points: Calculate and print the index of the lone survivor based on the input numbers. Bonus 10 Points: If your best friend was playing what position should he stand at? Print that out too!

How to Submit: Send me your source code file named: “Lastname_JosephusArray.java”

Recommended publications