Enigmatic Code

Programming Enigma Puzzles

Enigma 1599: Always prime

From New Scientist #2764, 12th June 2010 [link]

Harry, Tom and I were looking to find a set of three digits (not necessarily all different) such that whichever order one wrote them down in the resultant 3-digit number was a prime.

We each found a different valid example. One of Harry’s digits also appeared in my example, and one of my digits also appeared in Tom’s example.

What were my three digits?

[enigma1599]

Advertisements

One response to “Enigma 1599: Always prime

  1. jimrandell 11 January 2012 at 10:02 am

    The following Python program runs in 39ms.

    from itertools import permutations
    from enigma import irange, is_prime, printf
    
    # 3 digit primes (a prime sieve would be faster)
    prime3 = list(n for n in irange(100, 999) if is_prime(n))
    
    # check the 3 digit primes 'abc' (where a < b < c) such that the other
    # combinations are also prime
    primes = []
    for n in prime3:
      (a, b, c) = list(str(n))
      if not(a <= b <= c): continue
      c = set(((a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)))
      if all(int(''.join(x)) in prime3 for x in c):
        primes.append(n)
    
    for (T, D, H) in permutations(primes, 3):
      d = set(str(D))
      h = set(str(H))
      if not d.intersection(h): continue
      t = set(str(T))
      if not d.intersection(t): continue
      printf("D={D} [T={T} H={H}]")
    

    Solution: The three digits are 1, 1 and 3.

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: