Josephus Problem

Certified Associate Developer

100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last? 
There are 100 people starting from 1 to 100. 

can anyone please tell me how can i do it?

  Discussion posts and replies are publicly visible

Parents
  • This is my example with a recursive rule to solve the puzzle (#73).

    As always, make sure to save recursive rules before testing.

    /* rule!chris_test_kc_recur(
          list (Number Array, 1+enumerate(100))
          index (Number, 1)
       )
    */
    
    if(
      count(ri!list)<=1,
      index(ri!list,1,null),
      a!localVariables(
        local!removeIndex: if(
          ri!index=count(ri!list),
          1,
          if(
            ri!index>count(ri!list),
            2,
            ri!index+1
          )
        ),
        local!list: remove(ri!list,local!removeIndex),
        rule!chris_test_kc_recur(
          list: local!list,
          index: local!removeIndex
        )
      )
    )

Reply
  • This is my example with a recursive rule to solve the puzzle (#73).

    As always, make sure to save recursive rules before testing.

    /* rule!chris_test_kc_recur(
          list (Number Array, 1+enumerate(100))
          index (Number, 1)
       )
    */
    
    if(
      count(ri!list)<=1,
      index(ri!list,1,null),
      a!localVariables(
        local!removeIndex: if(
          ri!index=count(ri!list),
          1,
          if(
            ri!index>count(ri!list),
            2,
            ri!index+1
          )
        ),
        local!list: remove(ri!list,local!removeIndex),
        rule!chris_test_kc_recur(
          list: local!list,
          index: local!removeIndex
        )
      )
    )

Children