## Wednesday, August 27, 2014

### GuessTheNumber

Problem:
TopCoder Problem Statement - GuessTheNumber
Overview:

Determine the number of guesses it will take to arrive at the answer in the I'm Thinking of a Number game.

Java Source:
```01: import java.lang.Integer;
02:
03: public class GuessTheNumber {
04:
05:  public int noGuesses(int upper, int answer) {
06:
07:         int lower = 1;
08:         int numGuesses = 0;
09:         int guess = 0;
10:
11:         while (guess != answer) {
12:
13:             numGuesses++;
14:
15:             // guess = (upper + lower) / 2;  // Don't do This!!!
16:             guess = ((upper - lower) / 2) + lower;
17:             // System.out.println(guess);
18:
19:             if (guess < answer) {
20:                 lower = guess + 1;
21:
22:             } else if (guess > answer) {
23:                 upper = guess - 1;
24:             }
25:
26:         }
27:
28:         return numGuesses;
29:  }
30:
31:     public static void main(String[] args) {
32:         int upper = Integer.MAX_VALUE - 1;
33:         int answer = Integer.MAX_VALUE - 2;
34: