Enigmatic Code

Programming Enigma Puzzles

Enigma 1014: Mirror image

From New Scientist #2170, 23rd January 1999 [link]

Harry was playing about with his calculator and keyed in a 4-digit number. He placed a mirror behind and parallel to the display, and added the reflected number, which was smaller, to the number on the display. This gave him a 5-digit sum.

He then again keyed in the original number, and this time subtracted the reflection from it.

He divided the sum by the difference and found that the quotient was a 4-digit prime.

What was his original number?

[enigma1014]

One response to “Enigma 1014: Mirror image

  1. Jim Randell 19 April 2019 at 9:02 am

    This Python program runs in 100ms.

    Run: [ @repl.it ]

    from itertools import product
    from enigma import nconcat, is_prime, printf
    
    # digits that can be successfully mirrored
    mirror = { 0: 0, 1: 1, 2: 5, 3: 3, 5: 2, 8: 8 }
    
    # consider the initial 4 digits keyed in
    for ds in product(mirror.keys(), repeat=4):
    
      # the initial 4-digit number
      n = nconcat(ds)
      if n < 1000: continue
    
      # the mirrored number (is less than n)
      m = nconcat(mirror[d] for d in ds)
      if not(m < n): continue
    
      # their sum is a 5-digit number
      s = n + m
      if s < 10000: continue
    
      # their difference
      d = n - m
    
      # the quotient s/d is a 4-digit prime
      (q, r) = divmod(s, d)
      if r != 0 or q < 1000 or not is_prime(q): continue
    
      printf("n={n} m={m}, s={s} d={d}, q={q}")
    

    Solution: The original number was 8805.

    The reflected number is 8802. Giving a sum of 17607, and difference of 3. The quotient is 17607 / 3 = 5869.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: