# Enigmatic Code

Programming Enigma Puzzles

## Enigma 374: Just the ticket

From New Scientist #1523, 28th August 1986 [link]

Mr Bagel was intrigued when approached by Mr Bola selling raffle tickets, for he had never taken part in a raffle.

“I see that each ticket in your book has the same number of digits on it, the first having a number of zeros followed by a one, and the number on each successive ticket increasing by one.”

“That’s true,” replied Bola. “I haven’t sold any yet. Perhaps that’s because there is to be only one winning ticket.”

“Now tell me, Tom,” asked Bagel, “what happens if a ticket number is composed entirely of invertible digits, namely 0, 1, 8, 6 or 9, so that is also forms a number when viewed upside down?”

“In a draw we always read the tickets out with the perforation on the left,” replied Bola.

“That’s a pity, otherwise one could buy two numbers for the price of one ticket.”

Bagel, being superstitious, chose a ticket with an invertible number. One way up the number was divisible by all the even digits, and the other way up it was divisible by all the odd digits. Moreover, when his ticket number was multiplied by a digit (I forget which), the product was the number of the last ticket in the book, a number in which none of the digits was invertible.

I forget whether he won the draw, or even what the draw was for. But, given the chances of his winning were better than one in 100,000, what was the number on the last ticket in the book?

[enigma374]

### 3 responses to “Enigma 374: Just the ticket”

1. Jim Randell 12 December 2016 at 7:21 am

The chances of winning are less than 1 in 100,000, so the number of digits in the numbers on the tickets is 5 or less.

This Python program runs in 69ms.

from itertools import product
from enigma import irange, nconcat, nsplit, printf

# invertible digits
inv = { 0: 0, 1: 1, 6: 9, 8: 8, 9: 6 }

# consider increasing numbers of digits
for d in irange(1, 5):

# consider invertible numbers
for ds in product(sorted(inv.keys()), repeat=d):

# the number both ways up
n1 = nconcat(ds)
if n1 == 0: continue
n2 = nconcat(inv[d] for d in ds[::-1])

# one way up the number is divisible by the even digits (except 0)
# the other way up the number is divisible by the odd digits
# mlcm(2, 4, 6, 8) = 24, mlcm(1, 3, 5, 7, 9) = 315
if not((n1 % 24 == 0 and n2 % 315 == 0) or (n2 % 24 == 0 and n1 % 315 == 0)): continue

# the number multiplied by a digit is the number of the last ticket
for n in irange(2, 9):
f = n * n1
if f > 99999: break
fds = nsplit(f)
if len(fds) > d: break
# which contains no invertible digits
if len(fds) < d: continue
if any(x in inv for x in fds): continue

printf("[d={d}] ticket = {n1}, last ticket = {f}", n1=str(n1).zfill(d))

Solution: The number of the last ticket in the book is 77544.

Bagel’s ticket was number 08616.

8616 is divisible by 2, 4, 6, 8.

91980 is divisible by 3, 5, 7, 9.

Without the constraint on the chances of winning, we get further solutions starting with 8 digit tickets:

[d=8] ticket = 06061608, last ticket = 54554472
[d=8] ticket = 06110688, last ticket = 24442752
[d=8] ticket = 09190968, last ticket = 73527744

2. Brian Gladman 12 December 2016 at 8:25 pm
from itertools import product
from functools import reduce

# map for inverting invertible digits
d2d = {0:0, 1:1, 6:9, 8:8, 9:6}

# the divisors for all odd and all even digit division
do, de = 315, 24

# compose a number from a sequence of digits
fn = lambda s : reduce(lambda x, y: 10 * x + y, s)

fs = 'The last ticket number was {1:} ({2:0{0:}d}, {3:0{0:}d}).'

# the number of digits in the tickets
for nd in range(1, 7):
# the digits in the ticket
for digits in product(d2d.keys(), repeat=nd):

# convert the digit sequence and its 'reverse' and check that
# they are non-zero and divisible by all the even/odd digits
n1, n2 = fn(digits), fn(d2d[x] for x in digits[::-1])
if n1 and n1 % de == n2 % do == 0:

# now multiply the numbers by a digit between 2 and 10
# and check for a result that has no invertible digits
for nbr in (n1, n2):
for m in range(2, 10):
if not set(str(m * nbr)).intersection('01689'):
print(fs.format(nd, m * nbr, n1, n2))

It seems that the 1 in 100,000 chance of winning was intended to limit the number of digits in tickets to five in order to give a unique solution as reported by Jim. However the six digit ticket number 011088 (880110 reversed) gives a lower answer of 44352 for the final ticket.

• Jim Randell 12 December 2016 at 10:04 pm

If we allow all the ticket numbers to be padded with leading zeros we get two families of solutions:

(1) The last ticket in the book is number 77544, and Bagel’s ticket is number 8616. The number printed on each ticket is padded with a number of zeros to make whatever digit length (≥ 5) we require.

(2) The last ticket in the book is number 44352, and Bagel’s ticket is number 11088. Again, the number printed on each ticket is padded with zeros to make whatever digit length (≥ 6) we require.

And as the ticket numbers get longer we get further families of solutions.

Although in all these cases the number on the inverted ticket is larger than the number of the final ticket, so maybe we should not really consider them to be “two tickets for the price of one” (as one of the numbers can never win). But if we consider that then there is no solution to the puzzle.

I took the view that the lower number tickets were zero padded to bring them up to the same digit length as the final ticket, but that the final ticket was not itself padded with zeros.

This gives us the unique solution given above, so is probably what the setter had in mind.

But the wording in the puzzle could have been clearer to eliminate the additional solutions.

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