Let $S$ be the sequence of all positive integers whose decimal digits add to exactly 7, in increasing order:
$$S = \langle7, 16, 25, \ldots, 70, 106, 115, 124, \ldots 160, 205, \ldots, 10230010, \ldots\rangle$$
What is an efficient algorithm for finding the $n$th element of this sequence for a given positive integer $n$?
Observation: after $7,000,000$, all subsequent numbers from this list will be formed by sequentially inserting $0$'s into the $7$ digit numbers in the list (since after $1,111,111$, there can be no "new" combinations). As such, if we can formulate the sequence up to $7,000,000$, we'll be done in general.
Aim: to efficiently calculate the sequence up to $7,000,000$, at which point all future terms depend on previous terms. In doing so, generate a list of all 7 digit combinations, which will become the "base" for all further terms.
The method:
Some more detail:
In base $10$, the trick for working out if a number is divisible by $9$ is if and only if the sum of the digits is a multiple of $9$. The proof for this is slightly enlightening:
$$a_na_{n-1}\ldots a_0 = \sum_{i=0}^na_i10^i\equiv \sum_{i=0}^na_i\mod9$$ as $10\equiv 1 \mod 9$. What's important here is that $10 = 9+1$. The same thing will work in base $8$ with multiples of $7$
When we translate the multiples of $7$ into base $8$, we get:
$$7\mapsto7\\14\mapsto16\\21\mapsto 25\\28\mapsto34\\35\mapsto43\\\vdots$$
In base $8$ the multiples of $7$ are exactly the numbers whose digits sum to a multiple of $7$ in numerical order. If we then interpret the numbers we get as if they were in base $10$, we will get an almost one-to-one correspondence with the base $10$ integers whose digit sum to $7$.
Now this will also include numbers whose digits sum to $14, 21, \ldots$.