# Time Complexity of Loop with Powers

What is the time complexity of below function?

`void` `fun(` `int` `n, ` `int` `k) ` `{ ` ` ` `for` `(` `int` `i=1; i<=n; i++) ` ` ` `{ ` ` ` `int` `p = ` `pow` `(i, k); ` ` ` `for` `(` `int` `j=1; j<=p; j++) ` ` ` `{ ` ` ` `// Some O(1) work ` ` ` `} ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Time complexity of above function can be written as 1^{k} + 2^{k} + 3^{k} + … n1^{k}.

Let us try few examples:

k=1 Sum = 1 + 2 + 3 ... n = n(n+1)/2 = n^{2}/2 + n/2 k=2 Sum = 1^{2}+ 2^{2}+ 3^{2}+ ... n1^{2}. = n(n+1)(2n+1)/6 = n^{3}/3 + n^{2}/2 + n/6 k=3 Sum = 1^{3}+ 2^{3}+ 3^{3}+ ... n1^{3}. = n^{2}(n+1)^{2}/4 = n^{4}/4 + n^{3}/2 + n^{2}/4

In general, asymptotic value can be written as **(n ^{k+1})/(k+1) + Θ(n^{k})**

Note that, in asymptotic notations like Θ we can always ignore lower order terms. So the time complexity is **Θ(n ^{k+1} / (k+1))**

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time taken by Loop unrolling vs Normal loop
- A Time Complexity Question
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- An interesting time complexity question
- Time Complexity of building a heap
- Understanding Time Complexity with Simple Examples
- Time complexity of recursive Fibonacci program
- Practice Questions on Time Complexity Analysis
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Python Code for time Complexity plot of Heap Sort
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- What does 'Space Complexity' mean?
- Cyclomatic Complexity
- Knowing the complexity in competitive programming
- Complexity Analysis of Binary Search
- Complexity analysis of various operations of Binary Min Heap
- Interesting Infinite loop using characters in C
- Count of elements which form a loop in an Array according to given constraints