Enigmatic Code

Programming Enigma Puzzles

Enigma 1412: Any amount

From New Scientist #2572, 7th October 2006

I have a five-digit TOTAL, a six-digit AMOUNT, and several other whole numbers, all expressed in words written in capital letters, where different letters stand for different digits and the same letter stands for the same digit.

A few curious properties are noticeable. For instance, if you add ANY amount to my TOTAL, and then divide by say, A value, the result is a whole number.

And if you subtract AN amount from my AMOUNT then divide by say, ANY value, the result, of course, is ANY amount.

How much is my five-digit VALUE?

There are now 457 Enigma puzzles up on the site – which means there are only 1300 “Classic” puzzles remaining to publish!

[enigma1412]

Advertisements

3 responses to “Enigma 1412: Any amount

  1. Jim Randell 15 July 2013 at 9:57 am

    This Python program runs in 34ms. It takes a different approach to the problem than the solution I coded up in Perl when the puzzle was originally published and is much faster.

    from itertools import permutations
    from enigma import irange, split, printf
    
    # (AMOUNT - AN) / ANY = ANY
    # so:
    # ANY^2 + AN = AMOUNT
    for i in irange(100, 999):
      ANY = str(i)
      if len(set(ANY)) != 3: continue
      AN = ANY[0:2]
      AMOUNT = str(i * i + int(AN))
      # AMOUNT should be composed 6 different digits
      if len(AMOUNT) != 6 or len(set(AMOUNT)) != 6: continue
      # and the A and N should match
      (A, N, Y) = split(ANY)
      if AMOUNT[0] != A or AMOUNT[4] != N: continue
      # and Y should occur in AMOUNT
      if Y in AMOUNT: continue
      # (TOTAL + ANY) % A == 0
      (T, O) = AMOUNT[5], AMOUNT[2]
      L = str(int(A) - (10 * int(T + O + T + A) + i) % int(A))
      # V and E are what's left
      for (V, E) in permutations(set('0123456789').difference(ANY + AMOUNT + L)):
        if V == '0': continue # but V is non-zero
        VALUE = V + A + L + AMOUNT[3] + E
        printf("VALUE={VALUE} [ANY={ANY} AMOUNT={AMOUNT}]")
    

    Solution: VALUE = 89140.

  2. geoffrounce 16 July 2013 at 9:29 pm

    A simple, short permutation solution, but with a longer run time :

    from itertools import permutations
    
    for p in permutations((1,2,3,4,5,6,7,8,9,0),10):
        t,o,a,l,m,u,n,y,e,v = p
        if all (x !=0 for x in(t,a,v)):
            an = n + 10*a
            any = y + n*10 + a*100
            total = l + a*10 + t*100 + o*1000 + t*10000
            value = e + u*10 + l*100 + a*1000 + v*10000
            amount = t + n*10 + u*100 + o*1000 + m*10000 + a*100000
            if (total + any) % a == 0:
                if any**2 + an == amount:     # jim's simplification
                    print('VALUE = ', value)
    
  3. Jim Randell 8 September 2016 at 9:55 am

    We can use the general Alphametic solver (SubstitutedExpression()) from the enigma.py library to solve this.

    Here is the command and it’s output. Overall run time is 71ms.

    % python -m enigma SubstitutedExpression \
        --answer="VALUE" \
        "(ANY + TOTAL) % A = 0" \
        "ANY ** 2 + AN = AMOUNT"
    ((ANY + TOTAL) % A = 0) (ANY ** 2 + AN = AMOUNT) (VALUE)
    ((963 + 57591) % 9 = 0) (963 ** 2 + 96 = 927465) (89140) / A=9 E=0 L=1 M=2 N=6 O=7 T=5 U=4 V=8 Y=3
    VALUE = 89140 [1 solution]
    

    Or you can put the expressions in a file and just tell enigma.py to run it:

    SubstitutedExpression
    
    --answer="VALUE"
    
    "(ANY + TOTAL) % A = 0"
    "ANY ** 2 + AN = AMOUNT"
    
    % python -m enigma -r enigma1412.run
    ((ANY + TOTAL) % A = 0) (ANY ** 2 + AN = AMOUNT) (VALUE)
    ((963 + 57591) % 9 = 0) (963 ** 2 + 96 = 927465) (89140) / A=9 E=0 L=1 M=2 N=6 O=7 T=5 U=4 V=8 Y=3
    VALUE = 89140 [1 solution]
    

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: