Time Complexity of Loop with Powers Last Updated : 23 Aug, 2024 Comments Improve Suggest changes Like Article Like Report What is the time complexity of the below function? C++ 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 } } } // This code is contributed by Shubham Singh C 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 } } } Java static void fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = Math.pow(i, k); for (int j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by umadevi9616 Python def fun(n, k): for i in range(1, n + 1): p = pow(i, k) for j in range(1, p + 1): # Some O(1) work # This code is contributed by Shubham Singh C# static void fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = Math.Pow(i, k); for (int j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by umadevi9616 JavaScript <script> // JavaScript program for the above approach function fun(n, k) { for(let i = 1; i <= n; i++) { int p = Math.pow(i, k); for (let j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by Shubham Singh </script> Time complexity of above function can be written as 1k + 2k + 3k + ... n1k.Let us try few examples: k=1Sum = 1 + 2 + 3 ... n = n(n+1)/2 = n2/2 + n/2k=2Sum = 12 + 22 + 32 + ... n12. = n(n+1)(2n+1)/6 = n3/3 + n2/2 + n/6k=3Sum = 13 + 23 + 33 + ... n13. = n2(n+1)2/4 = n4/4 + n3/2 + n2/4 In general, asymptotic value can be written as (nk+1)/(k+1) + Θ(nk)If n>=k then the time complexity will be considered in O((nk+1)/(k+1)) and if n<k, then the time complexity will be considered as in the O(nk) Comment More infoAdvertise with us Next Article Time Complexity of Loop with Powers kartik Follow Improve Article Tags : Analysis of Algorithms DSA time complexity Similar Reads Miscellaneous Problems of Time Complexity Prerequisite : Asymptotic Notations Time Complexity :Time complexity is the time needed by an algorithm expressed as a function of the size of a problem. It can also be defined as the amount of computer time it needs to run a program to completion. When we solve a problem of time complexity then thi 15 min read Multiplication with a power of 2 Given two numbers x and n, we need to multiply x with 2nExamples : Input : x = 25, n = 3 Output : 200 25 multiplied by 2 raised to power 3 is 200. Input : x = 70, n = 2 Output : 280 A simple solution is to compute n-th power of 2 and then multiply with x. C++ // Simple C/C++ program // to compute x 5 min read Compute power of power k times % m Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k time 15+ min read A Time Complexity Question What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2. C++ void fun() { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= log(i); j++) cout << "GeeksforGeeks"; } // This code is contributed by SHUBHAMSINGH10. C void fun() { in 2 min read Program for power of a complex number in O(log n) Given a complex number of the form x + yi and an integer n, the task is to calculate the value of this complex number raised to the power n.Examples: Input: num = 17 - 12i, n = 3Output: -2431 + i ( -8676 )Input: num = 18 - 13i, n = 8Output: 16976403601 + i ( 56580909840 ) Approach: This algorithm wo 8 min read Time Complexity of a Loop when Loop variable âExpands or Shrinksâ exponentially For such cases, time complexity of the loop is O(log(log(n))).The following cases analyse different aspects of the problem. Case 1 : CPP for (int i = 2; i <=n; i = pow(i, k)) { // some O(1) expressions or statements } In this case, i takes values 2, 2k, (2k)k = 2k2, (2k2)k = 2k3, ..., 2klogk(log( 1 min read Step Count Method for Time Complexity Analysis What is Time Complexity? Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm. Time Complexity can be calculated by using Two types of methods. They are: Step Count MethodAsymptotic Notation. Here, we will d 4 min read Form a number using corner digits of powers Given two integers N and X. Make a number in such a way that the number contains the first and last digit occurring in N^1, N^2, N^3, .... N^X . Examples : Input : N = 10, X = 5 Output : 1010101010 Explanation : 10^1 = 10 10^2 = 100 10^3 = 1000 10^4 = 10000 10^5 = 100000 Take First and Last Digit of 8 min read Smallest power of 2 consisting of N digits Given an integer N, the task is to find the smallest power of 2 which consists of N digits. Examples: Input: N = 3Output: 7Explanation:27 = 128, which has three digits. Input: N = 4Output: 10Explanation:210 = 1024, which has four digits. Naive Approach: A simple solution is to iterate through all th 5 min read Find unit digit of x raised to power y Given two numbers x and y, find unit digit of xy. Examples : Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6. Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow f 11 min read Like