Enigmatic Code

Programming Enigma Puzzles

Enigma 1291: Carrés Pairs et Impairs

From New Scientist #2449, 29th May 2004 [link]

In the following statements digits have been consistently replaced by capital letters, different letters being used for different digits:

UN and NEUF are odd perfect squares, ZERO is an even perfect square.

Please send in the numerical value of the square root of (UN × NEUF × ZERO).

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment. The current estimate is that the line will be connected at the end of September 2014.



4 responses to “Enigma 1291: Carrés Pairs et Impairs

  1. Jim Randell 25 September 2014 at 3:07 pm

    This Python program runs in 31ms

    from enigma import irange, is_duplicate, printf
    # generate lists of squares with non-repeating digits as strings
    def squares(lo, hi, step=1):
      r = dict()
      for i in irange(lo, hi, step=step):
        s = str(i * i)
        if not is_duplicate(s): r[s] = i
      return r
    # odd 2-digit squares
    s1 = squares(5, 9, step=2)
    # odd 4-digit squares
    s2 = squares(33, 99, step=2)
    # even 4-digit squares
    s3 = squares(34, 98, step=2)
    # choose UN
    for (UN, r1) in s1.items():
      # choose NEUF
      for (NEUF, r2) in s2.items():
        # check U and N match
        if not(NEUF[2] == UN[0] and NEUF[0] == UN[1]): continue
        # choose ZERO
        for (ZERO, r3) in s3.items():
          # check E matches
          if not(ZERO[1] == NEUF[1]): continue
          # and the rest are distinct
          if len(set(UN + NEUF + ZERO)) != 7: continue
          # compute the answer
          r = r1 * r2 * r3
          printf("root={r} [UN={UN} NEUF={NEUF} ZERO={ZERO}]")

    Solution: The square root of (UN × NEUF × ZERO) is 24948.

    • Jim Randell 30 July 2016 at 11:33 pm

      Puzzles like this can now be solved directly using the general Alphametic solver (SubstitutedExpression()) that is now part of the enigma.py library, without the need to write a program at all:

      % python -m enigma SubstitutedExpression \
          --answer="is_square(UN * NEUF * ZERO)" \
          "is_square(UN) % 2 = 1" \
          "is_square(NEUF) % 2 = 1" \
          "is_square(ZERO) % 2 = 0"
      (is_square(UN) % 2 = 1) (is_square(NEUF) % 2 = 1) (is_square(ZERO) % 2 = 0)
      (is_square(81) % 2 = 1) (is_square(1089) % 2 = 1) (is_square(7056) % 2 = 0) / E=0 F=9 N=1 O=6 R=5 U=8 Z=7 / 24948
      is_square(UN * NEUF * ZERO) = 24948 [1 solution]

      The whole thing runs in 69ms.

      This shows the use of the --answer=<expr> parameter, which collects answers by the substituted value of the specified expression.

      It also shows how values that cause the expressions we are searching on to throw exceptions are ignored. The is_square() function will return None if the argument is not a perfect square, which, in turn, will cause the mod() (%) function to throw a TypeError, as it has not been passed a number. However, the program that is generated by the SubstitutedExpression() solver ignores these errors and looks for other values that make the expression work without such errors.

      One case where the error is not ignored is if a NameError is thrown. This is because you might accidentally mistype a function name, like "is_squared(UN)", and wonder why you don't get any solutions.

  2. Naim Uygun 25 September 2014 at 4:00 pm
    #Answer: 24948
    from itertools import permutations
    for n,e,u,f,z,r,o in permutations(range(0,10),7):
        if o%2 != 0: continue
        if 0 in [n,u,z]:continue
        if round(r1)**2 != neuf : continue
        if round(r2)**2 !=zero: continue
        if round(r3)**2 != un : continue
  3. Paul 27 September 2014 at 12:21 pm

    a MMA version



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: