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?


One response 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]
        # 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).

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: