title: Guess the number: How many guesses for computer to get correct number?
body: <p>I'm very new to C++, and programming in general. I decided that I wanted to make a "Guess the Number" game, but I wanted see how many guesses on average it would take the computer to guess a number between 1 and 10,000,000.</p>
  
  <p>The easiest way that I could think of finding the "secret" number was to
  1. take the range and divide it by two(<strong>divisor</strong>) and that would be the guess.</p>
  
  <p>a. if the guess is greater than the "secret" number, then the guess-1 becomes the new maximum of the     range, and I go back to step 1.
  b. if the guess is lower than the "secret" number, then the guess+1 becomes the new minimum of the range and I go back to step 1.</p>
  
  <p>This repeats until the number is found. From my experience, it takes the computer 22 guesses to guess the "secret" number.</p>
  
  <p>For fun, I wanted to see what would happen if I changed the divisor. I was actually a bit surprised by the results for 1,000,000 iterations of trying to guess the number between 1 and 10,000,000 for a range of divisors from 2 to 10.</p>
  
  <pre><code>Average with divisor 2 is 22.3195
  Average with divisor 3 is 20.5549
  Average with divisor 4 is 20.9087
  Average with divisor 5 is 22.0998
  Average with divisor 6 is 23.1571
  Average with divisor 7 is 25.5232
  Average with divisor 8 is 25.927
  Average with divisor 9 is 27.1941
  Average with divisor 10 is 28.0839
  </code></pre>
  
  <p>I would love to understand why when the divisors 3, 4, and 5, are used the computer is able to, on average, use less guesses to find the "secret" number.</p>
  
  <p>My code is below. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cmath&gt;
  #include &lt;ctime&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int half_search(int a, int b, int n, int m)
  {
      int aMax = b;
      int aMin = a;
      int divisor = m;
      int result;
      static int counter = 0;
      double guess = ((static_cast&lt;double&gt;(b) - a) / divisor) + aMin;
  
      if(guess - static_cast&lt;int&gt;(guess) &gt;= 0.5)
          guess = ceil(guess);
  
      if(guess &lt; n)
      {
          aMin = guess + 1;
          counter++;
          half_search(aMin, aMax, n, divisor);
      }
      else if(guess &gt; n)
      {
          aMax = guess - 1;
          counter++;
          half_search(aMin, aMax, n, divisor);
      }
      else
      {
          counter++;
          result = counter;
          counter = 0;
          return result;
      }
  }
  
  int main()
  {
      const int MIN = 1;
      const int MAX = 10000000;
      int k = 0;
      int j = 2; //represents lowest divisor
      int l = 10; //represent highest divisor
      int iterations = 100000;
      double stepSum = 0;
      vector&lt;int&gt; myVector(iterations);
  
      srand(1);
      while(j &lt;=10)
      {
          while(k &lt; iterations)
          {
              int n = rand() % MAX + 1;
  
              myVector[k] = half_search(MIN, MAX, n, j);
  
              stepSum += myVector[k];
  
              k++;
          }
          cout &lt;&lt; "Average with divisor " &lt;&lt; j &lt;&lt; " is " &lt;&lt; stepSum / iterations &lt;&lt; endl;
          j++;
          k = 0;
          stepSum = 0;
      }
  
      return 0;
  }
  </code></pre>