Enigmatic Code

Programming Enigma Puzzles

Enigma 1775: Third symphony

From New Scientist #2943, 16th November 2013 [link]

I have in mind three numbers. Each is a multiple of 3 and consists of three different non-zero digits, just one of which is 3. For each of these numbers individually, three of the following six statements are true and three are false.

1) It is the product of three different numbers, each a prime.
2) It is a triangular number, that is of the series 1, 3, 6, 10, 15 …
3) It is a cube, or a cube plus 3.
4) It may be written as a single-digit prime followed by a two-digit prime.
5) The 3 is the first or last digit.
6) It may be written as a two-digit prime followed by a single-digit prime.

What are my three numbers?

[enigma1775]

Advertisements

2 responses to “Enigma 1775: Third symphony

  1. Jim Randell 13 November 2013 at 6:41 pm

    This puzzle highlighted an inconsistency in the return values (now fixed) of the is_triangular() function from the enigma.py library. This Python program uses the latest version of that function, and runs in 36ms.

    from enigma import irange, factor, is_triangular, is_cube, Primes, printf
    
    # 1- and 2- digit primes
    ps = [None, [], []]
    for p in Primes(100):
      s = str(p)
      ps[len(s)].append(s)
    
    # generate 3-digit multiples of 3
    for n in irange(102, 999, 3):
      # all digits should be different and non-zero, but one should be 3
      s = str(n)
      ds = set(s)
      if len(ds) != 3 or '0' in ds or '3' not in ds: continue
    
      # evaluate the statements
      fs = factor(n)
      ss = (
        # 1. is the product of three different primes
        (len(fs) == 3 and len(set(fs)) == 3),
    
        # 2. is a triangular number
        bool(is_triangular(n)),
    
        # 3. is a cube or a cube + 3
        bool(is_cube(n) or is_cube(n - 3)),
    
        # 4. may be written as a 1-digit prime followed by a 2-digit prime
        (s[0] in ps[1] and s[1:3] in ps[2]),
    
        # 5. the 3 is the first or last digit
        (s[1] != '3'),
    
        # 6. may be written as a 2-digit prime followed by a 1-digit prime
        (s[0:2] in ps[2] and s[2] in ps[1]),
      )
    
      if ss.count(True) == 3:
        printf("n={n} {ss} {fs}")
    

    Solution: The three numbers are 231, 273 and 435.

  2. geoffrounce 9 May 2017 at 9:27 am

    I found the predicate ‘exactly’ was useful to check that a given number of items in a list are true.

    I also set the configuration to multiple outputs to give the three (and only three) numbers fulfilling
    the puzzle conditions

    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A;   var 1..9:B;  var 1..9:C;
    
    var 2..97:x;  var 2..97:y; var 2..97:z; 
    
    var 11..97:AB = 10*A + B;
    var 11..97:BC = 10*B + C;
    var 100..999:ABC = 100 * A + 10 * B + C;
    
    constraint alldifferent([A,B,C]) 
    /\ (A == 3 \/ B == 3 \/ C == 3) /\ ABC mod 3 == 0;
    
    % Three digit number prime factors
    constraint all_different( [x,y,z]); 
    
    predicate is_prime(var int: x) = x > 1 /\
       forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ( 
            (i < x) -> (x mod i > 0));
    
    % Three digit triangular numbers
    set of int: tri3 = { n * (n+1) div 2 | n in 14..44 };
    
    % Three digit cube numbers
    set of int: cb3 = { n*n*n | n in 5..9 };
    
    % Uses the predicate exactly(int: n, array [int] of var int: x, int: v);
    % Predicate below requires exactly n(ie 3) variables in x(ie 6) items to take the value 1 (ie v)
    
    constraint exactly ( 3, [ 
    bool2int (is_prime(x) /\ is_prime(y) /\ is_prime(z) /\ x*y*z == ABC),
    bool2int ( ABC in tri3 ),
    bool2int (( ABC in cb3) \/ (ABC - 3) in cb3),
    bool2int (is_prime(A) /\ is_prime(BC)),
    bool2int (A == 3 \/ C == 3),
    bool2int (is_prime(AB) /\ is_prime(C)),      
    ], 1 );
    
    solve satisfy;
    output [ "Number = ", show(ABC) ++ "\n"  ];   
    
    % Multiple output configuration gives:
    % Number = 231
    % Number = 273
    % Number = 435
    % Finished in 127msec
    
    

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: