Let's define the a function on natural numbers \$n\$, written as base 10 digits \$d_k\; d_{k-1}\; \dotsc\; d_1\; d_0\$, as follows:
As long as there are equal adjacent digits \$d_i\;d_{i-1}\$, replace them by their sum \$d_i+d_{i-1}\$ from left to right. If there were any such digits, repeat the same procedure.
In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).
Example
Let's take \$\texttt{9988}\$ for example:
- The first adjacent digits which are equal are the two \$\texttt{9}\$
- So we replace them by \$\texttt{9 + 9} = \texttt{18}\$ which gives us \$\texttt{1888}\$
- Since we're still in the first left-right traversal and there were still two \$\texttt{8}\$s we need to first replace these
- So we get \$\texttt{1816}\$
- Something changed, so we need to do another iteration
- But there are no such digits, so we stop
Therefore the \$9988^\text{th}\$ number in that sequence is \$1816\$.
Challenge
The first 200 terms are:
0,1,2,3,4,5,6,7,8,9,10,2,12,13,14,15,16,17,18,19,20,21,4,23,24,25,26,27,28,29,30,31,32,6,34,35,36,37,38,39,40,41,42,43,8,45,46,47,48,49,50,51,52,53,54,10,56,57,58,59,60,61,62,63,64,65,12,67,68,69,70,71,72,73,74,75,76,14,78,79,80,81,82,83,84,85,86,87,16,89,90,91,92,93,94,95,96,97,98,18,10,101,102,103,104,105,106,107,108,109,20,21,4,23,24,25,26,27,28,29,120,121,14,123,124,125,126,127,128,129,130,131,132,16,134,135,136,137,138,139,140,141,142,143,18,145,146,147,148,149,150,151,152,153,154,20,156,157,158,159,160,161,162,163,164,165,4,167,168,169,170,171,172,173,174,175,176,24,178,179,180,181,182,183,184,185,186,187,26,189,190,191,192,193,194,195,196,197,198,28
Your task is to generate that sequence, either
- given \$n\$, return the \$n^\text{th}\$ number in that sequence,
- given \$n\$, return the first \$n\$ numbers in that sequence
- or generate the sequence indefinitely.
You may choose your submission to use either \$0\$- or \$1\$-indexing, but please specify which.
Test cases
You may use the above given terms, however here are some larger ones:
222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818