Enigmatic Code

Programming Enigma Puzzles

Enigma 1683: Lottery numbers

From New Scientist #2850, 4th February 2012 [link]

A lottery draw, which is televised, uses balls numbered 01 to 39, from which five winning balls are selected and displayed in a row. By the magic of the producer’s graphics software, they are then rearranged into ascending order from left to right, to make checking easier.

One week, two of the winning balls had values less than 10, and the number obtained by reading (from left to right) all 10 digits as a single number was exactly 10 times the number similarly obtained after the balls had been rearranged.

What were the numbers of the five balls, in their original order?

[enigma1683]

Advertisements

One response to “Enigma 1683: Lottery numbers

  1. jimrandell 1 February 2012 at 6:58 pm

    The following Python program runs in 874ms.

    from itertools import combinations, permutations
    from enigma import printf
    
    # if a:b:c:d:e is 10x the sorted values it follows e = 10, 20, 30, 40
    
    # chunk the string into blocks of n
    def chunk(s, n):
      if len(s) == 0: return []
      elif len(s) < n: return [s]
      return [s[0:n]] + chunk(s[n:], n)
    
    for (a, b) in combinations(range(1, 10), 2):
      for (c, d) in combinations(range(10, 40), 2):
        for e in (10, 20, 30):
          if e in (c, d): continue
    
          # the numbers sorted as a 10-digit integer (represented as a string)
          # (which will have a leading zero)
          s = "".join("{:02d}".format(n) for n in sorted((a, b, c, d, e)))
    
          # drop the leading zero and turn this number*10 into a group of two digit integers
          t = list(int(i) for i in chunk(s[1:] + '0', 2))
    
          # see if it corresponds to the sorted numbers
          if not set(t).issuperset((a, b, c, d, e)): continue
    
          printf("drawn: {t}, sorted: {s}", s=list(int(i) for i in chunk(s, 2)))
    

    Solution: The five balls in the order drawn are: 20, 31, 02, 03 and 10.

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: