Enigmatic Code

Programming Enigma Puzzles

Enigma 1040: Elusive cube

From New Scientist #2196, 24th July 1999 [link]

I have written down a sum:

The three numbers between them use each of the digits 1-9 exactly once.

One of the three numbers is a perfect cube and another uses a collection of consecutive digits in some order.

What is the cube?

[enigma1040]

2 responses to “Enigma 1040: Elusive cube

  1. Jim Randell 19 October 2018 at 8:20 am

    This Python program uses the [[ SubstitutedSum() ]] solver from the enigma.py library.

    It runs in 181ms.

    Run: [ @repl.it ]

    from enigma import irange, SubstitutedSum, is_cube, nsplit, tuples, printf
    
    # are the digits in n consecutive (in some order)?
    def is_consecutive(n):
      return all(a + 1 == b for (a, b) in tuples(sorted(nsplit(n)), 2))
    
    # select elements in s satisfying fn
    select = lambda s, fn: set(filter(fn, s))
    
    # possible alphametic sums are: A + BCDE = FGHI, AB + CDE = FGHI, ABC + DEF = GHI
    sums = [('A', 'BCDE', 'FGHI'), ('AB', 'CDE', 'FGHI'), ('ABC', 'DEF', 'GHI')]
    
    # consider possible sums
    for (X, Y, Z) in sums:
      p = SubstitutedSum([X, Y], Z, digits=irange(1, 9))
      for s in p.solve(verbose=0):
        t = (x, y, z) = tuple(int(p.substitute(s, t)) for t in (X, Y, Z))
        if x > y: continue
        # find the cubes and numbers with consecutive digits
        cubes = select(t, is_cube)
        seqs = select(t, is_consecutive)
        # there should be (at least) one of each
        if not(cubes) or not(seqs): continue
        # and they can't be the same number
        if len(cubes.union(seqs)) < 2: continue
    
        # output a solution
        printf("{x} + {y} = {z} [cubes={cubes}, seqs={seqs}]")
    

    Solution: The cube is 729.

    If we require the smallest summand to appear first there are only three possible alphametic sums that fit the pattern:

    A + BCDE = FGHI
    AB + CDE = FGHI
    ABC + DEF = GHI

    If all symbols are required have distinct values the first of these has no solutions.

    Additionally if the digit 0 is not used the second of these has no solutions.

    This leaves ABC + DEF = GHI which has 168 solutions as an alphametic that doesn’t involve zero digits.

    Requiring one of the numbers to be a perfect cube, and another one to consist of three consecutive digits (in some order), narrows these down to only two possible solutions:

    183 + 546 = 729
    186 + 543 = 729

    In both cases the result of the sum is the perfect cube 729 (= 9^3).

    In the second case the number composed of consecutive digits (543) has them in descending order, so by tightening the wording of the puzzle it could have identified a specific sum.

  2. geoffrounce 21 October 2018 at 2:12 pm

    This programme identifies a specific sum, as identified by Jim, but does not consider all possible alphametic sums.

    % A Partial Solution in MiniZinc, finding a specific sum
    include "globals.mzn";
    
    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:i;
    
    constraint all_different ([a,b,c,d,e,f,g,h,i]);
    
    var 100..999: abc = 100*a + 10*b + c; 
    var 100..999: def = 100*d + 10*e + f; 
    var 100..999: ghi = 100*g + 10*h + i;
    
    set of int: cb3 = {n*n*n | n in 5..9};
    
    constraint abc + def == ghi;
    
    % One of the three numbers is a cube
    constraint abc in cb3 \/ def in cb3 \/ ghi in cb3;
    
    % One of the three numbers has consecutive digits
    constraint (abs(a - b) == 1/\ abs(b-c) == 1)
     \/ (abs(d - e) == 1 /\ abs(e-f) == 1)
     \/ (abs(g - h) == 1 /\ abs(h-i) == 1);
     
    solve satisfy;
    
    output [ show(abc) ++ " + " ++ show(def) ++ " = " ++ show(ghi) ];
    
    % 186 + 543 = 729
    % ----------
    % Finished in 64msec
    

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: