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]

2 responses 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
      cs = 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 cs):
        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.

  2. geoffrounce 13 March 2018 at 7:25 pm

    The answer was found without specifically programming one of my digits appearing in each of Harry and Tom’s sets of digits:

    % A Solution in MiniZinc
    include "globals.mzn";
    
    % Digits for the three sets
    var 1..9:A;   var 1..9:B;   var 1..9:C;
    var 1..9:D;   var 1..9:E;   var 1..9:F;
    var 1..9:G;   var 1..9:H;   var 1..9:J;
    
    % My six prime numbers
    var 100..999: ABC = 100*A + 10*B + C;
    var 100..999: ACB = 100*A + 10*C + B;
    var 100..999: BAC = 100*B + 10*A + C;
    var 100..999: BCA = 100*B + 10*C + A;
    var 100..999: CAB = 100*C + 10*A + B;
    var 100..999: CBA = 100*C + 10*B + A;
    
    % Harry's six prime numbers
    var 100..999: DEF = 100*D + 10*E + F;
    var 100..999: DFE = 100*D + 10*F + E;
    var 100..999: EFD = 100*E + 10*F + D;
    var 100..999: EDF = 100*E + 10*D + F;
    var 100..999: FED = 100*F + 10*E + D;
    var 100..999: FDE = 100*F + 10*D + E;
    
    % Tom's six prime numbers
    var 100..999: GHJ = 100*G + 10*H + J;
    var 100..999: GJH = 100*G + 10*J + H;
    var 100..999: HJG = 100*H + 10*J + G;
    var 100..999: HGJ = 100*H + 10*G + J;
    var 100..999: JHG = 100*J + 10*H + G;
    var 100..999: JGH = 100*J + 10*G + H;
    
    predicate is_prime(var int: x) = 
      x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ((i < x) -> (x mod i > 0));
      
     % My six prime numbers
    constraint sum ( [ is_prime(ABC), is_prime(ACB), is_prime(BAC),
    is_prime(BCA), is_prime(CAB), is_prime(CBA) ] ) == 6;
    
    % Harry's six prime numbers
    constraint sum ( [ is_prime(DEF), is_prime(DFE), is_prime(EFD),
    is_prime(EDF), is_prime(FED), is_prime(FDE) ] ) == 6;
    
    % Tom's six prime numbers
    constraint sum ( [ is_prime(GHJ), is_prime(GJH), is_prime(HJG),
    is_prime(HGJ), is_prime(JHG), is_prime(JGH) ] ) == 6;
    
    % The three sets of digits
    var set of int: s1 = { A, B, C };
    var set of int: s2 = { D, E, F };
    var set of int: s3 = { G, H, J };
    
    % Three different sets of numbers found
    constraint s1 != s2  /\ s1 !=  s3  /\ s2 != s3;
    
    % Order output of digits
    constraint  A < C  /\  B < C  /\ C < F;
    constraint  D < F /\  E <  F /\  F < J ;
    constraint G < J;
    
    solve satisfy;
    
    output [ "My Digits = " ++ show(A) ++ ",  " ++ show(B) ++ ",  " ++ show(C) ++ "\n"
    ++ "Harry's Digits  =  "++ show(D) ++ ",  " ++ show(E) ++ ",  " ++ show(F) ++ "\n"
    ++ "Tom's Digits  = "++ show(G) ++ ",  " ++ show(H) ++ ",  " ++ show(J) ];
    
    % My Digits = 1,  1,  3
    % Harry's Digits  =  3,  3,  7
    % Tom's Digits  = 1,  9,  9
    % ----------
    % ==========
    % Finished in 187msec
    

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: