Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. All But One Shall Die!

All But One Shall Die!

Scheduled Pinned Locked Moved The Lounge
question
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nagy Vilmos

    Genghis Kahn, being in a good mood decides to spare one life of his 1,000 captives. He has them stand in a circle and runs a sword through every second man. If the first man is #1 [amongst Kahn's many crimes was a love for VB], which man will be left alive?

    speramus in juniperus

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    Khaaaaaaaan!

    The language is JavaScript. that of Mordor, which I will not utter here
    I hold an A-7 computer expert classification, Commodore. I'm well acquainted with Dr. Daystrom's theories and discoveries. The basic design of all our ship's computers are JavaScript.

    OriginalGriffO Richard DeemingR 2 Replies Last reply
    0
    • A Abhinav S

      The first man will be left standing.

      Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #7

      He will be first down in the fourth time around...

      speramus in juniperus

      A 1 Reply Last reply
      0
      • N Nagy Vilmos

        He will be first down in the fourth time around...

        speramus in juniperus

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #8

        Delving a little deeper... 1,3,5,7....99 1,5,9,13......97 1,9,17,25,33,41,49,57,65,73,81,89,97 9,25,41,57,73,89 9,41,73, 9,73 73 73 survives!

        Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

        N 1 Reply Last reply
        0
        • A Abhinav S

          Delving a little deeper... 1,3,5,7....99 1,5,9,13......97 1,9,17,25,33,41,49,57,65,73,81,89,97 9,25,41,57,73,89 9,41,73, 9,73 73 73 survives!

          Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #9

          You're in the right area [not]. If it was 100, then 73 would survive but there were 1,000. [clue] For smaller cases:

          Prisoners 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
          Survivor 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1 3 5 7 9

          There is a formula here... [fixed]

          speramus in juniperus

          Richard DeemingR 1 Reply Last reply
          0
          • L Lost User

            Khaaaaaaaan!

            The language is JavaScript. that of Mordor, which I will not utter here
            I hold an A-7 computer expert classification, Commodore. I'm well acquainted with Dr. Daystrom's theories and discoveries. The basic design of all our ship's computers are JavaScript.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #10

            "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee." Don't think Ahab was too happy really... :laugh:

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            J 1 Reply Last reply
            0
            • L Lost User

              Khaaaaaaaan!

              The language is JavaScript. that of Mordor, which I will not utter here
              I hold an A-7 computer expert classification, Commodore. I'm well acquainted with Dr. Daystrom's theories and discoveries. The basic design of all our ship's computers are JavaScript.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #11

              http://khanaas.com/kirk/vilmos/[^] :-D


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              N 1 Reply Last reply
              0
              • N Nagy Vilmos

                You're in the right area [not]. If it was 100, then 73 would survive but there were 1,000. [clue] For smaller cases:

                Prisoners 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
                Survivor 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1 3 5 7 9

                There is a formula here... [fixed]

                speramus in juniperus

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #12

                Are you sure your clue is right? For example, with 14 prisoners, how can #15 be the survivor? :confused:


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                N 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  http://khanaas.com/kirk/vilmos/[^] :-D


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  N Offline
                  N Offline
                  Nagy Vilmos
                  wrote on last edited by
                  #13

                  I'll see you and raise![^]

                  speramus in juniperus

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Doesn't that discriminate against people in wheelchairs?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #14

                    Pete O'Hanlon wrote:

                    Doesn't that discriminate against people in wheelchairs?

                    Not more than your average daily scrum :)

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Are you sure your clue is right? For example, with 14 prisoners, how can #15 be the survivor? :confused:


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      N Offline
                      N Offline
                      Nagy Vilmos
                      wrote on last edited by
                      #15

                      Small mistyping, I fix...

                      speramus in juniperus

                      Richard DeemingR 1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        Small mistyping, I fix...

                        speramus in juniperus

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #16

                        Brain's not working properly today - I can see the pattern, but I can't see the formula. Is the answer #977?


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        N 1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          Brain's not working properly today - I can see the pattern, but I can't see the formula. Is the answer #977?


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          N Offline
                          N Offline
                          Nagy Vilmos
                          wrote on last edited by
                          #17

                          Well done! For the number of prisoners P find N, the highest power of 2 less than or equal to P. The survivor S, is: S = (P-N) * 2 + 1

                          speramus in juniperus

                          Richard DeemingR 1 Reply Last reply
                          0
                          • N Nagy Vilmos

                            Well done! For the number of prisoners P find N, the highest power of 2 less than or equal to P. The survivor S, is: S = (P-N) * 2 + 1

                            speramus in juniperus

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #18

                            Ah! That's what I did. I was expecting a more mathematical formula than that. :)


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            E 1 Reply Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              Ah! That's what I did. I was expecting a more mathematical formula than that. :)


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              E Offline
                              E Offline
                              ednrg
                              wrote on last edited by
                              #19

                              This is nasty and sloppy, but it figures it out to be 505. Where did I got wrong (other than write some horrific code :laugh: )?

                              class Program
                              {
                                  static List captures = new List();
                              
                                  static void Main(string\[\] args)
                                  {
                                      PopulateCaptures();
                                      int survivor = FindLastOne();
                              
                                      Console.WriteLine("The last one is: {0}", survivor);
                              
                                      Console.Write("\\n\\nHit any key to continue...");
                                      Console.ReadKey();
                                  }
                              
                                  static void PopulateCaptures()
                                  {
                                      for (int i = 1; i <= 1000; i++)
                                          captures.Add(i);
                                  }
                              
                                  static int FindLastOne(int startingPosition = 0)
                                  {
                                      int retVal = -1;
                              
                                      List dead = new List();
                              
                                      if (captures.Count == 1)
                                      {
                                          retVal = captures\[0\];
                                          return retVal;
                                      }
                              
                                      if (startingPosition == 0)
                                          startingPosition = 2;
                              
                                      if (startingPosition == 1)
                                          captures.RemoveAt(0);
                              
                                      for (int i = 1; i <= captures.Count; i++)
                                      {
                                          if (i % 2 == 0)
                                          {
                                              dead.Add(captures\[i - 1\]);
                                          }
                                      }
                              
                                      foreach (int i in dead)
                                      {
                                          captures.Remove(i);
                                      }
                              
                                      if (captures.Count % 2 != 0)
                                          startingPosition = 1;
                              
                                      if (captures.Count == 1)
                                          retVal = captures\[0\];
                                      else
                                          retVal = FindLastOne(startingPosition);
                              
                                      return retVal;
                                  }
                              }
                              
                              Richard DeemingR 1 Reply Last reply
                              0
                              • E ednrg

                                This is nasty and sloppy, but it figures it out to be 505. Where did I got wrong (other than write some horrific code :laugh: )?

                                class Program
                                {
                                    static List captures = new List();
                                
                                    static void Main(string\[\] args)
                                    {
                                        PopulateCaptures();
                                        int survivor = FindLastOne();
                                
                                        Console.WriteLine("The last one is: {0}", survivor);
                                
                                        Console.Write("\\n\\nHit any key to continue...");
                                        Console.ReadKey();
                                    }
                                
                                    static void PopulateCaptures()
                                    {
                                        for (int i = 1; i <= 1000; i++)
                                            captures.Add(i);
                                    }
                                
                                    static int FindLastOne(int startingPosition = 0)
                                    {
                                        int retVal = -1;
                                
                                        List dead = new List();
                                
                                        if (captures.Count == 1)
                                        {
                                            retVal = captures\[0\];
                                            return retVal;
                                        }
                                
                                        if (startingPosition == 0)
                                            startingPosition = 2;
                                
                                        if (startingPosition == 1)
                                            captures.RemoveAt(0);
                                
                                        for (int i = 1; i <= captures.Count; i++)
                                        {
                                            if (i % 2 == 0)
                                            {
                                                dead.Add(captures\[i - 1\]);
                                            }
                                        }
                                
                                        foreach (int i in dead)
                                        {
                                            captures.Remove(i);
                                        }
                                
                                        if (captures.Count % 2 != 0)
                                            startingPosition = 1;
                                
                                        if (captures.Count == 1)
                                            retVal = captures\[0\];
                                        else
                                            retVal = FindLastOne(startingPosition);
                                
                                        return retVal;
                                    }
                                }
                                
                                Richard DeemingR Offline
                                Richard DeemingR Offline
                                Richard Deeming
                                wrote on last edited by
                                #20

                                Try this:

                                int index = 1;
                                var prisoners = Enumerable.Range(1, 1000).ToList();
                                while (prisoners.Count != 1)
                                {
                                while (index < prisoners.Count)
                                {
                                prisoners.RemoveAt(index);

                                    // We've killed a prisoner, so the rest of the list has moved up one place.
                                    // Therefore, we only need to move to the next place to skip the next prisoner.
                                    index++;
                                }
                                
                                if (index == prisoners.Count + 1)
                                {
                                    // We killed the last one in the list; start the next round with the second in the list:
                                    index = 1;
                                }
                                else
                                {
                                    // We killed the second to last; start the next round with the first:
                                    index = 0;
                                }
                                

                                }

                                Console.WriteLine(prisoners[0]);


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                E 1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee." Don't think Ahab was too happy really... :laugh:

                                  J Offline
                                  J Offline
                                  JMK89
                                  wrote on last edited by
                                  #21

                                  OriginalGriff wrote:

                                  "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee."

                                  That quote always reminds me of the Simpsons!

                                  OriginalGriffO 1 Reply Last reply
                                  0
                                  • J JMK89

                                    OriginalGriff wrote:

                                    "...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee."

                                    That quote always reminds me of the Simpsons!

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #22

                                    Herman Melville for me, I'm afraid!

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    1 Reply Last reply
                                    0
                                    • Richard DeemingR Richard Deeming

                                      Try this:

                                      int index = 1;
                                      var prisoners = Enumerable.Range(1, 1000).ToList();
                                      while (prisoners.Count != 1)
                                      {
                                      while (index < prisoners.Count)
                                      {
                                      prisoners.RemoveAt(index);

                                          // We've killed a prisoner, so the rest of the list has moved up one place.
                                          // Therefore, we only need to move to the next place to skip the next prisoner.
                                          index++;
                                      }
                                      
                                      if (index == prisoners.Count + 1)
                                      {
                                          // We killed the last one in the list; start the next round with the second in the list:
                                          index = 1;
                                      }
                                      else
                                      {
                                          // We killed the second to last; start the next round with the first:
                                          index = 0;
                                      }
                                      

                                      }

                                      Console.WriteLine(prisoners[0]);


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      E Offline
                                      E Offline
                                      ednrg
                                      wrote on last edited by
                                      #23

                                      Found the error in my ways :) It now works.

                                         static int FindLastManStanding(int startingPosition = 2)
                                          {
                                              int retVal = -1;
                                              bool lastWasKilled = false;
                                              List dead = new List();
                                      
                                              if (startingPosition == 1)
                                                  captures.RemoveAt(0);
                                      
                                              int max = captures.Max();
                                      
                                              for (int i = 1; i <= captures.Count; i++)
                                              {
                                                  if (i % 2 == 0)
                                                  {
                                                      if ( max == captures\[i - 1\])
                                                          lastWasKilled = true;
                                      
                                                      dead.Add(captures\[i - 1\]);
                                                  }
                                              }
                                      
                                              dead.ForEach(x => captures.Remove(x));
                                      
                                              startingPosition = lastWasKilled ? 2 : 1;
                                      
                                              if (captures.Count == 1)
                                                  retVal = captures\[0\];
                                              else
                                                  retVal = FindLastManStanding(startingPosition);
                                      
                                              return retVal;
                                          }
                                      
                                      1 Reply Last reply
                                      0
                                      • N Nagy Vilmos

                                        Genghis Kahn, being in a good mood decides to spare one life of his 1,000 captives. He has them stand in a circle and runs a sword through every second man. If the first man is #1 [amongst Kahn's many crimes was a love for VB], which man will be left alive?

                                        speramus in juniperus

                                        E Offline
                                        E Offline
                                        ednrg
                                        wrote on last edited by
                                        #24

                                        Is this the shortest solution?

                                        var switcher = true;

                                        var range = Enumerable.Range(1,1000).ToList();

                                        while( range.Count > 1)
                                        range = range.Where(x => (switcher = !switcher) == false).ToList();

                                        range.Dump(); // (LinqPad)

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups