# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1207: Nice ones

From New Scientist #2363, 5th October 2002 [link]

We had a gathering of our very respectable and mathematically inclined family recently to celebrate the forthcoming centenary of its oldest surviving member.

Out of interest I pointed out to them that, given any odd number which does not end in 5, some multiple of it must consist simply of a string one ones. For example, if you look at 71 then the number 1111111… consisting of thirty-five ones is a multiple of it!

This set the family chattering and Alan wrote down the shortest string one ones which was a multiple of his age.

Alan’s father, Bob, then rubbed out a majority of the ones to leave the shortest string of ones which was a multiple of his age. Bob’s grandfather, Colin, then rubbed out a majority of the remaining ones to leave the shortest string of ones which was a multiple of his age.

How old is Bob?

[enigma1207]

### One response to “Enigma 1207: Nice ones”

1. Jim Randell 27 August 2015 at 8:43 am

See Enigma 1212 for more fun with repdigits.

To get to a unique solution I required at least 16 years between generations. (i.e. Bob must be at least 16 years older than Alan, and Colin must be at least 32 years older than Bob).

This Python program runs in 33ms.

```from enigma import irange, printf

# find the first repdigit of <d> that is a multiple of <m>
# return the length of the repdigit
def repdigit(d, m, base=10):
r = d % m
n = 1
while r > 0:
r = (r * base + d) % m
n += 1
return n

# check the examples
assert repdigit(1, 71) == 35

# record repunit lengths for ages less than 100
length = dict()
for i in irange(1, 99):
if not(i % 2 == 0 or i % 5 == 0):
length[i] = repdigit(1, i)

# minimum distance between generations
gen = 16

# consider Bob's age, B, and repunit length, b
for (B, b) in length.items():

# Alan's age, A, is less than B
# and his repunit length, a, is more than twice b
As = list((A, a) for (A, a) in length.items() if A + gen < B and a > b * 2)
if not As: continue

# Colin's age, C, in more than B
# and his repunit length, c, is less than half b
Cs = list((C, c) for (C, c) in length.items() if B + 2 * gen < C and c * 2 < b)
if not Cs: continue

printf("B={B} b={b}, As={As}, Cs={Cs}")
```

Solution: Bob is 53.