The Josephus Problem

Math 213 - Math with Mathematica
Christopher Hanusa
Supplement to Chapter 1 of Discovering Mathematics with Mathematica by Robert Cowen and John Kennedy.

Your name goes here:

Aim

In this tutorial, we investigate the Josephus problem.  Follow along in your textbook in Chapter 1, up through and including page 12.

You will be required to submit this file as your second homework assignment.  Make sure to include your name above.

Page 4: List commands

Here you are introduced to the RotateLeft command (there is an analagous RotateRight command)  What does RotateLeft do?  Try the examples from the book.

Next you are introduced to the Rest command.  The family of commands that are related to Rest are:
Rest removes the first element of the list.
Most removes the last element of the list.
Drop is the more general version of this: it removes specified elements of lists.
First outputs the first element of the list.
Last outputs the last element of the list.
Part outputs specified elements of lists.
For more information, see “tutorial/GettingPiecesOfLists” in the Documentation Center.
Try the examples from the book.

Page 4: Defining functions

The next part of Page 4 is very important, it is where you learn to create your own functions!
The function f is defined as follows:

f[x_]:=Rest[RotateLeft[x]]

Notice that when defining a function, you have to use the := operator.  You also specify what the inputs are (here we take in a list x), and use an underscore (_) to tell Mathematica that the input can be anything.  (We will talk more about this underscore and how it relates to matching patterns later in the course.)  You also can see that Mathematica colors x to be greenish, and that in the calculations that occur on the right-hand-side of the := operator, all instances of x are highlighted in this same greenish color.  [It is different from the teal color that Mathematica uses to highlight a variable that varies when using Table, or Plot.]

Page 5: Nest and NestList

Learn more about the Nest and NestList functions by playing around and consulting the Documentation Center.

Define the survivor function here.

Page 6: Putting it all together

Follow the instructions on Page 6, but use the Flatten command properly to get output that is a list of coordinates.
In other words, we want our output to look like 
{{4,1},{5,3},{6,5},...
instead of 
{{4,{1}},{5,{3}},{6,{5}},...

Define your list to have a #descriptiveVariableName.
Use the TableForm command to output the list in a readable format.  
Use the ListPlot command to view the list visually.

HW 2-1: Complete Exercise 1.1 here.

Complete Exercise 1.1 on page 7.  Write a paragraph explaining the patterns you see in the output of the survivor function.

HW 2-2: Complete Exercise 1.2 here.

Complete Exercise 1.2 on page 7. Use a Table command to vary m and n at the same time to look for patterns.  Write a paragraph explaining the patterns you see.

HW 2-3: Complete Exercise 1.4 here.

Write a paragraph explaining how you went about finding the Josephus survivor with an initial circle of 2000 people.

Page 9: Josephus survivor pattern algorithm

Try to understand how the function at the top of page 9 works.

Key idea: Module is useful when you need your user-defined function to process multiple steps in a row and only give an output after all the steps are complete.

Key idea: the While construction is used here to run through all powers of 2 up to n.

Here is how the book calculates the largest power of 2 less than or equal to n:

bookPowerOfTwo[n_]:=Module[{b=1},While[2b≤n,b=2b];b]

Table[bookPowerOfTwo[n],{n,1,10}]

Here is how I would have calculated the largest power of 2 less than or equal to n:

I probably would have programmed this differently: using logarithms base 2.

closestPowerOfTwo[n_]:=2^(Floor[Log[2,n]])

Table[closestPowerOfTwo[n],{n,1,10}]

Compare the two survive functions here:

surviveB[n_]:=2(n-bookPowerOfTwo[n])+1

surviveC[n_]:=2(n-closestPowerOfTwo[n])+1

Calculation Time

When following along with the book up through page 12, you can ignore the part about removing the “Second” unit and in all the commands, you should remove the “/Second” part.

First[Timing[surviveB[2000]]]

First[{0.00006099999999999999872194247907408737`1.805929748290391,1953}]

HW 2-4: Page 12: Comparing computation time

On page 11, you have Mathematica gather the computation time for computing survivor[n] for different values of n, and defined that list as t1.
Now do the same for computing survive[n] and surviveC[n] as defined above, defining those lists as t2 and t3.

Next, use the ListPlot command to plot them each individually and then all together on the same set of axes in order to compare the computation time.

Write a paragraph explaining what you see, and what this tells you about the relative amount of computing power to use the different methods of calculating the answer to the Josephus problem.

Enrichment: Complete Exercise 1.3.

Complete Exercise 1.3 on page 7. Use a Table command to vary m and n and r at the same time to look for patterns.

Enrichment: Follow the remainder of Chapter 1.