I once had a project which involved taking actual real social security numbers and anonymizing them into unique real-looking fake SSNs. One of the rules for SSNs is that it cannot contain a run of 666 inside it. That raised the question, is it possible to do this mathematically?
More generally, given this sequence
$$S = 1, 2, 3, ..., 665, 667, 668, ..., 1665, 1667, ..., 6658, 6659, 6670, ...$$
Is it possible to get the $n$th term without simply counting up to it?
A partial answer: This approach is based upon generating functions. Let $S(m)$ denote the positive integers $\leq m$ which do not contain three consecutive $6$. We calculate $S(m)$ for powers of $10$ and show the following is valid for $n\geq 0$:
\begin{align*} S\left(10^n\right)=1+\sum_{j=0}^{n}9^j\sum_{k=0}^{j+1}\binom{j+1}{k}\binom{k}{n-j-k}[[n-j\geq k]]\tag{1} \end{align*}
We use Iverson brackets $[[P]]$ in order to filter out invalid cases. Equivalently we can use $\min\{j+1,n-j\}$ as upper limit of the inner sum instead of $j+1$. We start with
Example: $n=4$:
The number of strings of length $4$ is given as $\color{blue}{9\,981}$ which counts the $10\,000$ words of length $4$ minus the following $19$ words: \begin{align*} 0666\ 1666\ 2666\ 3666\ 4666\ 5666\ 6666\ 7666\ 8666\ 9666\\ 6660\ 6661\ 6662\ 6663\ 6664\ 6665\ \ \qquad 6667\ 6668\ 6669 \end{align*}
We don't want to count words with leading zeros. Denoting with $[z^n]$ the coefficient of $z^n$ of a series we have to subtract $[z^{3}]A(z)$.
The number of valid words of length $4$ is \begin{align*} \left([z^4]-[z^{3}]\right)A(z)=9\,981-999=8\,982 \end{align*}
The number of all valid words of length $\leq 4$ is even simpler, since it is \begin{align*} &\left([z^4]-[z^{3}]\right)A(z)+\left([z^3]-[z^{2}]\right)A(z)+\left([z^2]-[z^{1}]\right)A(z)\\ &\qquad+\left([z^1]-[z^{0}]\right)A(z)+[z^0]A(z)\\ &\qquad=[z^4]A(z)\\ &\qquad\color{blue}{=9\,981} \end{align*}
We conclude \begin{align*} \color{blue}{S(10^4)}=1+S(9999)=1+[z^4]A(z)\color{blue}{=9\,982}\tag{6} \end{align*} and an general we have $S(10^n)=1+[z^n]A(z)$ for $n\geq 0$.
Comment:
In (7) we use the geometric series expansion.
In (8) we apply the rule $[z^{p-q}]A(z)=[z^p]z^qA(z)$ and restrict the upper limit of the sum with $n$ since greater indices do not contribute to the coefficient of $z^n$.
In (9) we expand the binomial.
In (10) we apply the rule as we did in (8) and ensure with the help of the Iverson brackets that the power $n-j-k$ of $z$ is non-negative.
In (11) we finally select the coefficient of $z^{n-j-k}$.