Enigmatic Code

Programming Enigma Puzzles

Enigma 1658: Seven into five

From New Scientist #2824, 6th August 2011 [link]

ENIGMA is a number in which the letters denote different digits. The numbers corresponding to IN, GIN, GAIN, GAMMA and ENIGMA, in which there are no leading zeroes, are divided by 7 and each has remainder I (I as in Ink).

Find the maximum value ENIGMA can take.

[enigma1658]

Advertisements

3 responses to “Enigma 1658: Seven into five

  1. jimrandell 4 December 2011 at 1:09 pm

    This Python code is not the fastest solution, but I like its neatness. It runs in 114ms.

    from enigma import irange, is_pairwise_distinct, printf
    
    for ENIGMA in irange(987654, 123456, step=-1):
      (E, N, I, G, M, A) = map(int, list(str(ENIGMA)))
      if 0 in (I, G, E): continue
      if not is_pairwise_distinct(E, N, I, G, M, A): continue
      if not(ENIGMA % 7 == I): continue
      if not((10*I + N) % 7 == I): continue
      if not((100*G + 10*I + N) % 7 == I): continue
      if not((1000*G + 100*A + 10*I + N) % 7 == I): continue
      if not((10000*G + 1001*A + 110*M) % 7 == I): continue
      printf("ENIGMA = {ENIGMA}")
      break
    

    Solution: The maximum possible value for ENIGMA is 983720.

    • Jim Randell 22 December 2013 at 9:28 am

      Or even more compactly (and using a few more routines from enigma.py):

      from enigma import irange, split, is_pairwise_distinct, nconcat, printf
      
      for ENIGMA in irange(987654, 123456, step=-1):
        (E, N, I, G, M, A) = split(ENIGMA, int)
        if 0 in (I, G, E): continue
        if not is_pairwise_distinct(E, N, I, G, M, A): continue
        if not(ENIGMA % 7 == I): continue
        if not all(nconcat(s) % 7 == I for s in ((I, N), (G, I, N), (G, A, I, N), (G, A, M, M, A))): continue
        printf("ENIGMA = {ENIGMA}")
        break
      
  2. geoffrounce 20 April 2017 at 8:33 pm
    % A solution in MiniZinc
    include "globals.mzn"; 
    
    var 0..9: e; var 0..9: n; var 0..9: I; 
    var 0..9: g; var 0..9: m; var 0..9: a; 
    
    constraint all_different([e,n,I,g,m,a]) /\ I > 0 /\ g > 0 /\ e > 0;
    
    var 10..99: In;   
    var 100..999: gIn;     
    var 1000..9999: gaIn;   
    var 10000..99999: gamma;
    var 100000..999999: enIgma;
    
    constraint In = 10*I + n 
            /\ gIn = 100*g + 10*I + n
            /\ gaIn = 1000*g + 100*a + 10*I + n
            /\ gamma = 10000*g + 1000*a + 110*m + a
            /\ enIgma = 100000*e + 10000*n + 1000*I + 100*g + 10*m + a;
    
    constraint In mod 7 == I /\ gIn mod 7 == I
    /\ gaIn mod 7 == I /\ gamma mod 7 == I
    /\ gamma mod 7 == I /\ enIgma mod 7 == I;
    
    solve maximize(enIgma); 
    
    output["Maximum ENIGMA = ", show(enIgma)];
    
    % Maximum ENIGMA = 983720
    % Finished in 141msec
    
    % Without maximising ENIGMA, I found five other solutions satisfying the
    % constraints for ENIGMA i.e. 283790, 145780, 845710, 213790  and 913720.
    
    

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: