Sets and many roots and sums maths question. This is not a homework question. It was given by a friend, even contains the current year for fun. I understand the idea but I can't comprehend the methods on solving this with 2017 digits. I am not proficient enough in this field. It's supposedly tricky. Can anyone try and do it? I'll try to be clear:
Let $A$ be the set of all the 2017-digit numbers that satisfy the following property:
If $a$ is in $A$ and $a_1,...,a_{2017}$ are the digits of $a$ (in order), then there exists a positive integer $S$ such that:
$$S=\sqrt{a_1+\sqrt{a_2+\sqrt{a_3+...+\sqrt{a_{2016}+\sqrt{a_{2017}+S}}}}}$$
Let $m$ and $n$ be, respectively, the least and the greatest elements of $A$.
If $s_m=\sum_{i=1}^{2017} m_i$ and $s_n=\sum_{i=1}^{2017} n_i$, compute $s_m+s_n$.
Thanks very much.
Edit: Qudit has given a clear, comprehensive and the most correct solution. It has been answered.
 A: Let $a_n$ be a sequence of non-negative integers between $0$ and $9$ such that $a_{2017}\neq 0$. Let $S$ be a positive integer, and define $b_n$ by $b_0=S$ and $b_{n+1}=\sqrt{a_{n+1}+b_n}$


Claim: If $S\ge4$ then $b_n<S$ for all $n\ge 1$ 
Proof: By induction. $b_1=\sqrt{a_1+S}\le\sqrt{9+S}<\sqrt{S^2}=S$ since $S^2-S-9>0$ for $S\ge 4$. So $b_n=\sqrt{a_n+b_{n-1}}<\sqrt{a_n+S}<S$ by induction.


So for $b_0=b_{2017}$ we require $1\le S\le 3$.

If $S=1$. Then the possible values for $b_1$ are $1,2,3$ with corresponding $a_1=0,3,8$.
If $S=2$ then $b_1=2,3$ with $a_1=2,7$
If $S=3$ then $b_1=2,3$ with $a_1=1,6$.
Now here is a crucial observation:


Observation: Suppose that $b_{2017}=b_0$. Then $S\neq 1$.
Proof: From the earlier observation we see that for $b_{2017}=b_0$ we must have $1\le S\le 3$ and also that $1\le b_n\le 3$ for all $n\ge 0$. However, given $b_n$ the only way to have $b_{n+1}=1$ is by having $a_{n+1}=0$. So if $S=1$ then $b_{2017}=1$ would imply $a_{2017}=0$, a contradiction.


Once we know this, then only possible cases are $S=2$ and $S=3$, and in this case $2\le b_n \le 3$ for all $n\ge 0$ and we have a complete freedom as far as we are within this bound.

To find the smallest element in $A$, we'd need $b_{2017}=S=2$ and $a_{2017}=1$, with $b_{2016}=3$. Then $a_{2016}=6$ with $b_{2015}=3$ and so on, until we have $b_0=2$ with $a_1=7$ so $b_1=3$. So the number we are looking at is $166666\dots6667$ with $2015$ six between $1$ and $7$
For the largest one $a_{2017}=7$ with $b_{2016}=2$ and $b_{2017}=S=3$. For $b_{2016}=2$, our best choice is $b_{2015}=2$ with $a_{2016}=2$, repeating this until $b_1=2$, $b_0=3$, we need $a_1=1$. So the number we are looking for is $7222222\dots22221$.
A: Let $a \in A$.  Then there exists a positive integer $S$ such that $S = \sqrt{b_1}$ where $b_{2017} = a_{2017} + S$ and $$b_k = a_k + \sqrt{b_{k + 1}}$$ for $1 \leq k < 2017$.
For any irrational number $\alpha$ and integer $k$, it is easy to show that $\sqrt{k + \alpha}$ is also irrational.  This implies that each $b_k$ is a perfect square.  
Now, consider a positive integer $x$.  Observe that $\sqrt{9 + x} < x$ for $x \geq 4$.  Therefore, $S \leq 3$.  Then $1 \leq b_n \leq 12$ so $b_k \in \{1, 4, 9\}$ and $\sqrt{b_k} \in \{1, 2, 3\}$ for $k = 2017$.  By induction, this holds for all $k$.
Now, we cannot have $a_1 = 9$ because then $S$ would not be an integer.  If $a_1 = 8$, then $b_1 = 9$ so $\sqrt{b_2} = 1$.  Since each $b_k \geq 1$, it follows that $a_k = 0$  for $k \geq 2$ and $\sqrt{b_k} = 1$ for $k \geq 3$.  Then $b_{2017} = 1$ and $a_{2017} = 0$ so $S = 1$.  However, $S = \sqrt{b_1} = \sqrt{9} = 3$, so $a_1 \not= 8$.
If $a_1 = 7$, then $S = 3$ and $b_2 = 4$ so $a_2 \in \{1, 2, 3\}$.  We cannot have $a_2 = 3$ because then $\sqrt{b_3} = 1$ and by the argument used in the previous paragraph, $\sqrt{b_k} = 1$ for $4 \leq k \leq 2017$.  However, this contradicts the fact that $b_{2017} = a_{2017} + S \geq 3$.  Therefore, $a_2 \not= 3$.  If $a_2 = 2$ then $b_3 = 4$.  Then $a_3 \in \{1, 2, 3\}$ and by the same argument as before, $a_3 \not= 3$.  Continuing in this manner, we see that $a_k \in \{1, 2\}$ for $2 \leq k \leq 2016$.  Since $S = 3$, we see that the largest candidate for an element of $A$ is $$n = 7\overbrace{2\cdots2}^{\text{$2015$ times}} 1$$ and it is straightforward to verify that $n$ satisfies the conditions of $A$.
Now, let's find the smallest element of $A$.  A $2017$-digit number cannot start with $0$ so the smallest possible value for $a_1$ is $1$.  Then $\sqrt{b_2} = 3$ so $S = 2$ and $b_2 = 9$.  Since $\sqrt{b_3} \in \{1, 2, 3\}$, the smallest possible value for $a_2$ is $6$ in which case $\sqrt{b_3} = 3$.  Continuing in this manner, we see that the smallest candidate for an element of $A$ is $$m = 1\overbrace{6\cdots6}^{\text{$2015$ times}} 7$$ and it is easy to verify that $m$ satisfies the conditions of $A$.
Let $m_k$ and $n_k$ denote the $k^{\text{th}}$ digits of $m$ and $n$ respectively.  Then
\begin{align}
s_m + s_n &= \sum_{k = 1}^{2017} m_k + \sum_{k = 1}^{2017} n_k \\
{} &= 12098 + 4038 \\
{} &= 16136
\end{align}
