Skip to main content
Known Participant
Aug 9, 2023 11:11 AM UTC
Question

Josephus Problem

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?

    15 replies

    richardm900458
    Inspiring
    Aug 9, 2023 11:14 AM UTC

    Thats no appian related question, is it ;)?

    Known Participant
    Aug 9, 2023 11:16 AM UTC

    No just wanted to try using appian. Whether is it possible using expressuin rules or not?

    stefanhelzle0001
    Brainy
    Aug 9, 2023 11:35 AM UTC

    You can implement recursive and iterative algorithms. So, the answer is: Yes!

    mathieud0001
    Genius
    Aug 9, 2023 11:46 AM UTC

    Is this some sort of exam question?

    Known Participant
    Aug 9, 2023 11:49 AM UTC

    Yeah its a puzzle problem. You can refer this - https://www.geeksforgeeks.org/puzzle-100-people-in-a-circle-with-gun-puzzle/

    i was just trying to implement using appian.

    csteward
    Brainy
    Aug 9, 2023 3:41 PM UTC

    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
        )
      )
    )

    Known Participant
    Aug 10, 2023 4:32 AM UTC

    can you please share me the code rule!chris_test_kc_recur? That would be helpful

    richardm900458
    Inspiring
    Aug 10, 2023 6:55 AM UTC

    why not trying yourself than getting it served already prepared? :) The question was , if it was possible. yes it is. :)

    jamesm4933
    Known Participant
    Aug 10, 2023 6:06 PM UTC

    It's called the Josephus Problem.  It was inspired by a story in "The Jewish War" by Flavius Josephus.

    Apparently, the solutions for n=2 up to group size 78 are sequence number A006257 in the On-line Encyclopedia of Integer Sequences

    Wikipedia

    The Jewish War

    nikhilcht188835
    New Participant
    Aug 11, 2023 5:33 AM UTC

    Here is the solution to your challenge. We have also added a feather so that the user may choose the number of players and who should start the game.

    reduce(
      rule!EI_subEliminateTask(list: _, value: _),
      {
        ldrop(
          enumerate(ri!players) + 1,
          ri!startPlayer - 1
        ),
        if(
          ri!startPlayer = 1,
          {},
          enumerate(ri!startPlayer - 1) + 1
        )
      },
      repeat(ri!players - 1, 0)
    )
    
    subrule: rule!EI_subEliminateTask(list: _, value: _),
    code: { ldrop(ri!list, 2), ri!list[1] }