Enigmatic Code

Programming Enigma Puzzles

Enigma 509: Banking on a prime

From New Scientist #1661, 22nd April 1989 [link]

I have two accounts at Midloids bank, both with unusual eight-digit account numbers, which are made up of a combination of only odd digits.

If either of the account numbers is split in half it gives two four-digit prime numbers. These two primes contain the same four digits, but in a different order, and with no digit repeated. Furthermore if these four-digit primes are split in half, they each give two two-digit prime numbers.

If, for both numbers, the prime formed from the first four digits is larger than the prime formed from the second four digits, what are the numbers of my accounts?

[enigma509]

2 responses to “Enigma 509: Banking on a prime

  1. Jim Randell 22 July 2019 at 9:07 am

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

    The following run file executes in 149ms.

    Run: [ @repl.it ]

    #!/usr/bin/env python -m enigma -r
    
    SubstitutedExpression
    
    --digits="1,3,5,7,9"
    --distinct="ABCD,EFGH"
    
    # two 4-digit primes
    "is_prime(ABCD)" "is_prime(EFGH)"
    
    # same digits in either half
    "ordered(A, B, C, D) == ordered(E, F, G, H)"
    
    # first half is larger than second half
    "ABCD > EFGH"
    
    # four 2-digit primes
    "is_prime(AB)" "is_prime(CD)" "is_prime(EF)" "is_prime(GH)"
    
    # answer is the account number
    --answer="ABCDEFGH"
    

    Solution: The two account numbers are 37191973 and 71591759.

  2. GeoffR 23 July 2019 at 12:28 pm
    % A Solution in MiniZinc
    include "globals.mzn";
    
    set of int: odd = {1,3,5,7,9};
    
    % 8 odd digits for the bank account numbers
    var odd: a; var odd: b; var odd: c; var odd: d;
    var odd: e; var odd: f; var odd: g; var odd: 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));
    
    % account number of 8 digits is abcdefgh with 2 no. 4-digit parts
    var 1000..9999: LH = 1000*a + 100*b + 10*c + d;
    var 1000..9999: RH = 1000*e + 100*f + 10*g + h;
    
    % 1st 4-digit prime greater than 2nd 4-digit prime
    constraint LH > RH;
    
    % both sets of 4 digits have the same digits
    constraint {a,b,c,d} == {e,f,g,h};
    constraint all_different( [a,b,c,d] ) /\ all_different( [e,f,g,h] );
    
    % the four 2-digit primes from LH and RH numbers
    var 10..99: pr1 = 10*a + b;
    var 10..99: pr2 = 10*c + d;
    var 10..99: pr3 = 10*e + f;
    var 10..99: pr4 = 10*g + h;
    
    % 4-digit and 2-digit prime numbers
    constraint is_prime(LH) /\ is_prime(RH);
    constraint is_prime(pr1) /\ is_prime(pr2) /\ is_prime(pr3) /\ is_prime(pr4);
    
    solve satisfy;
    
    output ["Account number = " ++ show(LH),show(RH) ++
    "\n1st 4-digit prime = " ++ show(LH) ++ ", 2nd 4-digit prime = " ++ show(RH) ++
    "\nTwo digit primes are " ++ show(pr1) ++ ", " ++ show(pr2)
    ++ ", " ++ show(pr3) ++ ", " ++ show(pr4) ];
    
    % Account number = 37191973
    % 1st 4-digit prime = 3719, 2nd 4-digit prime = 1973
    % Two digit primes are 37, 19, 19, 73
    % ----------
    % Account number = 71591759
    % 1st 4-digit prime = 7159, 2nd 4-digit prime = 1759
    % Two digit primes are 71, 59, 17, 59
    % ----------
    % ==========
    % Finished in 273msec
    
    

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: