Enigmatic Code

Programming Enigma Puzzles

Enigma 326: Tombola ups and downs

From New Scientist #1474, 19th September 1985 [link]

We are going to run a tombola at our big local charity ball. We’ve had a particular number of tickets printed, numbered consecutively from 1 upwards. We asked the printer to put “Prize-winner” on all those tickets whose number had its digits increasing from start to finish (e.g. 9, 37, 256, etc), but in error he marked those tickets whose numbers had their digits decreasing from start to finish (e.g. 7, 54, 310, etc). (Single digit numbers were intended to be winners and were printed as winners).

By a great coincidence this meant that there were as many prize-winning tickets as we had wished. But had the number of tombola tickets been any higher that could not have been the case.

How many tickets were there?

[enigma326]

One response to “Enigma 326: Tombola ups and downs

  1. Jim Randell 1 January 2016 at 8:27 am

    This Python program finds intervals where the numbers of “ups” (numbers with increasing digits) and “downs” (numbers with decreasing digits) are equal. It runs in 44ms.

    from itertools import combinations
    from enigma import irange, concat, compare, printf
    
    # consider numbers of length l
    n = u = d = m = 0
    for l in irange(1, 10):
      # increasing numbers (0 can't occur)
      ups = list(int(concat(*x)) for x in combinations('123456789', l))
      # decreasing numbers
      downs = list(int(concat(*x)) for x in combinations('9876543210', l))
      downs.reverse()
      # remove 0 (which only can happen when l = 1)
      if l == 1 and downs[0] == 0: downs.pop(0)
    
      # consider the next number in the lists
      while ups or downs:
        n0 = n
        n = min(ups[:1] + downs[:1])
        if u == d:
          m = n - 1
          printf("[{n0},{m}] u=d={u}")
        if ups and n == ups[0]:
          ups.pop(0)
          u += 1
        if downs and n == downs[0]:
          downs.pop(0)
          d += 1
    
    printf("max={m}")
    

    Solution: There were 8519 tickets.

    There are 255 “ups” and “downs”.

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: