Enigmatic Code

Programming Enigma Puzzles

Enigma 1306: Three all

From New Scientist #2464, 11th September 2004

I have in mind three numbers each of three digits (no leading zero) in each of which one digit is 3. Of the following statements about them, three are true and three are false.

(a) The number is a prime.
(b) The number is (appropriately) a cube.
(c) The middle digit is the average of the other two digits.
(d) The third digit differs from the second by 3.
(e) The number has as a factor a two-digit prime the difference of whose digits is 3, or whose sum is a cube.
(f) The number belongs (appropriately) to the set of triangular numbers 1, 3, 6, 10, 15, 21…

What is the sum of the three numbers?



One response to “Enigma 1306: Three all

  1. Jim Randell 27 July 2014 at 8:43 am

    I think this could have been worded slightly more clearly. I took the view that for each of the three numbers being considered three of the statements were true (and three of them false), and that seemed to work.

    This Python program runs in 35ms.

    from enigma import irange, Primes, split, factor, T, printf
    # primes
    primes = Primes(999)
    # cubes
    cubes = set(n ** 3 for n in irange(1, 9))
    # triangular numbers
    tris = set(T(n) for n in irange(4, 140))
    # accumulate the sum of the required numbers
    r = 0
    # consider 3-digit candidate numbers
    for n in irange(100, 999):
      s = str(n)
      # must contain the digit 3
      # or: [[ if s.count('3') != 1 ]]
      if '3' not in s: continue
      # accumulate satisfied statements
      t = ''
      # A: primes
      if n in primes: t += 'A'
      # B: cubes
      if n in cubes: t += 'B'
      (a, b, c) = split(s, int)
      # C: middle digit is the average of the other two
      if a + c == 2 * b: t += 'C'
      # D: third digit differs from the second by 3
      if abs(b - c) == 3: t += 'D'
      # E: a factor of the number is a 2-digit prime, with digit difference
      # of 3 or digit sum is a cube
      for f in set(factor(n)):
        if f < 10: continue
        if f > 99: break
        (x, y) = split(f, int)
        if abs(x - y) == 3 or (x + y) in cubes:
          t += 'E'
      # F: triangular numbers
      if n in tris: t += 'F'
      if len(t) == 3:
        r += n
        printf("[{n}: {t}]")
    printf("sum = {r}")

    Solution: The sum of the three numbers is 1135.

    The three numbers themselves are:

    136: satisfies statements (d), (e), (f);
    369: satisfies statements (c), (d), (e);
    630: satisfies statements (c), (d), (f).

    It wasn’t clear to me if candidate numbers should contain exactly one digit 3. I considered numbers with multiple 3 digits in my program, and, as it turns out they could have been excluded at the start.

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: