What mathematical method should I use when I want to sum the result of an equation on a range of numbers?

55 Views Asked by At

I have a very simple equation which I need to do on a range of numbers as following:

(x/min)+(x/(min+1))+(x/(min+2))+...++(x/(max))

I have a variable called x which needs to be divided to a range of numbers from min to max and get the result of the addition of all the equations.

Currently I'm using a loop and it's working fine, but I am wondering how a similar problem is being solved with a single equation?

2

There are 2 best solutions below

7
On BEST ANSWER

Provided that $min$ and $max$ are natural numbers, the formula you seek is $$x(H_{max}-H_{min-1})$$ where $H_n$ denotes the $n$-th Harmonic number, defined as $H_n=\frac{1}{1}+\frac{1}{2}+\cdots+\frac{1}{n}$.

If an approximation is good enough, $H_n\approx \ln n$, so a decent approximation to your desired sum is $$x(\ln max - ln(min -1))= x \ln \frac{max}{min -1}$$

2
On

Putting your formula into a more mathematical format, you say you want to compute $$ \frac{x}{min} + \frac{x}{min+1} + \frac{x}{min+2} + \cdots + \frac{x}{max} $$ which makes sense if $min$ and $max$ are both integers. (Actually it makes sense as long as the difference $max - min$ is an integer.)

But \begin{multline} \frac{x}{min} + \frac{x}{min+1} + \frac{x}{min+2} + \cdots + \frac{x}{max} \\ = x \left(\frac{1}{min} + \frac{1}{min+1} + \frac{1}{min+2} + \cdots + \frac{1}{max} \right). \end{multline}

So if you find that you have to compute the sum for many different values of $x,$ but the same values of $min$ and $max$ each time, compute the part in the parentheses first, store it, and use it as a multiplier for each value of $x.$