Let's define a function \$f\$ which, given a positive integer \$x\$, returns the sum of:
- \$x\$
- the smallest digit in the decimal representation of \$x\$
- the highest digit in the decimal representation of \$x\$ (which may be the same as the smallest one)
For instance:
- \$f(1)=1+1+1=3\$
- \$f(135)=135+1+5=141\$
- \$f(209)=209+0+9=218\$
We now define the sequence \$a_k = f^k(1)\$. That is to say:
- \$a_1=1\$
- \$a_k=f(a_{k-1})\$ for \$k>1\$
The first few terms are:
1, 3, 9, 27, 36, 45, 54, 63, 72, 81, 90, 99, 117, 125, 131, 135, 141, 146, ...
Challenge
Given a positive integer \$x\$, you must return the smallest \$n\$ such that \$f^n(x)\$ belongs to the sequence. Or in other words: how many times \$f\$ should be applied consecutively to turn \$x\$ into a term of the sequence.
You can assume that you'll never be given an input for which no solution exists. (Although I suspect that this can't happen, I didn't attempt to prove it.)
You may also use 1-based indices (returning \$n+1\$ instead of \$n\$). Please make it clear in your answer if you do so.
Standard code-golf rules apply.
Examples
- Given \$x=45\$, the answer is \$0\$ because \$x\$ is already a term of the sequence (\$a_6=45\$).
- Given \$x=2\$, the answer is \$3\$ because 3 iterations are required: \$f(2)=2+2+2=6\$, \$f(6)=6+6+6=18\$, \$f(18)=18+1+8=27=a_4\$. Or more concisely: \$f^3(2)=27\$.
(These are the 0-based results. The answers would be \$1\$ and \$4\$ respectively if 1-based indices are used.)
Test cases
Input : 0-based output
1 : 0
2 : 3
4 : 15
14 : 16
18 : 1
45 : 0
270 : 0
465 : 67
1497 : 33