Enigmatic Code

Programming Enigma Puzzles

Enigma 1350: All but one

From New Scientist #2509, 23rd July 2005

Joe asked Penny to think of four different positive digits and add up the 24 different four-digit numbers that could be made using the four digits. Then he asked her to subtract just one of the four-digit numbers from the total and write down the new total. When asked what number she had written down, Penny replied: “122??0.”

Joe didn’t quite hear the fourth or fifth digits. But he was able to work out the number Penny had subtracted.

What was that number?

[enigma1350]

4 responses to “Enigma 1350: All but one

  1. Jim Randell 1 February 2014 at 8:55 am

    This Python programs runs in 48ms.

    from itertools import combinations, permutations
    from enigma import irange, nconcat, printf
    
    # consider 4 digit combinations
    for s in combinations(irange(1, 9), 4):
      # what numbers can be made from them
      ns = list(nconcat(p) for p in permutations(s))
      # the total sum
      t = sum(ns)
      # subtract one of them
      for n in ns:
        p = t - n
        # check the number matches 122??0
        if p // 1000 == 122 and p % 10 == 0:
          printf("n={n} [digits={s}, total={t}, penny={p}]")
    

    Solution: The subtracted number is 3754.

    Originally I computed the sum of all 24 possible numbers as [[ 6666 * sum(s) ]], but as I had to generate all the permutations anyway (and there’s only 24 of them) it seemed to be a bit clearer to let Python determine the sum, and it doesn’t affect the runtime of the program.

    • Hugh Casement 18 September 2014 at 4:29 pm

      I reasoned as follows:

      The partly heard number lies between 122004 and 122994.
      Divided by 6666 that’s 18.3 to 18.45.
      But one permutation of digits had been left out, so it’s reasonable to suppose the original digit sum was 19.
      19×6666 = 126654, so the missing number lies between 3664 and 4654 and ends in 4.

      The only one I can find in that range, with all digits different and summing to 19, is 3754.

  2. Naim Uygun 1 February 2014 at 10:57 am
    #Answer= 3754
    from itertools import permutations
    for p in permutations("987654321",4):
        t=0
        L=[]
        for q in permutations(p,4):
            n=int("".join(q))
            L.append(n)
            t=t+n
        for i in range(0,24):
            x=t-L[i]
            if x%10 ==0 and x//1000 ==122:
                print("Answer=",L[i])
            break
                   
    
  3. Jim Randell 4 April 2019 at 2:22 pm

    Here’s a solution using the [[ SubstitutedExpression() ]] solver from the enigma.py library, that treats the puzzle as a single alphametic expression.

    It is based on the observation that the sum of all permutations of ABCD is 6666 × (A + B + C + D).

    It runs in 160ms.

    #!/usr/bin/env python -m enigma -r
    SubstitutedExpression
    
    --digits="1-9"
    
    "match(6666 * (A + B + C + D) - ABCD, '122??0')"
    

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: