Enigmatic Code

Programming Enigma Puzzles

Enigma 549: Prime pairs

From New Scientist #1702, 3rd February 1990 [link]

The twenty children on the school trip had labels on their lapels. The numbers from 1 to 20 inclusive were written on the labels, one number on each label.

The twenty children were told to form pairs. I noted that the average number of each pair was prime. And when I noted the ten primes obtained in this way I saw that the primes which occurred each occurred a different number of times.

List the ten pairs.

[enigma549]

One response to “Enigma 549: Prime pairs

  1. Jim Randell 18 May 2020 at 8:27 am

    This Python 3 program runs in 116ms.

    Run: [ @repl.it ]

    from enigma import irange, multiset, div, is_prime, seq_all_different as all_different, printf
    
    # collect <ns> in pairs, such that the average of each pair is prime
    # ps = pairs collected so far
    # ms = averages seen so far
    def solve(ns, ps=[], ms=[]):
      # are we done?
      if not ns:
        yield (ps, ms)
      else:
        # choose the next number
        a = ns.pop(0)
        # and its partner
        for (i, b) in enumerate(ns):
          x = div(a + b, 2)
          if x is not None and is_prime(x):
            yield from solve(ns[:i] + ns[i + 1:], ps + [(a, b)], ms + [x])
    
    # find pairs and count the primes
    for (ps, ms) in solve(list(irange(1, 20))):
      # the number of times each average occurs is unique
      if all_different(multiset.from_seq(ms).values()):
        printf("{ps} -> {ms}")
    

    Solution: The pairs are: 1 & 3; 2 & 20; 4 & 18; 5 & 17; 6 & 16; 7 & 19; 8 & 14; 9 & 13; 10 & 12; 11 & 15.

    The averages and their occurrences are:

    avg = 2, count = 1, pairs = 1 & 3
    avg = 13, count = 2, pairs = 7 & 19, 11 & 15
    avg = 11, count =7, pairs = 2 & 20, 4 & 18, 5 & 17, 6 & 16, 8 & 14, 9 & 13, 10 & 12

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

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

%d bloggers like this: