Enigmatic Code

Programming Enigma Puzzles

Puzzle #214: Seven up!

From New Scientist #3431, 25th March 2023 [link] [link]

My burglar alarm won’t stop beeping. I need to enter the four-digit code, but I just can’t remember what it is.

I do remember that I chose a number that is divisible by 7. I also know that I chose a number in the thousands. I recall that the numbers formed by the first three digits and the last three are divisible by 7 as well. I also deliberately chose four different digits. Finally, I know that if I add all the digits, their sum isn’t divisible by 7, but if I add the digits of that sum, the result is divisible by 7.

Can you help me work out what my code is to stop the incessant beeping?

[puzzle#214]

11 responses to “Puzzle #214: Seven up!

  1. Jim Randell 24 March 2023 at 8:37 am

    This puzzle can be solved using the [[ SubstitutedExpression ]] solver from the enigma.py library.

    The following run file executes in 68ms. (Internal run time of the generated program is 329µs).

    Run: [ @replit ]

    #! python3 -m enigma -r
    
    SubstitutedExpression
    
    # check a number is divisible by 7
    --code="div7 = lambda n: n % 7 == 0"
    
    # the entire number is divisible by 7
    "div7(ABCD)"
    
    # as are just the first 3 and last 3 digits
    "div7(ABC)"
    "div7(BCD)"
    
    # the digit sum is not, but digit sum of the digit sum is
    "not div7(dsum(ABCD))"
    "div7(dsum(dsum(ABCD)))"
    

    Solution: The code is: 7630.

  2. GeoffR 24 March 2023 at 9:12 am
    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A; var 0..9:B; var 0..9:C; var 0..9:D;
    constraint all_different([A, B, C, D]);
    
    var 1000..9999:ABCD = 1000*A + 100*B + 10*C + D;
    
    % I chose a number that is divisible by 7
    constraint ABCD mod 7 == 0;
    
    % I recall that the numbers formed by the first three digits 
    % ...and the last three are divisible by 7 as well.
    constraint (100*A +10*B + C) mod 7 == 0 /\ (100*B +10*C + D) mod 7 == 0; 
    
    % I know that if I add all the digits, their sum isn’t divisible by 7
    var 6..30:dig_sum = A + B + C + D;
    constraint dig_sum mod 7 != 0;
    
    %...but if I add add the digits of that sum, the result is divisible by 7.
    var 1..9:d1; var 1..9:d2;
    constraint d1 == dig_sum div 10 /\ d2 = dig_sum mod 10;
    constraint (d1 + d2) mod 7 == 0;
    
    solve satisfy;
    
    output["My code is " ++ show(ABCD) ];
    
    % My code is 7630
    % ----------
    % ==========
    
    
    
    
  3. Peter Phillips 24 March 2023 at 3:16 pm

    Using good ol’ Excel I can confirm that 7630 is indeed the result. After step 3 (first three and last three digits are divisible by seven), you are left with 13 pairs of numbers, all beginning with 7, which in itself is interesting – something missed, no doubt by programming fans.

    • Jim Randell 24 March 2023 at 9:13 pm

      I found there were 11 candidate solutions before the digit sum conditions are applied (all of the form 7xx0). None of them have a digit sum divisible by 7, so only the final condition narrows these down to a single value.

  4. Hugo 27 March 2023 at 11:12 am

    “If I add the digits of that sum” is ambiguous.
    The digit sum of 7630 is 16, and if we add to it 1 + 6 we get 23,
    which is not an integer multiple of 7.
    The solution based on my understanding of the phrase is 7350:
    digit sum 15, which when increased by 1 + 5 gives 21.
    Puzzle setters should say what they mean!

    • Jim Randell 27 March 2023 at 6:09 pm

      I see what you mean. Fortunately each interpretation gives rise to a unique answer (although different in each case), so when the solution is published we should be able to determine what the setter had in mind.

      To find the answer using this interpretation we can replace line 17 with:

      "div7(dsum(ABCD) + dsum(dsum(ABCD)))"
      
    • Jim Randell 29 March 2023 at 9:11 pm

      It looks like the published answer is 7630 [link].

  5. GeoffR 29 March 2023 at 2:28 pm
    
    from enigma import nsplit, all_different
    
    # I chose a number in the thousands, divisible by 7
    # ... with all different digits
    for num in range(1000, 9999):
        if num % 7 != 0:continue
        A, B, C, D = nsplit(num)
        if not all_different(A, B, C, D): continue
        # The first three digit number is divisible by 7
        if not (100*A + 10*B + C) % 7 == 0:continue
        # The last three digit number is divisible by 7
        if not (100*B + 10*C + D) % 7 == 0:continue
        # The four digit sum is not divisible by 7
        # ... and must be two digits 
        if (A + B + C + D) % 7 != 0:
            # The two digit sum of the four digits is divisible by 7
            X = (A + B + C + D) // 10
            Y = (A + B + C + D) % 10
            if (X + Y) % 7 == 0:
                print(f"My Code was {1000*A + 100*B + 10*C + D}.")
    
    # My Code was 7630.
    
    
  6. GeoffR 29 March 2023 at 8:22 pm

    I tried this teaser on ChatGPT. It took repeated replies to correct the proposed solution where it started to go wrong. The final solution below gets the answer ABCD = 7630 which has been obtained previously as a result. (It also gets another answer i.e 2590, which is incorrect as 590 is not exactly divisible by 7.)

    FINAL SOLUTION BY ChatGPT

    Given:

    ABCD is exactly divisible by 7.
    All four digits are distinct.
    The 3-digit numbers formed as ABC and BCD are also exactly divisible by 7.
    The sum of the digits (A + B + C + D) is not divisible by 7.
    The sum of (A + B + C + D) is a two digit number.
    The sum of these two digits of the two digit number is exactly divisible by 7.

    To solve this problem, we will use the following steps:

    Step 1: Find all possible values of ABCD that are divisible by 7.

    We know that ABCD is divisible by 7, and all four digits are distinct. Since there are only 5040 possible four-digit numbers with distinct digits, we can generate all of them and check for divisibility by 7.

    After generating all possible four-digit numbers with distinct digits, we find that there are only two possible values of ABCD that are divisible by 7: 2590 and 7630.

    Step 2: Check which of the two possible values of ABCD satisfy the other given conditions.

    For each of the two possible values of ABCD, we check if the 3-digit numbers formed as ABC and BCD are also divisible by 7.

    For 2590, we have ABC = 259 and BCD = 590. Both ABC and BCD are divisible by 7, so this value of ABCD satisfies the second condition.

    For 7630, we have ABC = 763 and BCD = 630. Both ABC and BCD are divisible by 7, so this value of ABCD also satisfies the second condition.

    Step 3: Check which of the two possible values of ABCD satisfy the remaining conditions.

    For each of the two possible values of ABCD, we check if the sum of the digits (A + B + C + D) is not divisible by 7, and if it is a two-digit number whose digits sum to a multiple of 7.

    For 2590, we have A + B + C + D = 16, which is not divisible by 7. The digits of the two-digit number (A + B + C + D) sum to 7, which is a multiple of 7.

    For 7630, we have A + B + C + D = 16, which is not divisible by 7. The digits of the two-digit number (A + B + C + D) sum to 7, which is a multiple of 7.

    Step 4: Check if all four digits are distinct for the solution(s) that satisfy all conditions.

    For the value of ABCD = 2590, we have A = 2, B = 5, C = 9, and D = 0, which are all distinct digits.

    For the value of ABCD = 7630, we have A = 7, B = 6, C = 3, and D = 0, which are all distinct digits.

    Therefore, the solutions to the puzzle are ABCD = 2590 and ABCD = 7630.

    • Jim Randell 1 April 2023 at 8:39 am

      @GeoffR: You obviously had more luck (or patience) with ChatGPT than I did.

      When I presented it with the problem (as originally worded) and asked it to write a Python program to solve the puzzle, then it did produce viable code.

      But when I asked it what the answer to the puzzle was, it produced a set of fourteen 4-digit numbers that it claimed were solutions, but none of them were correct.

  7. GeoffR 2 April 2023 at 12:34 pm

    With the clarified brief at the beginning of the program,
    produced a program which ran correctly, It stated on its
    blog, however, that it found three answers (all of which
    were incorrect) Perhaps it is the briefing line with
    five asterisks which is causing the ambiguity in the original
    teaser description.

    The code ChatGPT produces code with very long variable
    names and it was produced by ChatGPT exactly as shown
    below.

    '''
    AMENDED BRIEF FOR PROGRAM
    
    ABCD is exactly divisible by 7.
    All four digits are distinct.
    The 3-digit numbers formed as ABC and BCD are also exactly divisible by 7.
    The sum of the digits (A + B + C + D) is not divisible by 7.
    The sum of (A + B + C + D) is a two digit number.
    ***** The sum of these two digits of the two digit number is exactly divisible by 7. *****
    
    Produce a Python program and show the output of this program
    
    '''
    
    for A in range(1, 10):
        for B in range(0, 10):
            if B == A:
                continue
            for C in range(0, 10):
                if C == A or C == B:
                    continue
                for D in range(0, 10):
                    if D == A or D == B or D == C:
                        continue
                    
                    ABC = A * 100 + B * 10 + C
                    BCD = B * 100 + C * 10 + D
                    ABC_divisible_by_7 = ABC % 7 == 0
                    BCD_divisible_by_7 = BCD % 7 == 0
                    ABCD_divisible_by_7 = (ABC * 10 + D) % 7 == 0
                    sum_ABCD_not_divisible_by_7 = (A + B + C + D) % 7 != 0
                    sum_ABCD_two_digits = 10 <= A + B + C + D <= 99
                    sum_ABCD_two_digits_divisible_by_7 = (A + B + C + D) // 10 + (A + B + C + D) % 10 == 7
                    
                    if (ABC_divisible_by_7 and BCD_divisible_by_7 and ABCD_divisible_by_7 
                        and sum_ABCD_not_divisible_by_7 and sum_ABCD_two_digits 
                        and sum_ABCD_two_digits_divisible_by_7):
                        print(f"{A}{B}{C}{D}")
    
    # Actual output from its own Python code is correct
    # 7630
    
    '''
    ChatGPT outputs three incorrect answers
    when asked to  output its own code:
    
    1963
    7852
    9873
    '''
    

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 )

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: