Enigmatic Code

Programming Enigma Puzzles

Enigma 1425: Spell check

From New Scientist #2586, 13th January 2007

In the following statements and questions each digit has consistently been replaced by a capital letter, with different letters used for different digits.

ONE, FOUR and NINE are perfect squares.

Also, more than one of the following statements is true:

EIGHT is even;
EIGHT + 1 is a  perfect square;
EIGHT + ONE is a perfect square;
EIGHT – 1 is a prime;
EIGHT – ONE is a prime;
EIGHT + 7 is the product of two primes;
EIGHTEEN is divisible by 3;
EIGHTEEN is divisible by THREE;
EIGHTEEN – 2 is a perfect square.

What is the RIGHT number?

[enigma1425]

Advertisements

One response to “Enigma 1425: Spell check

  1. Jim Randell 2 June 2013 at 9:00 am

    This Python program solves the puzzle in 119ms. When the puzzle was published I originally solved it with a much messier (but faster) Perl script.

    from itertools import permutations
    from enigma import irange, split, nconcat, is_square, is_prime, factor, printf
    
    # if leading digits cannot be 0 then O, F, N, E, T, R are not zero
    
    digits = set(irange(0, 9))
    
    # find O, N, E
    for i in irange(10, 31):
      ONE = i * i
      (O, N, E) = split(ONE, int)
      if 0 in (O, N, E): continue
      ds1 = digits.difference((O, N, E))
      if len(ds1) != 7: continue
    
      # find I
      for I in ds1:
        NINE = nconcat(N, I, N, E)
        if not is_square(NINE): continue
    
        # find G, H, T, R
        ds2 = ds1.difference((I,))
        for (G, H, T, R) in permutations(ds2, 4):
          if 0 in (T, R): continue
          EIGHT = nconcat(E, I, G, H, T)
          EIGHTEEN = nconcat(E, I, G, H, T, E, E, N)
          THREE = nconcat(T, H, R, E, E)
    
          # evaluate the statements
          ss = (
            # "EIGHT is even"
            (EIGHT % 2 == 0),
            # "EIGHT + 1 is a perfect square"
            (is_square(EIGHT + 1) is not None),
            # "EIGHT + ONE is a perfect square"
            (is_square(EIGHT + ONE) is not None),
            # "EIGHT - 1 is a prime"
            is_prime(EIGHT - 1),
            # "EIGHT - ONE is a prime"
            is_prime(EIGHT - ONE),
            # "EIGHT + 7 is the product of two primes"
            (len(factor(EIGHT + 7)) == 2),
            # "EIGHTEEN is divisible by 3"
            (EIGHTEEN % 3 == 0),
            # "EIGHTEEN is divisible by THREE"
            (EIGHTEEN % THREE == 0),
            # "EIGHTEEN - 2 is a perfect square"
            (is_square(EIGHTEEN - 2) is not None)
          )
          # more than one of them should be true
          if ss.count(True) < 2: continue
    
          # find F, U
          ds3 = ds2.difference((G, H, T, R))
          for (F, U) in permutations(ds3, 2):
            if F == 0: continue
            # FOUR should be a perfect square
            FOUR = nconcat(F, O, U, R)
            if not is_square(FOUR): continue
    
            printf("RIGHT={R}{I}{G}{H}{T} [O={O} N={N} E={E} F={F} U={U} {ss}]")
    

    Solution: RIGHT = 45798.

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: