# Enigmatic Code

Programming Enigma Puzzles

## Puzzle #09: The cake and the candles

Lady Frederica von Battenberg has baked a long, thin rectangular cake for her daughter Victoria. She has picked two random points on top of the cake on which she has placed two candles.

She hands the cake knife to Victoria, who now proceeds to pick a random point along the length of the cake, and cuts across the cake at that point.

Now that the cake has been cut in two, what is the chance that both pieces of cake have a candle on them?

[puzzle#09]

### One response to “Puzzle #09: The cake and the candles”

1. Jim Randell 29 June 2019 at 8:10 am

We select three points, a, b, x along the cake (we don’t need to worry about where they are across the cake). The chance of any two of the points coinciding is zero, so we can order them such that one is in the middle.

The probability that any particular point is in the middle is 1/3.

If a and b represent the points where the candles are placed, and x represents the point the cake is cut, then the probability that each half of the cut cake has a candle, is the same as the probability that x is between a and b, i.e. 1/3.

Here is a program that does a million random trials and reaches the same conclusion.

Run: [ @repl.it ]

```import random
from enigma import irange, fdiv, printf

# number of trials
N = 1000000

# count the number of successful trials
n = 0
for _ in irange(1, N):

# choose three numbers (a, b, x)
fn = lambda: random.uniform(0, 1)
(a, b, x) = (fn(), fn(), fn())
if a < x < b or b < x < a: n += 1

printf("{n} / {N} = {f:.4f}", f=fdiv(n, N))
```

Solution: The probability that both pieces of cut cake have a candle is 1/3.

This site uses Akismet to reduce spam. Learn how your comment data is processed.