Enigmatic Code

Programming Enigma Puzzles

Enigma 189: Roll up!

From New Scientist #1334, 2nd December 1982 [link]

I have just organised a national tombola. The tickets are numbered consecutively from 1 upwards. The purchaser of a ticket wins a prize if the number on his ticket is palindromic (i.e. it reads the same when its digits are written in the reverse order). For example a bottle of lemonade goes to the buyer of ticket number 7, a bottle of wine goes to the buyer of ticket number 3003, and a bottle of champagne goes to the buyer of the highest winning ticket printed.

The number of tickets printed has been chosen so that precisely 1½ per cent of them are eligible for prizes.

What number will win the bottle of champagne?



One response to “Enigma 189: Roll up!

  1. Jim Randell 6 May 2014 at 8:26 am

    This Python program runs in 55ms.

    from itertools import count
    from enigma import printf
    def is_palindrome(n):
      s = str(n)
      return s == s[::-1]
    # record the number of palindromes, and the last one found
    (np, p) = (0, None)
    for n in count(1):
      if is_palindrome(n):
        np += 1
        p = n
      # stop when np is 1.5% of n
      if 200 * np == 3 * n:
        printf("p={p} [n={n}, np={np}]")

    Solution: The bottle of champagne is won by ticket number 19591.

    There are 19600 tickets printed, and 294 of them are palindromic.

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: