0

If we have this summation

$$ \sum_{k=1}^n n^k $$

The time complexity of this function is O($n^{n+1}$). Why would that be? If I'm not mistaken this is a function which is the addition of n to all powers between 1 and n, meaning the term with the largest power, which dictates our time complexity, is $n^n$.

M Zein
  • 25

2 Answers2

1

$$\sum_{k=1}^n n^k = \frac{n^{n+1}-1}{n-1} = O(n^n)\ \text{as}\ n \to \infty$$ But that doesn't prevent it from being $O(n^{n+1})$.
Check the definition.

Robert Israel
  • 448,999
0

Remember that the notation $\mathcal{O}$ is additive. Wich means that :

$$\mathcal{O}(m+n) = \mathcal{O}(m) + \mathcal{O}(n)$$

Moreover note that we have :

$$\forall 0 \leq k \leq n, n^k = \mathcal{O}(n^n)$$

Hence we have :

$$\sum_{k = 1}^n n^k = \sum_{k = 1}^n \mathcal{O}(n^n) = \mathcal{O}\left(\sum_{k = 1}^n n^n\right) = \mathcal{O}\left(n^{n+1} \right)$$

Thinking
  • 1,310