21

After every time you guess, you're told if you're right, too high, or too low. Is there a strategy you can use to guarantee you'll get it on your 6th attempt (or lower)?

I know a strategy to get it on your 7th attempt.

warspyking
  • 14,500
  • 10
  • 78
  • 144
  • http://stackoverflow.com/questions/9605110/guessing-a-number-knowing-only-if-the-number-proposed-is-lower-or-higher AND http://math.stackexchange.com/questions/98344/what-is-the-best-strategy-for-a-guess-my-number-game – d'alar'cop Oct 20 '14 at 22:02
  • Maybe you should open it up to other possibilities... like other questions besides guesses. that's more like us – d'alar'cop Oct 20 '14 at 22:03
  • Just to check, at each guess you're supposed to tell just one number? Or I can ask using a range of numbers? :) – woliveirajr Oct 21 '14 at 18:17
  • @wolive You're indirectly asking for a range. "50", "higher" it's NOT within the range 1-50 – warspyking Oct 21 '14 at 19:02
  • 2
    @warspyking: can I ask "it's between 33 and 66"? Answers: right, too low, too high... "It's between 11 and 22?" "It's between 4 and 8?" – woliveirajr Oct 21 '14 at 19:21
  • @wolive no, as the program that I use this strategy against don't have the ability to take a range for input. Although it would still be interesting to know how that would work, would that be worth another question? – warspyking Oct 21 '14 at 19:26
  • @warspyking: perhaps it deserves another question. :) The only way to get lower than 7 attempts would be like that. – woliveirajr Oct 21 '14 at 19:27
  • @wolive http://puzzling.stackexchange.com/questions/3124/6-attempts-to-guess-a-number-between-1-100-ranges – warspyking Oct 21 '14 at 19:40
  • 2
    It would have been amazing if somebody came up with a way to do this is < 7 attempts. It'd be like someone unknowingly proving that P=NP in an attempt to solve some silly riddle. The funny thing is that any CompSci PhD would immediately say "no, it's impossible" but an amatuer puzzle solver might try to come up with an answer... and maybe they would find one! Not knowing what is "impossible" can be really powerful. – Gray Oct 22 '14 at 18:56
  • 1
    @Gray I was thinking the same thing – d'alar'cop Oct 23 '14 at 12:32
  • 6
    @Gray This would do more than prove P=NP. It would violate the pigeonhole principle and thus prove large portions of math inconsistent. – Taemyr Nov 05 '14 at 14:19
  • considering the number is X is it allowed to ask * + - / operations ? for eg the number X*2 > 60 ? – Karan Thakkar Dec 04 '14 at 10:50
  • I can do it in one attempt, I am a very lucky person.. ;) – Amruth A Aug 27 '22 at 05:49

11 Answers11

34

Ask about 50.

  • If too high, then ask about 25.

  • If too low, then ask about 75.

Continue, continually halving the search-space. This requires $\lceil \log_2 (n+1)\rceil$ maximum questions. For 100, that's 7. It is a binary search algorithm known to be $\mathrm O(\log(n))$ time. I'm fairly sure there isn't a faster way. Binary search is considered the best for this problem - unless you are allowed to ask other questions.

d'alar'cop
  • 12,892
  • 4
  • 49
  • 90
  • That's my 7 strategy, I'm wondering if there's a faster way. – warspyking Oct 20 '14 at 21:13
  • Oh and the number is never a decimal so after it's too low for 25, then it'll be 12, or if it's too high it's be 38, the point is to make it even. – warspyking Oct 20 '14 at 21:15
  • @warspyking maybe we can... can we only ask if it's a certain number $x$? or can we ask anything? – d'alar'cop Oct 20 '14 at 21:28
  • You can only guess the number. – warspyking Oct 20 '14 at 21:56
  • @warspyking then we are doomed. but then look at Matt's answer, is that valid? – d'alar'cop Oct 20 '14 at 22:01
  • No, not really. This was more of a is-it-possible, if so how-to-do-it in which involves strategy, which fits into the puzzles category, thanks for the answers. – warspyking Oct 20 '14 at 22:11
  • Now should I accept yours or Doorknob's... – warspyking Oct 20 '14 at 22:14
  • @warspyking mine was first :p – d'alar'cop Oct 20 '14 at 22:17
  • Since you have to actually say the number, the least upper bound is $\lceil\log_2(n+1)\rceil$ attempts. For example, $n=2$ may requires two guesses. – Dennis Oct 21 '14 at 04:30
  • @Dennis thanks, are we sure it's not even $\lfloor log_2 (n)\rfloor + 1$ – d'alar'cop Oct 21 '14 at 04:32
  • 2
    That's the same bound. If $n=2^m$, $\lceil\log_2(n+1)\rceil=m+1=\log_2n+1$. If $n$ is not a power of $2$, $log_2n$ is not entire and $\lceil\log_2(n+1)\rceil=\lceil\log_2n\rceil=\lfloor\log_2n\rfloor+1$. – Dennis Oct 21 '14 at 04:46
  • @Dennis that's what I suspected. cheers – d'alar'cop Oct 21 '14 at 04:53
  • I don't understand how the $O$-notation is relevant here. Can somebody please explain that to me? – Twinkles Oct 21 '14 at 09:31
  • 3
    @Twinkles That's "Big-O" or "Landau" notation. It is used in Computer Science to express the "complexity" or computational cost of an algorithm. The reason it's used here is because it expresses that the algorithm is efficient and highlights that a search algorithm with better than that "efficiency"/"computational cost" is not known for this problem - that supports the argument of optimality of the solution. – d'alar'cop Oct 21 '14 at 09:41
  • "I'm fairly sure" isn't really a convincing argument and from O(.) you cannot conclude a lower bound. – miracle173 Nov 01 '14 at 07:38
  • 3
    @miracle173: Good points. But binary search is, in fact, provably optimal in the worst case (see http://stackoverflow.com/q/7578709/) – Nemo Dec 03 '14 at 21:55
  • @Nemo: That's not really new to me and I actually provided a proof for this: http://puzzling.stackexchange.com/a/3488/1967 . – miracle173 Dec 04 '14 at 04:39
  • @miracle173 "from O(.) you cannot conclude a lower bound."... ok, but if an algorithm has the lowest upper bound, then it's optimal – d'alar'cop Dec 04 '14 at 04:40
  • I am not sure if I understand your statement, but to say that for n=100 the value of O(n) is 7 is wrong. – miracle173 Dec 04 '14 at 04:50
  • @miracle173 the formula $\lceil log_2 (n+1) \rceil $ evaulates to $7$. in general, the algorithm provided has a complexity of $ O(log (n)) $ – d'alar'cop Dec 04 '14 at 04:54
  • $\lceil \log_2 (n+1) \rceil$ evaluates to 7, that is true. But you wrote $O(\log (n))$ evaluates to 7 for n=100, that is wrong. Also from the upper bound of the running time of the binary search algorithm one cannot conclude something about the lower bound of its running time. But you do. – miracle173 Dec 04 '14 at 05:14
  • @miracle173 for your first comment: OK, I just reordered the sentences. for your second: if I can establish an upper bound for an algorithm that is lower than every other one... then it is optimal – d'alar'cop Dec 04 '14 at 05:18
  • You can find it named as bisection method also. – Ognyan Dimitrov May 05 '16 at 11:23
28

Yes.

Each guess eliminates one number as well as dividing the remaining numbers into 2. One guess can pick a number from 3 (is your number 2?). 2 guesses can do 7. N guesses can pick a number from $2^{N+1}-1$, so 6 guesses can do it for 1-127.

Edit: As noted in the comments, you're supposed to have guessed the number on or before the 6th attempt, while this only ensures you know the answer by then.

frodoskywalker
  • 7,369
  • 1
  • 30
  • 49
  • 2
    good god you're right. – d'alar'cop Oct 20 '14 at 23:20
  • 8
    Six guesses may be enough to tell you what the number is, but knowing the number isn't enough. You still have to say it, which potentially counts as your 7th attempt. – cHao Oct 20 '14 at 23:34
  • @cHao Exactly why this has not been marked accepted answer ;D – warspyking Oct 20 '14 at 23:49
  • haha... I must be on 1st gear today :p – d'alar'cop Oct 21 '14 at 00:08
  • @warspyking atleast frodoskywalker told you "Yes" :) so +1 from me though in essence its almost the same answer – skv Oct 21 '14 at 05:25
  • @frodoskywalker I'm really struggling to see how one guess can let you know a number 1-3? "Is your number 2?" - if they say no, it's either 1 or 3 and you still don't know. – Señor O Oct 21 '14 at 17:52
  • @SeñorO, they don't say no. They say "too high" or "too low". – frodoskywalker Oct 21 '14 at 18:29
  • @frodoskywalker Ah. Simple. Got it. – Señor O Oct 21 '14 at 18:45
  • Unfortunately this little bit of extra information is swamped by the halving occurring in the binary search, and only affects the maximum number of guesses right at the second to last guess - far too late to remove a whole guess from the total number of guesses. It does affect the number of guesses for specific numbers, and brings down the average number of guesses over the entire set, but it doesn't alter the number of guesses in the worst case. – Adam Davis Oct 22 '14 at 15:46
  • 1
    @AdamDavis, what do you mean by "only affects the number of guesses ... far too late to remove a whole guess"? This method guarantees a correct guess 7 (and you're guaranteed to know the number after guess 6) vs binary search guaranteeing a correct guess 8 and letting you know after 7. – frodoskywalker Oct 22 '14 at 18:28
  • @frodoskywalker technically your answer is still a binary search, but a fine-tuned one. – justhalf Aug 28 '22 at 08:31
17

Totally ripping off Matt Malone's answer:

If you can ask any question about the number where "correct," "too high," or "too low" are valid answers, go with:

  1. If you translate the number into trinary, is the last digit a "1"?
  2. If you translate the number into trinary, is the second-to-last digit a "1"?
  3. If you translate the number into trinary, is the third digit from the right a "1"?
  4. If you translate the number into trinary, is the forth digit from the right a "1"?
  5. If you translate the number into trinary, is the fifth digit from the right a "1"?
  6. Is the number X?

For example, 100 in trinary would be 10201. The first five answers would be: "correct, too high, too low, too high, correct" which would tell me that the number is 100. That would be my final guess.

This works for any integer from 0 up to 242.

user3294068
  • 7,498
  • 23
  • 32
  • I have totally same answer, because correct, to high, to low are 3 different info, so trinary should be use. – jwall Oct 22 '14 at 01:28
  • 2
    I think the OP means 'guess a number', not 'is the following hypothesis correct, too high or too low' – Seb Oct 22 '14 at 17:03
11

A strategy that solves this in under 7 questions is impossible.

For each question, you only get one bit of data (too low, or too high). It's impossible to do this in 6 questions because if you enumerated all the possible outcomes (let's say "too low" is 0, and "too high" is 1):

Q1 Q2 Q3 Q4 Q5 Q6
0  0  0  0  0  0
0  0  0  0  0  1
0  0  0  0  1  0
0  0  0  0  1  1
0  0  0  1  0  0
0  0  0  1  0  1
etc.

there would only be $2^6=64$ possible outcomes, while there are 100 possible numbers.

Doorknob
  • 4,657
  • 5
  • 35
  • 50
  • 9
    Nitpick: you do get a little more than one bit of data. Besides "too low" and "too high", there's also "correct!". (That still doesn't reduce the search space enough in this case...but if the question were between 1 and 70, it could make all the difference.) – cHao Oct 20 '14 at 23:46
  • 6
    70 would be too much. 63 is the actual maximum. 1 question: 1 value. 2 questions: 3 values. 3 questions: 7 values, etc. You get 6 questions: 63 values. – Florian F Oct 20 '14 at 23:49
4

I think skywalker's answer is the one that the riddler seeks.

In his answer the notion of a 3rd outcome for a guess "that's the number" is being considered.

Consider the following sequences (Un) and (Vn)

Defined as

U(0) = 1

U(n+1) = 2*U(n) ("naive" answer without regard to the additionnal information)

and

V(0) = 1

V(n+1) = 2*V(n) + 1 (with regard to the additionnal information)

For the first items of the suite we have

  • n - U(n) - V(n)

  • 0 - 1 - 1

  • 1 - 2 - 3

  • 2 - 4 - 7

  • 3 - 8 - 15

  • 4 - 16 - 31

  • 5 - 32 - 63

  • 6 - 64 - 127

  • 7 - 128 - 255

As you can see the (Vn) suite is one step ahead of (Un) thanks to the additionnal info. I think this single step ahead is the reason why this answer is the one the riddler is looking for.

EDIT: It is true that you can "only" be certain of the number to be guessed after 6 question instead of giving the right solution on the 6th guess.

But the more naive approach actually leads to the conclusion hat you should be allowed to speak an 8th time to say the number you were certain of after the 7th guess.

(Sorry I wanted to comment on skywalker's answer but I don't have enough points for that)

3

Six question are not sufficient Player 1 one guesses a number between 1 and 100. Player 2 says some numbers and Player 1 answers with "lower", "higher" or "equal" if his number is lower, higher or equal to your number. We write down the

answers of player 1, 0 if he answers "lower", 1 if he answers "higher" and = if he answers "equal".

An Example: Assume his secret number is 23.

  1. We ask 60 and he answers "lower"
  2. We ask 20 and he answers "higher"
  3. We ask 25 and he answers "lower"
  4. We ask 23 and he answers "equal"

The string we write down is 010=.

These are the possible answers

=
0=
1=
00=
01=
10=
11=
000=
001=
010=
011=
100=
101=
110=
111=
0000=
0001=
0010=
0011=
0100=
0101=
0110=
0111=
1000=
1001=
1010=
1011=
1100=
1101=
1110=
1111=
00000=
00001=
00010=
00011=
00100=
00101=
00110=
00111=
01000=
01001=
01010=
01011=
01100=
01101=
01110=
01111=
10000=
10001=
10010=
10011=
10100=
10101=
10110=
10111=
11000=
11001=
11010=
11011=
11100=
11101=
11110=
11111=

If you have a strategy it produces exactly one answer string for every secret number. There are only 63 possible answer strings so there cannot be 100 possible secret numbers.

But six questions are sufficient for the first player to know the right answer

If you can pose one question, you can find the solution if the there are $3$ possible numbers ${1,2,3}$ if you ask for $2$. If the answer is 'yes' the number is $2$, if the answer is 'lower' the answer is $1$ and if the answer is 'higher' the number is $3$. We model this with (a so called binary tree)

  2
 / \
1   3

which can be written in as $1-2-3$ using less space and ask for the number in the middle. If we have two question we can use the model $(1-2-3)-4-(5-6-7)$ or

     4 
   /   \
  2     6
 / \   / \
1   3 7   8

We ask for 7 and if it is not 7 the remaining model is $1-2-3$ or $5-6-7$. which can be solved after one question. So its immediately clear how to query and that there are 7 possible numbers. This can be continued, for 3 queries we have $((1-2-3)-4-(5-6-7))-8-((9-10-11)-12-(13-14-15))$. I will avoid the two dimensional graphic. So for k question we can differentiate between $N(k)=2 \cdot N(k-1)+1=2^{k+1}-1$ numbers. For $k=6$ we get $N(6)=127$ so we can differentiate between $127$ numbers. Therefore $6$ questions are sufficient for the numbers from $1$ to $100$. The first question always asks for the number $64$.

miracle173
  • 167
  • 9
2

If I am allowed to ask about individual digits, I can do it in 6.

First question: Where X is the number from 1 to 100 inclusive, if X - 1 is left padded with zeros (00-99), is the left digit 5?

If the answer is "lower", I ask about 2. If higher I ask about 7 or 8.

Say I asked about 2, and the answer again comes back "lower". Then I ask about zero and the answer comes back "higher". I've found the first digit to be 1 in three guesses.

I repeat the process to get the second digit in 3 guesses, for 6 total, max. But of course that's 6 questions, not 6 guesses as to the actual number.

Edit: Taking it a step further, asking about the left digit means asking about a 10 number range. If I get to ask about ranges in general, I push the number of questions down to five. Basically, I just split the 1-100 range into ranges of 33, 33, and 34 numbers and ask about the middle one. "Is the number between 34 and 66 inclusive?" So your ranges go from size 100 to 34 (in the worst case) to 12 to 4 to 2 to 1.

Matt Malone
  • 5,143
  • 3
  • 22
  • 34
  • 2
    Technically asking about the digits would still be 7 questions because even though you know the digits you have to still confirm the number is correct. Otherwise the binary search method would also give you the answer in 6 guesses. – kjbartel Oct 21 '14 at 04:22
  • Heck, you're right. – Matt Malone Oct 21 '14 at 14:15
1

If we assume that you can only ask the question, "Is the number XXX too high, or too low?", (for the purpose that "correct" will instantly let you know the answer), then there are three possibilities:

  1. Correct
  2. Too high
  3. Too low

Thus, if you had one guess, you can know the number in one guess if the number was between 1,2,3. (i.e. The probability space of the number is a set of at most 3 numbers) It follows that you can guess the number in two guesses.

If you had two guesses, the first guess could go three ways:

  1. Correct
  2. Too high
  3. Too low

If the first guess is too low or too high, there must be at most 3 numbers remaining for you to know the number in another guess. Thus, there can be at most 7 numbers.

Similarly, 15 numbers can be uniquely separated in 3 guesses, 31 in 4, 63 in 5, and 127 in 6. Thus it takes 6 guesses to know the number.

However, it will take 7 guesses to guess the number.

If you can ask questions such that when answered with "correct", there can still be possibilities (like in this answer), then 9 numbers can be uniquely separated in 3 guesses, and 5 guesses to know the number is sufficient as $3^5=243>100$.

If the question is to maximize the probability of guessing the number correct in 6 moves, the method is subject to change. (probably)

bobble
  • 10,245
  • 4
  • 32
  • 80
Number Basher
  • 364
  • 1
  • 19
1

Nobody quite has the right answer to why this is impossible in less than seven questions (assuming you're restricted to asking about single numbers).

Here is why. Whatever number you pick first, there is some chance that the answer will be on the "big" side; the smallest you can make the big side is 50. (E.g. if you guess "51" then "52-100" is the small side (49 numbers) and "1-50" is the big side (50 numbers). Ask again and your best worst case is 25. Then 12. Then 6. Then 3. You've just used 5 guesses, but you still have 3 numbers left to pick between. Can you always state the correct one? Nope! It could be any of those 3.

If you get one more guess you can get the worst case down to 1 number, which means you've got it. So this proves (well, illustrates, as this isn't a formal proof) that it is impossible in 6 guesses but is possible in 7.

(Note that, if you allow guesses at ranges, the answer is different. You can, by carefully selecting your ranges, go from 100 to 34 at worst (by stating "the number is between 34 and 67") on your first go. Second gets you down to 12. Then 4, then 2, then 1. So you can always do it in 6.)

Rex Kerr
  • 909
  • 1
  • 5
  • 10
0

I think you can do it with 6.72 on average. Start with the above 50 question, then split again with the above 25 question. Now, if it less than 25, 'ask is it 16 or under?'. If it is then it takes 4 more guesses (2^4=16). If it is above you have 9 possibilities. 7 of these take 3 guesses (for example 'if it is less than or equal to 20?' and we know it is more than 16, then that's 2 more guesses, so 3 in total). And 2 of them take 4 guesses (for example, 'is it greater than 23', still leaves 23 and 24).

We then do the same for the branches between 26 and 50, 51 and 75 and 76 and 100.

So to sum up, we have 7 * 16 * 4+ 6 * 7 * 4+ 7 * 2 * 4 all divided by 100 gives 6.72 questions on average.

  • 1
    This appears to be the same method as proposed before in other answers, and the same overall conclusion ("not possible in 6 moves") – bobble Aug 27 '22 at 01:23
  • It is a similar method, but I couldn't find any of the above answers which had worked out the average guess or thought about the details to get that as low as possible. But maybe I missed something. – David Sumpter Aug 28 '22 at 06:09
-1

(Lateral thinking)

I have a strategy that guarantees guessing the number in 3 guesses:

First guess 11
    Higher -> second guess 100
    Lower  -> second guess 10
        Lower -> third guess 1

The question didn't mention the number base so I proclaim that we are using binary numbers.

JS1
  • 17,874
  • 3
  • 52
  • 103