Skip to main content
1 of 6
xyz
  • 655
  • 4
  • 38
  • 117

How to correct my code in a problem?

Problem Decription

Recently,I have been reading the book that called 《Schaum_s Outline of Mathematica (2nd Edition)》,I encounted the problem:

Flavius Joseph was a Jewish historian of the first century. He wrote about a group of ten Jews in a cave who, rather than surrender to the Romans, chose to commit suicide, one by one. They formed a circle and every other one was killed. Who was the lone survivor?

The author's solution:

list = Range[10];
While[Length[list] > 1, list = Rest[RotateLeft[list]]];
list

{5}

However,I know it is not efficient to use the procedure method such as ,,etc.So I want use the command like NestWhile,Nest,Fixedpoint to solve it.

My solutions:

Method 1:

list=Range@10 
NestList[Rest@RotateLeft[#] &, list, 9]

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {3, 4, 5, 6, 7, 8, 9, 10, 1}, {5, 6, 7, 8, 9, 10, 1, 3}, {7, 8, 9, 10, 1, 3, 5}, {9, 10, 1, 3, 5, 7}, {1, 3, 5, 7, 9}, {5, 7, 9, 1}, {9, 1, 5}, {5, 9}, {5}}

Furthermore,this method has a flaw that I must give the the number of iterations.

Method 2:

FixedPoint[If[Length@# != 1 &, Rest@RotateLeft[#] &], list]
xyz
  • 655
  • 4
  • 38
  • 117