Enigmatic Code

Programming Enigma Puzzles

Enigma 1482: Piling on the agony

From New Scientist #2644, 23rd February 2008

My nephew was playing with some counters, so I decided to give him a mathematical exercise using them. I asked him to calculate how many ways there were of dividing his counters into three piles. For example, eight counters could be divided into three piles five ways, namely 1+1+6, 1+2+5, 1+3+4, 2+2+4 and 2+3+3.

After some time he wrote down his four-digit answer but I assumed that he had made a slight error in writing it down because I knew the correct answer and saw that in his number the middle two digits had been swapped over.

However, it turned out that he had made his error when counting, not when writing the number down. He had assumed that the three piles had to contain different numbers of counters, and had done his calculations accordingly. (So for example, with eight counters he would have got the answer “two” rather than “five””.)

How many counters did he have?

This puzzle marks the start of my attempts at programmatic solutions to Enigma puzzles as they are published. Before this I only attempted them sporadically, when a puzzle caught my fancy. Eventually I decided to collect my solutions and put them up on this site.



2 responses to “Enigma 1482: Piling on the agony

  1. Jim Randell 11 November 2012 at 11:19 pm

    The following Python program runs in 93ms.

    from itertools import count
    from enigma import irange, printf
    # count the number of 3-splits
    def piles(n):
      # count the number of splits and distinct splits
      (c, d) = (0, 0)
      for x in irange(1, (n + 2) // 3):
        for y in irange(x, (n + 1) // 2):
          z = n - (x + y)
          if z < y: continue
          c += 1
          if x < y < z: d += 1
      return (c, d)
    for n in count(3):
      (c, d) = map(str, piles(n))
      # check we're right when n is 8
      if n == 8: assert c == '5' and d == '2'
      # c and d should be 4 digits
      if len(d) < 4: continue
      if len(c) > 4: break
      # check c and d have their middle digits swapped
      if not(d[1] != d[2] and c == d[0] + d[2] + d[1] + d[3]): continue
      printf("n={n} [c={c} d={d}]")

    Solution: There are 182 counters.

  2. Hugh Casement 5 October 2014 at 1:52 pm

    It took me a while to find the formula, but only because I was tackling things the hard way.
    The number of ways of dividing n counters into three piles is int(n²/12 + ¼).
    In fact the “plus a quarter” is needed only when n is an odd multiple of 3.
    With three different piles, it’s the same formula with n – 3 in place of n.

    So for n = 182 we get 2760 and 2670.

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: