# [Math] How to find sum of powers from 1 to r

number theorysummation

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.

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}$$