O(1) Exponential summation

62 Views Asked by At

Is there an O(1) (uses a function instead of summation/for loop notation) way to calculate $$ \sum\limits_{i=0}^n x^i $$ Given (x,n) Example: (4,3) 64+16+4+1 (3,3) 27+9+3+1 (2,10) 1024+...+8+4+2+1 I know that for x=2, f(x,n)=(x^(n+1))-1 I am in search of a general solution for all x,n.

1

There are 1 best solutions below

0
On BEST ANSWER

This is a geometric progression. The general formula is given by $$f(x,n) = \sum_{i=0}^n x^i = \begin{cases}\frac{x^{n+1}-1}{x-1} & x\neq 1\\ n+1 & x=1\end{cases}$$ Assuming $0^0 = 1$