How to solve for an unknown upper bound in a summation I've always wondered: if you have something like 
$$
\sum\limits_{k=0}^x k
$$
And you want to find x such that the value of the summation is the closest to a constant c, how would you proceed?
And is the summation is, in general,
$$
\sum\limits_{k=a}^{a+x} f(k)
$$
Is there anyway to proceed?
 A: There are cases for which it is "simple". Let us consider the case where $f(k)$ is a polynomial of $k$ $$f(k)=a_0+a_1 k+a_2 k^2+a_3 k^3+\cdots$$ So, $$S=\sum\limits_{k=a}^{a+x} f(k)=a_0 \sum\limits_{k=a}^{a+x} 1+a_1 \sum\limits_{k=a}^{a+x} k+a_2 \sum\limits_{k=a}^{a+x} k^2+a_3 \sum\limits_{k=a}^{a+x} k^3+\cdots$$ and the differents sums are successively $$x+1$$ $$\frac{1}{2} (x+1) (2 a+x)$$ $$\frac{1}{6} (x+1) \left(6 a^2+6 a x+2 x^2+x\right)$$ $$\frac{1}{4} (x+1) (2 a+x) \left(2 a^2+2 a x+x^2+x\right)$$ So, you are left with an explicit polynomial equation in $x$ ... to be solved; the easiest way (at least to me) should be to use algebra and text the closest integer value.
Suppose that $$f(k)=\sin(\alpha k+\beta)$$ Trigonometric manipulations would lead to another equation to solve for $x$ $$S=\sum\limits_{k=a}^{a+x} f(k)=\csc \left(\frac{\alpha }{2}\right) \sin \left(\frac{1}{2} \alpha  (x+1)\right) \sin
   \left(a \alpha +\beta +\frac{\alpha  x}{2}\right)$$
Suppose that $$f(k)=\cosh(\alpha k+\beta)$$ Similarly, we should obtain $$S=\sum\limits_{k=a}^{a+x} f(k)=\text{csch}\left(\frac{\alpha }{2}\right) \sinh \left(\frac{1}{2} \alpha 
   (x+1)\right) \cosh \left(a \alpha +\beta +\frac{\alpha  x}{2}\right)$$
Suppose that $$f(k)=e^{\alpha k+\beta}$$ Similarly, we should obtain $$S=\sum\limits_{k=a}^{a+x} f(k)=\frac{\left(e^{\alpha +\alpha  x}-1\right) e^{a \alpha +\beta }}{e^{\alpha }-1}$$
A: for the first sum you can use the formual $\sum_{k=0}^{x}k=\frac{1}{2}x(x+1)$
A: First sum is just $\frac{x(x+1)}{2}$. So you have to solve an equation $x(x+1) = 2C$ and find out $x$ from here and then check $[x]$ or $[x + 1]$ which gives sum closest to $C$.
If $f(k)$ is linear and you can find out $f^{-1}$ then you can generalize this rule.
$$
\sum_{k=a}^{a+x}f(k) = f\Big(\sum_{k=a}^{a+x}k\Big) = f\Big(\frac{(2a +x)(x+1)}{2}\Big)
$$
Then just solve quadratic equation
$$
\frac{(2a +x)(x+1)}{2} = C_0 =  f^{-1}(C)
$$
and check 2 points.
