Enigmatic Code

Programming Enigma Puzzles

Enigma 1655: Number spotting

From New Scientist #2821, 16th July 2011 [link]

I have been playing some number games with my nephew using five dice. For example, when viewing the dice as shown, we would interpret the number on the front as 23114 and the number on top as 11532.

On one occasion I placed the five dice in a row on a table and commented that the five-figure number on the front was a prime, with all its digits different, and that the five-figure number on the top was a perfect square.

My nephew, who was viewing the row from the other side, said exactly the same was true for him, but that his square was higher than mine.

Which prime and which square could I see?

[enigma1655]

Advertisements

One response to “Enigma 1655: Number spotting

  1. jimrandell 4 December 2011 at 3:56 pm

    The following Python code runs in 110ms.

    from enigma import irange, is_prime
    
    # we need only consider numbers containing the digits 1-6
    digits = set('123456')
    
    # we need a five digit square, whose reverse is also a square
    squares = list()
    for i in irange(106, 258):
      s = i * i
      if not set(str(s)).issubset(digits): continue
      squares.append(s)
    
    candidate_squares = list()
    for s in squares:
      r = int(str(s)[::-1])
      if not(s < r): continue
      if not(r in squares): continue
      candidate_squares.append((s, r))
    
    # and we need five digit primes A:B:C:D:E, st. 7-E:7-D:7-C:7-B:7-A is also prime
    primes = list()
    for i in irange(11111, 66666):
      s = set(str(i))
      if not len(s) == 5: continue
      if not s.issubset(digits): continue
      if not is_prime(i): continue
      primes.append(i)
    
    candidate_primes = list()
    for p in primes:
      r = 77777 - int(str(p)[::-1])
      if not(p < r): continue
      if not(r in primes): continue
      candidate_primes.append((p, r))
    
    def check(s, p):
      for (a, b) in zip(str(s[0]), str(p[0])):
        if a == b: return False
        if a == str(7 - int(b)): return False
    
    # now see which combinations are possible
    for s in candidate_squares:
      for p in candidate_primes:
        check(s, p)
    

    Solution: The prime is 43651, and the square is 12544.

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: