Enigmatic Code

Programming Enigma Puzzles

Enigma 1566: Consistently older

From New Scientist #2729, 10th October 2009 [link]

Yesterday was Harry’s birthday and also Tom’s birthday. Harry is the older. Harry’s current age, Tom’s current age and the difference between their ages are each the product of two primes. The six primes are all different.

Before Harry reaches the age of 90, there will be three further years during which his age, Tom’s age and the difference between their ages will again each be the product of two primes, the six primes all being different.

What is Harry’s current age?



One response to “Enigma 1566: Consistently older

  1. jimrandell 21 February 2012 at 9:32 pm

    The following Python program runs in 39ms.

    from itertools import combinations
    from collections import defaultdict
    from enigma import is_prime, printf
    # primes less than 90
    primes = list(n for n in range(2, 90) if is_prime(n))
    # numbers that are the product of distinct primes, less than 90
    products = {}
    for (a, b) in combinations(primes, 2):
      p = a * b
      if not(p < 90): continue
      products[p] = (a, b)
    # accumulate pairs of these products
    d = defaultdict(list)
    for (T, H) in combinations(products.keys(), 2):
      if H < T: (H, T) = (T, H) # just to be sure
      # where the difference is also in the product
      D = H - T
      if D not in products: continue
      # and all the primes must be different
      if len(set(products[H]).union(products[T], products[D])) < 6: continue
      # record the ages by the difference
      d[D].append((H, T))
    # now find the differences which have at least 4 pairs of ages
    for (k, v) in d.items():
      if len(v) < 4: continue
      printf("age={age} diff={k} {v}", age=min(i[0] for i in v))

    Solution: Harry’s current age is 57.

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: