**From New Scientist #2856, 17th March 2012** [link]

Three very logical friends, Amy, Bob and Carol, sat in a circle wearing hats. Each hat had a number on it so all three could see the others’ numbers but not their own. They were told that the numbers were three different positive digits.

They each made a statement in turn and were told to raise their hands when they knew their own number. Amy said: “Carol’s number is greater than Bob’s.” No one raised their hand.

Bob then said: “The sum of Amy’s and Carol’s numbers is even.” On hearing this, Carol raised her hand. Even then Amy and Bob did not raise theirs. But, after a pause, and once it had become clear that Amy wasn’t going to raise her hand, Bob raised his and then Amy raised hers.

What were Amy’s, Bob’s and Carol’s numbers?

[enigma1689]

### Like this:

Like Loading...

The following Python program runs in 37ms.

It accumulates the possibilities in various ways and eliminates options that don’t match the conditions of the problem, until we are left with the solution.

Solution:Amy’s number is 6. Bob’s number is 7. Carol’s number is 8.Great stuff. I just wonder in line 5, if the range is 1-9, I was calculating on one positive digit, as in the first paragraph of the question.

In Python the

range(a, b)function generates numbers fromatobbutnotincluding the endpoint. Sorange(1, 10)produces[1, 2, 3, 4, 5, 6, 7, 8, 9]. It’s handy when you’re indexing into an array of length n – you can just userange(n)– but at other times it can be annoying!This is one of the rare cases in which Perl is more readable than Python. The equivalent Perl expression is simply

1..9.In fact I’ve introduced an

irange()(inclusive range) function to theenigma.pymodule precisely for when you want an iterator that includes both its endpoints. If you use that you can useirange(1, 9)to get[1, 2, 3, 4, 5, 6, 7, 8, 9].