A
simple backtracking algorithm would take too much time to find the
numbers. First, note that all the sequences are exactly of length 4,
because of the limit for the number of digits. The first number should
be less than 1.25*10
K and greater than or equal to 10
K.
We will use the predicate
isMotley(n) iff n is motley. A
double-motley number n is such that isMotley(n) and isMotley(2*n). A
triple-motley is isMotley(n) and isMotley(2*n) and isMotley(4*n). A
quadruple-motley is isMotley(n) and isMotley(2*n) and isMotley(4*n) and isMotley(8*n).
All
sequences, therefore, should start with a quadruple-motley number.
Generating a sequence of quadruple-motley numbers may be difficult,
hence we generate all the triple-motley numbers and check each if it
is quadruple-motley.
Let's start with a simple observation: each part of a motley number
N of a motley number. Thus, if
N=N1N2 (concatenation), then both
N1 and
N2 are motley. That is not always true for double-motley numbers: if
N=N1N2 is double-motley, than
N1 should be double-motley only if
N2 starts with a digit 0 .. 4.
N2 should be double-motley, too. Indeed, by multiplying
N2 by 2 we get a motley number and a carrier, which is 0, if the first digit of
N2 is in the range 0 .. 4, or 1, if the first digit of
N2 is in the range 5 .. 9. we should extend the predicate 'double-motley' by a parameter 'carry bit':
double-motley (n, 0) = isMotley(n) and isMotley(2*n)
double-motley (n, 1) = isMotley(n) and isMotley(2*n+1)
Similarly, for a triple-motley number
N=N1N2 we have four cases:
- N2 starts with digits 00 .. 24
- N2 starts with digits 25 .. 49
- N2 starts with digits 50 .. 74
- N2 starts with digits 75 .. 99
This fact is ensued by two multiplications, thus two carry bits:
triple-motley(n, c1, c2) = double-motley (n, c1) and double-motley(2*n+c1, c2)
We will generate numbers by adding a digit
d to the 'current' number
N1: and checking condition
triple-motley(N1d, c1, c2) for each combination of
c1 and
c2. The combination will tell us the range for values of
dN2, thus reducing the number of combinations required to enumerate the triple-motley numbers.
See
output for K=10.