Enigmatic Code

Programming Enigma Puzzles

Enigma 1476: Portuguese squares

From New Scientist #2638, 12th January 2008

Zero, um, nove and cem are the Portuguese for 0, 1, 9 and 100; so it is appropriate that I can make the following statement: ZERO, UM, NOVE and CEM are perfect squares.

In this statement digits have been consistently replaced by capital letters, different letters being used for different digits. No number starts with a zero.

Find the numerical value of the square root of (ZERO × UM × NOVE × CEM).



2 responses to “Enigma 1476: Portuguese squares

  1. Jim Randell 4 December 2012 at 3:56 pm

    Originally I searched the squares and matched up the letters, which may be a bit faster (it ran in 35ms), but my normal way of using itertools.permutations() results in a more compact program, and it runs in 36ms.

    from itertools import permutations
    from enigma import irange, is_duplicate, multiply, is_square, printf
    # we need 2, 3 and 4 digit squares (with no repeated digits)
    s = [[] for i in range(5)]
    for i in irange(4, 99):
      n = str(i * i)
      if is_duplicate(n): continue
    ds = set('0123456789')
    for ZERO in s[4]:
      (Z, E, R, O) = ZERO
      ds1 = ds.difference((Z, E, R, O))
      for (N, V) in permutations(ds1, 2):
        NOVE = N + O + V + E
        if NOVE not in s[4]: continue
        ds2 = ds1.difference((N, V))
        for (C, M) in permutations(ds2, 2):
          CEM = C + E + M
          if CEM not in s[3]: continue
          ds3 = ds2.difference((C, M))
          for U in ds3:
            UM = U + M
            if UM not in s[2]: continue
            # compute the result
            r = is_square(multiply(int(s) for s in (UM, CEM, NOVE, ZERO)))
            printf("{r} [UM={UM} CEM={CEM} NOVE={NOVE} ZERO={ZERO}]")

    Solution: The value of the square root is 1240092.

    • Jim Randell 5 July 2016 at 9:33 am

      Using the general alphametic solver from the enigma.py library means you don’t have to write a program at all:

      % python -m enigma SubstitutedExpression \
          "is_square(ZERO)" \
          "is_square(UM)" \
          "is_square(NOVE)" \
      (is_square(ZERO)) (is_square(UM)) (is_square(NOVE)) (is_square(CEM))
      (is_square(9604)) (is_square(81)) (is_square(5476)) (is_square(361)) / C=3 E=6 M=1 N=5 O=4 R=0 U=8 V=7 Z=9

      It runs in 76ms, and gives us the assignment of letters to digits, then a simple calculation gives the 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: