Maximizing summation function with upper bound as variable. I'm trying to find the general way of optimizing a summation function and I'm a bit lost. I would like to find the maxium (and the minimum, ideally) of:
$$ \sum_{n=1}^{x} f(n)$$
For example, let's say:
$$ f(n) = -(n-4)^2 + 16 $$
In this scenario, the answer is easy to find without doing any work: f(n) is positive for $n \in [0, 8]$, and then becomes negative for infinity, we can maximize the sum by adding all positive numbers ($x = 8$).
Is there a way to find this mathematically, in a general way for all functions? Usually when looking to optimize a function I would take the derivative, but the derivative of a summation doesn't make much sense, does it?
Thanks.

For a local max at positive integer $x$, you want $f(x) \ge 0$ while $f(x+1) \le 0$.  Similarly for a local min with $\le$ and $\ge$.  So you look for the places where $f$ changes sign.
