Can somebody explain this? Why does this happen?
Yesterday I was on a popular chat bot and I asked it to make me a code to generate a sequence of numbers. What I wanted, was a script that given a number "n" would go on and subtract the successive square roots of the successive number on intervals of $10$. So if $n=10$ the first element is $-\sqrt{11}-\sqrt{12}-...-\sqrt{21}$, the second is $-\sqrt{21}-\sqrt{22}-...-\sqrt{31}-\sqrt{32}$. the third would start at $-\sqrt{31}$ and end at $-√43$, and so on, iterating $n+1$ times. I also wanted the script to calculate the difference between the first and the second element, the third and the fourth and continuing until the last element. In the result, it gives me a simple numerical sequence, and I notice that from the first one, it starts diminishing up until a certain point, and then it starts increasing. I went to look at this value (let's call it q) and I wrote it, for several n inputs. What I find weird is that if you calculate q/n it gives you a near constant result: $1,732$ (for powers of 10, it gives a better result, but more or less any n works).
That's an approximation of $\sqrt{3}$ and if you input n=100.000, look for q, divide it by n, you'll get $1,732050808$.
Here is a desmos graph plotting the series: https://www.desmos.com/calculator/mcmzlaaimi?lang=en
Here is the full code for anybody that wants to try it.
" import math
def subtract_square_roots(start, end):
result = 0
for i in range(start, end+1):
result -= math.sqrt(i)
return result
# Get n value from user
n = int(input("Enter the value of n: "))
previous_result = None
for i in range(1, n+1):
start = i*n + 1
end = (i+1)*n + i
result = subtract_square_roots(start, end)
if previous_result is not None:
diff = result - previous_result
print(f"{diff:.4f},")
previous_result = result "
Here's also a code that points out the q value for any given n, making it kind of easier to spot.
"import math
def subtract_square_roots(start, end):
result = 0
for i in range(start, end+1):
result -= math.sqrt(i)
return result
# Get n value from user
n = int(input("Enter the value of n: "))
previous_result = None
previous_diff = None
for i in range(1, n+1):
start = i*n + 1
end = (i+1)*n + i
result = subtract_square_roots(start, end)
if previous_result is not None:
diff = result - previous_result
if previous_diff is not None and diff < previous_diff:
# The sequence has gone from increasing to decreasing
print(f"The value at which the sequence goes from increasing to decreasing is {previous_diff}")
break
previous_diff = diff
previous_result = result ".

The question takes sums of negative square roots but the Desmos worksheet takes sums of positive square roots. This is OK, because we can easily change a sum of positive square roots to a sum of negative square roots (or vice versa) just by negating the sum. I'll work with positive square roots for simplicity.
For a given $n$ we have a sequence of sums, which I'll denote by $\sigma(n,m)$. The first sum is $$ \sigma(n,1) = \sqrt{n+1} + \sqrt{n+2} + \sqrt{n+3} + \cdots + \sqrt{2n+1}, $$ the next is $$ \sigma(n,2) = \sqrt{2n+1} + \sqrt{2n+2} + \sqrt{2n+3} + \cdots + \sqrt{3n+2}, $$ and so forth; the $m$th sum is $$ \sigma(n,m) = \sqrt{mn+1} + \sqrt{mn+2} + \sqrt{mn+3} + \cdots + \sqrt{(m+1)n + m}. $$
You then examine the difference between each sum and the previous sum in the sequence, that is, $\sigma(n,m) - \sigma(n,m - 1).$
Note that there are some terms shared by $\sigma(n,m - 1)$, which ends with $$ \cdots + \sqrt{mn + (m - 3)} + \sqrt{mn + (m - 2)} + \sqrt{mn + (m - 1)}, $$ and $\sigma(n,m)$, which starts with $$ \sqrt{mn + 1} + \sqrt{mn + 2} + \sqrt{mn + 3} + \cdots. $$ So we can cancel the terms $\sqrt{mn + 1}$ through $\sqrt{mn + m - 1}$ when taking the difference of sums $\sigma(n,m) - \sigma(n,m - 1).$ Therefore
$$ \sigma(n,m) - \sigma(n,m - 1) = h(n,m) - g(n,m) $$
where \begin{align} h(n,m) &= \sqrt{mn+m} + \sqrt{mn+m+1} + \sqrt{mn+m+2} + \cdots + \sqrt{(m+1)n + m} \\ &= \sqrt{mn+m} + \sqrt{mn+m+1} + \cdots + \sqrt{mn + m + n} \\ &= \sum_{k=1}^{n+1} \sqrt{mn + m + k - 1} \end{align} and \begin{align} g(n,m) &= \sqrt{(m-1)n+1} + \sqrt{(m-1)n+2} + \sqrt{(m-1)n+3} + \cdots + \sqrt{mn} \\ &= \sqrt{mn-n+1} + \sqrt{mn-n+2} + \cdots + \sqrt{mn - n + n} \\ &= \sum_{k=1}^{n} \sqrt{mn - n + k}. \end{align}
Your value $q$ is the minimum of $h(n,m) - g(n,m)$ where $m$ is an integer such that $m \geq 2.$ (In Desmos you have $2 \leq m \leq n,$ but let's see whether that upper bound on $m$ makes any difference.)
Noting that $\sqrt{x+c}$ is an increasing function, by Riemann sums with a uniform mesh with $\Delta x = 1,$ we have \begin{align} \int_0^{n+1} \sqrt{mn + m + x - 1}\,\mathrm dx &\leq h(n,m) \leq \int_1^{n+2} \sqrt{mn + m + x - 1}\,\mathrm dx \\ \int_0^{n} \sqrt{mn - n + x}\,\mathrm dx &\leq g(n,m) \leq \int_1^{n+1} \sqrt{mn - n + x}\,\mathrm dx \end{align}
Therefore $$ f_-(n,m) \leq h(n,m) - g(n,m) \leq f_+(n,m) $$ where $$ f_-(n,m) = \int_0^{n+1} \sqrt{mn + m + x - 1}\,\mathrm dx - \int_1^{n+1} \sqrt{mn - n + x}\,\mathrm dx $$ and $$ f_+(n,m) = \int_1^{n+2} \sqrt{mn + m + x - 1}\,\mathrm dx - \int_0^{n} \sqrt{mn - n + x}\,\mathrm dx. $$
Note that the minimum of $h(n,m) - g(n,m)$ cannot be less than the minimum of $f_-(n,m)$ nor greater than the minimum of $f_+(n,m).$
Let's look at $f_+(n,m)$ first. Solving the integrals, \begin{align} f_+(n,m) &= \left(\frac23(mn + m + n + 1)^{3/2} - \frac23(mn + m)^{3/2}\right) \\ &\qquad - \left(\frac23(mn)^{3/2} - \frac23(mn - n)^{3/2}\right) \\ &= \frac23\left(((m+1)(n+1))^{3/2} - (m(n+1))^{3/2} - (mn)^{3/2} + ((m - 1)n)^{3/2}\right). \tag1 \end{align}
Taking the derivative with respect to $m,$ \begin{align} \frac{\partial}{\partial m} f_+(n,m) &= \frac23 \begin{aligned}[t] \left((n+1)\sqrt{(m+1)(n+1)} - (n+1)\sqrt{m(n+1)}\right.\ \\ - \left. n\sqrt{mn} + n\sqrt{(m - 1)n}\right) \end{aligned} \\ &= \frac23\left((n+1)^{3/2}\left(\sqrt{m+1} - \sqrt{m}\right) - n^{3/2}\left(\sqrt{m} - \sqrt{m - 1}\right)\right) \\ \end{align}
For positive $m$ and $n$ we have Taylor series in $1/m$ and $1/n,$ \begin{align} \sqrt{m+1} &= \sqrt{m}\sqrt{1+\frac1m} = \sqrt{m}\left(1 + \frac{1}{2m} - \frac{1}{8m^2} + O\left(\frac1{m^3}\right)\right), \\ \sqrt{m-1} &= \sqrt{m}\sqrt{1-\frac1m} = \sqrt{m}\left(1 - \frac{1}{2m} - \frac{1}{8m^2} + O\left(\frac1{m^3}\right)\right), \\ (n+1)^{3/2} &= n^{3/2}\left(1+\frac1n\right)^{3/2} = n^{3/2}\left(1 + \frac{3}{2n} + O\left(\frac1{n^2}\right)\right). \end{align}
Therefore \begin{align} \sqrt{m+1} - \sqrt{m} &= \frac{1}{2\sqrt m} - \frac{1}{8m^{3/2}} + O\left(\frac1{m^{5/2}}\right), \\ \sqrt{m} - \sqrt{m-1} &= \frac{1}{2\sqrt m} + \frac{1}{8m^{3/2}} + O\left(\frac1{m^{5/2}}\right), \end{align}
and collecting the higher-order terms together, \begin{align} (n+1)^{3/2}\left(\sqrt{m+1} - \sqrt{m}\right) &= \begin{aligned}[t] \left(n^{3/2} + \frac32\sqrt{n}\right) \left(\frac{1}{2\sqrt{m}} - \frac{1}{8m^{3/2}}\right) + O\left(\frac{1}{\sqrt{mn}} + \frac{n^{3/2}}{m^{5/2}}\right),\end{aligned} \\ n^{3/2}\left(\sqrt{m} - \sqrt{m-1}\right) &= n^{3/2} \left(\frac{1}{2\sqrt{m}} + \frac{1}{8m^{3/2}}\right) + O\left(\frac{1}{\sqrt{mn}} + \frac{n^{3/2}}{m^{5/2}}\right), \\ \end{align} and \begin{align} (n+1)^{3/2}\left(\sqrt{m+1} - \sqrt{m}\right) & - n^{3/2}\left(\sqrt{m} - \sqrt{m-1}\right)\\ &= -\frac{n^{3/2}}{4m^{3/2}} + \frac{3\sqrt{n}}{4\sqrt{m}} + O\left(\frac1{\sqrt{mn}} + \frac{\sqrt n}{m^{3/2}} + \frac{n^{3/2}}{m^{5/2}}\right)\\ &= \frac{\sqrt{n}}{4m^{3/2}} \left(3m - n\right) + O\left(\frac1{\sqrt{mn}} + \frac{\sqrt n}{m^{3/2}} + \frac{n^{3/2}}{m^{5/2}}\right) \end{align}
The partial derivative $\frac{\partial}{\partial m} f_+(n,m)$ is zero when both sides of this equation are zero. Observe that if we set $m = \frac13 n,$ the right-hand side comes out to $O\left(\frac1n\right),$ that is, for large enough $n$ this expression comes out near zero while it is negative for $m \ll \frac13 n$ and positive for $m \gg \frac13 n.$ This suggests that $\frac{\partial}{\partial m} f_+(n,m)$ is zero when $m \approx \frac13 n$ and that $f_+(n,m)$ has a minimum at about that value.
Next we might look at $f_-(n,m).$ The fact that \begin{multline} f_-(n,m) = f_+(n,m) - \int_{n+1}^{n+2} \sqrt{mn + m + x - 1}\,\mathrm dx - \int_n^{n+1} \sqrt{mn - n + x}\,\mathrm dx \\ + \int_0^1 \sqrt{mn + m + x - 1}\,\mathrm dx + \int_0^1 \sqrt{mn - n + x}\,\mathrm dx \end{multline} suggests that the difference is $O(\sqrt{mn}).$ Numerical calculations suggest that the difference is about $2$ when either $f_-(n,m)$ or $f_+(n,m)$ is near its minimum. If so, the minimums of $$ \frac{f_-(n,m)}{n} \quad\text{and}\quad \frac{f_+(n,m)}{n} $$ converge for large $n.$ That is, for large $n$ we should expect the minimum of $\dfrac{\sigma(n,m)}{n}$ to approach the minimum of $\dfrac{f_+(n,m)}{n}.$
From Equation $(1),$ \begin{align} \frac{f_+(n,m)}{n} &= \frac2{3n}\left(((m+1)^{3/2} - m^{3/2})(n+1)^{3/2} - (m^{3/2} - (m - 1)^{3/2})n^{3/2} \right). \end{align}
Again we take Taylor series in $1/m,$ \begin{align} (m+1)^{3/2} &= m^{3/2}\left(1 + \frac{3}{2m} + \frac{3}{8m^2} + O\left(\frac1{m^3}\right)\right), \\ (m-1)^{3/2} &= m^{3/2}\left(1 - \frac{3}{2m} + \frac{3}{8m^2} + O\left(\frac1{m^3}\right)\right), \\ \end{align}
so \begin{align} (m+1)^{3/2} - m^{3/2} &= \frac32\sqrt{m} + \frac{3}{8\sqrt m} + O\left(\frac1{m^{3/2}}\right), \\ m^{3/2} - (m-1)^{3/2} &= \frac32\sqrt{m} - \frac{3}{8\sqrt m} + O\left(\frac1{m^{3/2}}\right), \end{align} and setting $m = \frac13 n,$ \begin{align} ((m+1)^{3/2} - m^{3/2})(n+1)^{3/2} &= \left(\frac{\sqrt{3n}}2 + \frac{3\sqrt3}{8\sqrt n}\right) \left(n^{3/2} + \frac{3\sqrt n}{2} \right) + O(1) \\ &= \frac{\sqrt3 n^2}2 + \frac{9\sqrt3n}{8} + O(1), \\ (m^{3/2} - (m - 1)^{3/2})n^{3/2} &= \left(\frac{\sqrt{3n}}2 - \frac{3\sqrt3}{8\sqrt n}\right) n^{3/2} + O(1) \\ &= \frac{\sqrt3 n^2}2 - \frac{3\sqrt3n}{8} + O(1), \end{align}
and \begin{align} \frac{f_+(n,m)}{n} &= \frac2{3n}\left(\left(\frac{\sqrt3 n^2}2 + \frac{9\sqrt3n}{8}\right) - \left(\frac{\sqrt3 n^2}2 - \frac{3\sqrt3n}{8} \right) + O(1)\right)\\ &= \frac2{3n}\left(\frac{3\sqrt3n}{2} + O(1)\right)\\ &= \sqrt3 + O\left(\frac1n\right). \end{align}
That's why the value $\dfrac qn$ that you are calculating comes out to approximately $\sqrt3$ (or $-\sqrt3$ when you take sums of negative square roots).