Let\'s define eleven-non-free
numbers:
If we consider a number as a string, then if any substring inside is a (non-zero) power of 11
, then this
It's interesting how easily people get scared by the term "brute force" without even trying to quantify it :)
where R is the result (k
-th eleven-non-free number). You just test all numbers 1..R and for each of them you perform string searches for 11, 121, 1331, 14641 etc. using prepared automaton (for given number of digits). O(R * log R)
doesn't look that bad, if you look at it this way, does it? :-)
Clever idea is to try to generate the number more directly:
k
-th number;k
eleven-non-free numbers;Solution 1 would have to be very clever, if not close to impossible. Solution 2 seems better than brute force solution because it skips those numbers which are not eleven-non-free. Solution 3 would be an easily implemented alternative, which brings us to important question:
Easy combinatorics (works exactly for n <= 10; for higher n it is close approximation, neglecting that 112 is substring of 1113 and 11 is substring of 1111 and 1119):
so basicly for the limit 10n you get more than 10n-2 solutions! This means that number of solutions is a constant proportion of the limit, which means that
which implies that
The dreaded brute force solution kooks much much better now, doesn't it? Yet it's still the same :)
O(k)
but you would have to be very careful to achieve this. There will be many complications that will make it uneasy to select next smallest eleven-non-free-number. E.g. when generating all 11xxx, 121xx, 1331x, eg. 13121 falls in between, making any automatic generation of ordered numbers difficult, let alone duplicities caused by pattern appearing in xxx digits etc. You will probably end up with some complicated data structure that will force O(log k)
in each step, and thus O(k log k)
in total.O(k log k)
, which is the same as to find k
-th number.