Question
The Josephus problem is the following game: N people, numbered 1 to N , are sitting in a circle. Starting at person 1, a hot
The Josephus problem is the following game: N people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After M passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins. Thus, if M = 0 and N = 5, players are eliminated in order, and player 5 wins. If M = 1 and N = 5, the order of elimination is 2, 4, 1, 5.
Write a program to solve the Josephus problem for general values of M and N. Try to make your program as efficient as possible. Make sure you dispose of cells.
Hint:
Use the comments in the code as a guideline to add your code.
Testing examples:
If you have n=4, m=1, the order of the people picked should be: 2 4 3 1
If you have n=5, m=0, the order of the people picked should be: 1 2 3 4 5
If you have n=6, m=3, the order of the people picked should be: 4 2 1 3 6 5
If you have n=6, m=9, the order of the people picked should be: 4 2 1 3 6 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started