Enigmatic Code

Programming Enigma Puzzles

Enigma 1656: Five integers

From New Scientist #2822, 23rd July 2011 [link]

I have written down five single-digit positive integers.

The sum of the five integers is divisible by the fifth integer, but not by any of the first four. The sum of the first four integers is divisible by the fourth but not by any of the first three. The sum of the first three integers is divisible by the third but not by either of the first two. The sum of the first two integers is divisible by the second but not by the first.

List the five integers in the order in which I have written them down.

[enigma1656]

Advertisements

5 responses to “Enigma 1656: Five integers

  1. jimrandell 4 December 2011 at 2:24 pm

    The following recursive Python program runs in 35ms.

    Run: [ @codepad.org | @repl.it ]

    from enigma import irange
    
    # check the list of numbers
    def check(ns):
      t = sum(ns)
      return (t % ns[-1] == 0) and all(t % n > 0 for n in ns[0:-1])
    
    # allowable numbers
    numbers = list(irange(1, 9))
    
    # recursive solver
    def solve(ns):
      # are we done?
      if len(ns) == 5:
        print(ns)
      else:
        # extend the list of numbers
        for n in numbers:
          ns2 = ns + [n]
          if check(ns2):
            solve(ns2)
    
    solve([])
    

    Solution: The five integers are 8, 4, 6, 3 and 1.

      • geoffrounce 4 February 2016 at 7:29 pm
        include "globals.mzn";
         
        var 1..9: A;   var 1..9: B;  var 1..9: C;
        var 1..9: D;   var 1..9: E; 
        var 10..50 : sum5;
        var 10..50 : sum4;
        var 10..50 : sum3;
        var 10..50 : sum2;
        
        constraint alldifferent([A,B,C,D,E]);
        
        constraint sum5 = A + B + C + D + E;
        
        constraint
            sum5 mod E == 0 /\ sum5 mod D != 0 /\ sum5 mod C != 0
            /\ sum5 mod B != 0 /\ sum5 mod A != 0;
        
        constraint sum4 = A + B + C + D;
        
        constraint
            sum4 mod D == 0 /\ sum4 mod C != 0 /\ 
            sum4 mod B != 0 /\ sum4 mod A != 0;
        
        constraint
            sum3 = A + B + C /\ sum3 mod C == 0 /\
            sum3 mod B != 0 /\ sum3 mod A != 0;
        
        constraint
            sum2 = A + B /\ sum2 mod B == 0 /\
            sum2 mod A != 0;
        
        solve satisfy;
        
        output [show(A)," ",show(B)," ",show(C)," ",show(D)," ",show(E)];
        % Output : 8 4 6 3 1
        %
        
        • Jim Randell 30 June 2016 at 10:59 am

          I just realised that the alldifferent() constraint is not required by the puzzle text, and you still get the right answer if you remove this constraint from the model.

          • geoffRounce 6 August 2016 at 12:18 pm

            Yes, I have just confirmed the ‘AllDifferent’ condition is not required in my Visual Basic solution which runs in VS 2015. The ‘AllDifferent’ condition is commented out in this posting.

            Both options had fast run times :

            1. With AllDifferent condition :
            5 Digits are: 8,4,6,3,1
            1.6535 msec

            2. Without AllDifferent condition :
            5 Digits are: 8,4,6,3,1
            1.0989 msec

            Module Module1
            
              Function AllDifferent(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer,
                                      ByVal d As Integer, ByVal e As Integer) As Boolean
                If a <> b And a <> c And a <> d And a <> e _
                    And b <> c And b <> d And b <> e _
                    And c <> d And c <> d And c <> e And d <> e Then
                  Return True
                Else
                  Return False
                End If
            
              End Function
            
            
              Sub Main()
                Dim watch As Stopwatch = Stopwatch.StartNew()
            
                Dim A, B, C, D, E As Integer
                Dim sum5, sum4, sum3, sum2 As Integer
            
                For A = 1 To 9
                  For B = 1 To 9
                    For C = 1 To 9
                      For D = 1 To 9
                        For E = 1 To 9
            
                          'If AllDifferent(A, B, C, D, E) Then
                          sum5 = A + B + C + D + E
                            If sum5 Mod E = 0 And sum5 Mod D <> 0 And sum5 Mod C <> 0 _
                              And sum5 Mod B <> 0 And sum5 Mod A <> 0 Then
                              sum4 = A + B + C + D
                              If sum4 Mod D = 0 And sum4 Mod C <> 0 And
                                sum4 Mod B <> 0 And sum4 Mod A <> 0 Then
                                sum3 = A + B + C
                                If sum3 Mod C = 0 And sum3 Mod B <> 0 And
                                  sum3 Mod A <> 0 Then
                                  sum2 = A + B
                                  If sum2 Mod B = 0 And sum2 Mod A <> 0 Then
                                  Console.WriteLine(" 5 Digits are: {0},{1},{2},{3},{4}",
                                                      A, B, C, D, E)
                                  watch.Stop()
                                    Console.WriteLine(watch.Elapsed.TotalMilliseconds & " msec")
                                    Console.ReadLine()
                                  End If ' sum2
                                End If ' sum3
                              End If ' sum4
                            End If 'sum5
                          'End If  'AllDifferent
            
                        Next E
                      Next D
                    Next C
                  Next B
                Next A
            
              End Sub
            
            End Module
            
            

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: