### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,115)
- misc (2)
- project euler (2)
- puzzle (29)
- site news (43)
- tantalizer (29)
- teaser (3)

### Site Stats

- 166,357 hits

Programming Enigma Puzzles

7 December 2011

Posted by on **From New Scientist #2842, 10th December 2011** [link]

There is always the same number of sweets – fewer than 50 – in a box of Tweets. Some are red and the rest are green. The proportion of reds is always the same.

Two lads, Dip and Flip, had a box each last week and a box each this week. Dip picks sweets at random from his box without looking in. Last week, when he had just eaten his last red one, he noticed he had just one green left. This week, when he had eaten his last red sweet, there were two greens left. Last week’s situation is four times as likely as this week’s.

Until he runs out of one colour, Flip picks his sweets by tossing a coin: heads he eats a red one and tails he eats a green one. Last week he had just eaten his last red one and he counted the number of green ones left. This week, when he had eaten his last red sweet there was one more green sweet left than in the previous week. In fact, having last week’s number left is four times as likely as having this week’s.

Tell me how many red sweets and how many green ones make up a box of Tweets.

[enigma1676]

Advertisements

%d bloggers like this:

This Python program runs in 140ms:

Solution:There are 22 red sweets and 8 green sweets in a box.Simplifying the equations for dip and flip lets you search the solution space much more effectively (runtime: 30ms).

This program is a constructive demonstration that the answer is indeed 30 sweets composed of 22 red and 8 green. It constructs all possible ways of taking the sweets out of the box and counts the probabilities associated with each desired outcome.

But it is a bit slow to use over the entire solution space to determine the answer (just running this check takes 45s (using PyPy)).