Enigmatic Code

Programming Enigma Puzzles

Enigma 1119: Six primes

From New Scientist #2275, 27th January 2001 [link]

I invite you to select a three-digit prime number such that if you reverse the order of its digits you form a larger three-digit prime number. Furthermore the first two digits and the last two digits of these two three-digit prime numbers must themselves be four two-digit prime numbers, each one different from all the others.

Which three-digit prime number should you select?

[enigma1119]

Advertisements

4 responses to “Enigma 1119: Six primes

  1. Jim Randell 17 April 2017 at 7:25 am

    We can use the SubstitutedExpression() solver from the enigma.py library to solve this problem.

    Here is the run file. It executes in 87ms.

    #!/usr/bin/env python -m enigma -r
    
    SubstitutedExpression
    
    --distinct=""
    --answer="ABC"
    
    "is_prime(ABC)"
    "is_prime(CBA)"
    "A < C"
    "is_prime(AB)"
    "is_prime(BC)"
    "is_prime(CB)"
    "is_prime(BA)"
    "is_pairwise_distinct(AB, BC, CB, BA)"
    

    Solution: The starting number is 179.

  2. Hugh Casement 17 April 2017 at 7:58 am

    Its anagrams 197 and 719 are also prime, as it happens.
    Clearly, the number may contain no even digit or 5; all its digits must be different.
    There are not many candidates: a somewhat trivial puzzle.

  3. geoffrounce 17 April 2017 at 3:55 pm
    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A;   var 0..9:B;  var 1..9:C;
    
    % 3-digit primes
    var 101..997 : ABC = 100 * A + 10 * B + C;
    var 101..997 : CBA = 100 * C + 10 * B + A;
    
    % 2-digit primes
    var 11..97 : AB = 10 * A + B;
    var 11..97 : BC = 10 * B + C;
    var 11..97 : CB = 10 * C + B;
    var 11..97 : BA = 10 * B + A;
    
    constraint CBA > ABC /\ alldifferent([AB,BC,CB,BA]);
    
    % set of 2-digit primes
    set of int: primes = {11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
    53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
    
    predicate is_prime(var int: x) = 
      x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ((i < x) -> (x mod i > 0));
    
    % check all primes
    constraint  AB in primes /\ BC in primes /\ CB in primes /\ BA in primes
    /\ is_prime(ABC) /\ is_prime(CBA);
    
    solve satisfy;
    
    output [ "Three digit prime number = " ++ show(ABC) ]; 
    
    % Three digit prime number = 179
    % Finished in 81msec
    
    
  4. Brian Gladman 18 April 2017 at 5:47 pm
    from itertools import product
    from collections import defaultdict
    from number_theory import Primes, is_prime
    
    # map the high (low) digits in two digit primes to lists
    # of the low (high) digits in primes that contain them
    h2p, l2p = defaultdict(list), defaultdict(list)
    for p in Primes().range(10, 100):
      h2p[p // 10].append(p % 10)
      l2p[p % 10].append(p // 10)
    
    # for all possible centre digits of the three digit prime
    for c in h2p.keys() & l2p.keys():
      # ... consider its possible high and low digits
      for h, l in product(h2p[c], l2p[c]):
        # check that the number and its digit reversal are prime
        p = 100 * l + 10 * c + h
        q = 100 * h + 10 * c + l
        if p < q and is_prime(p) and is_prime(q):
          # check that the four two digit primes are all different
          p4 = { 10 * l + c, 10 * h + c, 10 * c + l, 10 * c + h } 
          if len(p4) == 4:
            print('{} ({}, {})'.format(p, q, sorted(p4)))      
    

    This uses my Number Theory library.

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: