Enigmatic Code

Programming Enigma Puzzles

Enigma 1503: Division decision

From New Scientist #2665, 19th July 2008

In the long-division sum shown, all odd digits have been replaced by dashes and all even digits by asterisks. There is no remainder.

Enigma 1503

What is the 5-digit dividend?



One response to “Enigma 1503: Division decision

  1. Jim Randell 21 October 2012 at 8:40 pm

    The following Python program runs in 36ms.

    from enigma import irange, nconcat, printf
    # check the number <n> has digits corresponding to the sets <ds>
    def check(n, *ds):
      s = str(n)
      if len(s) != len(ds): return False
      return all(int(a) in b for a, b in zip(s, ds))
    odd = set((1, 3, 5, 7, 9))
    evenp = set((2, 4, 6, 8))
    even = evenp.union([0])
    # possible values for the divisor
    for a1 in odd:
      for a2 in odd:
        aa = nconcat(a1, a2)
        # work through each digit in the dividend
        for c1 in odd:
          for c2 in odd:
            (b1, r1) = divmod(c1 * 10 + c2, aa)
            if not check(b1, evenp): continue
            if not check(r1, odd, odd): continue
            if not check(b1 * aa, odd, even): continue
            for c3 in even:
              (b2, r2) = divmod(r1 * 10 + c3, aa)
              if not check(b2, odd): continue
              if not check(r2, odd): continue
              if not check(b2 * aa, odd, even, odd): continue
              for c4 in odd:
                (b3, r3) = divmod(r2 * 10 + c4, aa)
                if not check(b3, odd): continue
                if not check(r3, odd, even): continue
                if not check(b3 * aa, odd, odd): continue
                for c5 in even:
                  (b4, r4) = divmod(r3 * 10 + c5, aa)
                  if not check(b4, even): continue
                  if r4 != 0: continue
                  if not check(b4 * aa, odd, even, even): continue
                  printf("{c1}{c2}{c3}{c4}{c5} / {aa} = {b1}{b2}{b3}{b4}")

    Solution: The five-figure dividend is 93018.

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: