# Enigmatic Code

Programming Enigma Puzzles

## Enigma 348: A piece of cake

From New Scientist #1497, 27th February 1986 [link]

I went to three parties in succession. At each one I was surprised to see an identical cake, bought at the Bêtisserie Noire, and on each occasion the cake was divided equally among all those present, including me.

When I arrived home (what a dog I felt!) I realised to my embarrassment that I had eaten altogether the equivalent of exactly half a cake. Of course, I could have done that by going to three parties with five people at each, so that there would have been 18 pieces of cake at all three parties. But how dull that would have been! However, just as reversing 18 gives 81, a perfect square, so too reversing the digits in the total number of pieces of cake involved in that triple binge produces a perfect square. Is it a two-digit square? Really, I shan’t hand you the answer on a plate.

How many people did I encounter at each party?

[enigma348]

### 2 responses to “Enigma 348: A piece of cake”

1. Jim Randell 10 June 2016 at 8:53 am

This Python program runs in 36ms.

```from enigma import irange, divf, nreverse, is_square, printf

# generate n numbers (d1, d2, ..., dn) such that 1/d1 + 1/d2 + ... + 1/dn = a/b
# the numbers are generated as an ordered list
# m = minimum allowed number
# g = minimum allowed gap between numbers
def generate(n, b=1, a=1, m=1, g=0):
# are we done?
if n == 1:
(d, r) = divmod(b, a)
if r == 0 and not(d < m):
yield [d]
else:
# find a suitable reciprocal
for d in irange(m, divf(n * b, a)):
if not(b < a * d): continue
# and solve for the remaining fraction
for ds in generate(n - 1, b * d, a * d - b, d + g, g):
yield [d] + ds

# find (a, b, c) such that 1/a + 1/b + 1/c = 1/2
for (a, b, c) in generate(3, 2):

# check the reverse of the total number of pieces is a square
t = a + b + c
r = nreverse(t)
if is_square(r) is None: continue

# ignore a = b = c = 6
s = ('' if a == b == c == 6 else ' [*** SOLUTION ***]')
printf("a={a} b={b} c={c}, t={t} r={r}{s}")
```

Solution: The three parties had 2, 6 and 41 other guests (not including the setter).

Including the setter there were 3, 7 and 42 guests at the parties. So altogether there would be 3 + 7 + 42 = 52 pieces of cake.

1/3 + 1/7 + 1/42 = 14/42 + 6/42 + 1/42 = 21/42 = 1/2.

The generate() function can be used to generate sequences of numbers whose reciprocals sum to a specified fraction. The m parameter can be used to specify a minimum value for the numbers, and the g parameter can be used to specify the minimum gap between numbers (so if you want all the numbers to be different set g=1).

The number of solutions for numbers whose reciprocals sum to unity is given by OEIS A002966 (and if the numbers are all required to be different (g=1) OEIS A006585).

2. geoffrounce 16 May 2018 at 2:15 pm

This Enigma follows on from Enigma 1062.

```def is_sq(n):
c = int(n**(1/2) + 0.5)
return (c**2 == n)

# 3 parties with x, y and z people attending each of the three parties
for x in range (1,100):
for y in range(x,100):
for z in range(y,100):

# x, y and z people attend the three parties
# 1/x + 1/y + 1/z = 1/2
if x * y * z != 2 * (y * z + x * z + x * y): continue
pieces = x + y + z

# allow for a 2 or 3 digit square as reverse number of pieces of cake
# 2-digit squares
if pieces < 100 and x < y < z:
rcake = 10 * (pieces % 10) + (pieces // 10 % 10)

# or 3-digit squares
if pieces > 100 and pieces < 1000 and x < y < z:
rcake = 100*(pieces % 10) + 10*(pieces // 10 *10) + pieces // 100

if is_sq(rcake):
pieces = x + y + z
print ('x={}, y={},z={}, total pieces of cake = {}' \
.format(x, y, z, pieces))
print('People encountered at each party = {}, {} and {}' \
. format(x-1, y-1, z-1))

# x=3, y=7,z=42, total pieces of cake = 52
# People encountered at each party = 2, 6 and 41
```

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