How to find sum of powers from 1 to r

2.2k Views Asked by At

Let say I have two numbers n power r. How can we find sums of all powers. For example if n = 3 and r 3 then we can calculate manually like this

3 ^ 3 = 27
3 ^ 2 = 9
3 ^ 1 = 3

Sum   = 39

Can we formulate this? I mean can we create a function which takes n and r and returns this sum?

I have background in programming but don't know maths :-) . I know using any programming language we can write a function which can calculate sum using loops or recursion but is there any other solution so I find sum without loops or recursion.

Thanks in advance

2

There are 2 best solutions below

2
On BEST ANSWER

As I said in the comment, it is called geometric series:

$$a^0+a^1+a^2+\ldots + a^n = \sum_{k=0}^n a^k = \frac{a^{n+1}-1}{a-1}$$

So in your case we do not begin witht the exponent 0 but with 1 so we just substract $a^0=1$:

$$a^1 + a^2+a^3 + \ldots + a^n = \frac{a^{n+1}-1}{a-1} - 1$$

In your concrete case $a=3$ and $n=3$:

$$3^1+3^2+3^3 = \frac{3^{4}-1}{3-1} -1 = 39$$

You can derive it as follows:

Let $$S = a^0 + a^1 + \ldots a^n.$$ Therefore

$$ a\cdot S = a^1 + a^2 \ldots + a^{n+1}.$$

So $$(a-1)S = aS-S = a^{n+1}-a^0 = a^{n+1} -1$$ results when dividing by $(a-1)$ in:

$$S = \frac{a^{n+1}-1}{a-1}$$

0
On

Let $r \neq 1$ be whatever number you're going to sum consecutive powers of (in your example $r = 3$). Let $m$ and $n$ be counting numbers (positive integers) with $m \leq n$. Then $$ \sum\limits_{k=m}^n r^k = r^m + r^{m+1} + \cdots + r^n = \frac{r^{m}-r^{n+1}}{1-r}. $$ To see why this is true, consider $(1-r)\sum\limits_{k=m}^nr^k$. We have $$ (1-r)\sum\limits_{k=m}^n r^k = \sum\limits_{k=m}^n r^k - r\sum\limits_{k=m}^n r^k = (r^m + r^{m+1}+ \cdots + r^n) - r(r^m + r^{m+1}+ \cdots + r^n)= (r^m + r^{m+1}+ \cdots + r^n) - (r^{m+1} + r^{m+2}+ \cdots + r^{n+1}) = r^m - r^{n+1} $$ where the last equality follows since all other terms cancel. But then $$ (1-r) \sum\limits_{k=m}^n r^k = r^{m} - r^{n+1} $$ so dividing both sides by $1-r$ will give you the formula $\sum\limits_{k=m}^{n} r^k = \frac{r^m - r^{n+1}}{1-r}$.