Logarithmic Time Complexity O(Log Log n)
The Time Complexity of a loop is considered as O(LogLogn) if the loop variables are reduced/increased exponentially by a constant amount.
C++
// Here c is a constant greater than 1 for ( int i = 2; i <= n; i = pow (i, c)) { // some O(1) expressions } // Here fun() is sqrt or cuberoot or any other constant root for ( int i = n; i > 1; i = fun(i)) { // some O(1) expressions } //This code is contributed by Kshitij |
C
// Here c is a constant greater than 1 for ( int i = 2; i <= n; i = pow (i, c)) { // some O(1) expressions } // Here fun is sqrt or cuberoot or any other constant root for ( int i = n; i > 1; i = fun(i)) { // some O(1) expressions } |
Java
// Here c is a constant greater than 1 for ( int i = 2 ; i <= n; i = Math.pow(i, c)) { // some O(1) expressions } // Here fun is sqrt or cuberoot or any other constant root for ( int i = n; i > 1 ; i = fun(i)) { // some O(1) expressions } // This code is contributed by Utkarsh |
C#
using System; public class Main { public static void Execute( string [] args) { int n = 100; // Example value of n int c = 2; // Example value of c // Here c is a constant greater than 1 for ( int i = 2; i <= n; i = ( int )Math.Pow(i, c)) { // some O(1) expressions Console.WriteLine(i); // For demonstration } // Here fun() is sqrt or cuberoot or any other constant root for ( int i = n; i > 1; i = fun(i)) { // some O(1) expressions Console.WriteLine(i); // For demonstration } } // Function to find constant root (e.g., sqrt, cuberoot) public static int fun( int num) { // Here, let's consider finding the square root return ( int )Math.Sqrt(num); } } |
Javascript
// Here c is a constant greater than 1 for ( var i = 2; i <= n; i = i**c) { // some O(1) expressions } // Here fun is sqrt or cuberoot or any other constant root for ( var i = n; i > 1; i = fun(i)) { // some O(1) expressions } |
Python3
# Here c is a constant greater than 1 i = 2 while (i < = n): # some O(1) expressions i = i * * c # Here fun is sqrt or cuberoot or any other constant root i = n while (i > 1 ): # some O(1) expressions i = fun(i) # This code is contributed by Pushpesh Raj |
See this for mathematical details.
How to Analyse Loops for Complexity Analysis of Algorithms
We have discussed Asymptotic Analysis, Worst, Average and Best Cases and Asymptotic Notations in previous posts. In this post, an analysis of iterative programs with simple examples is discussed.
The analysis of loops for the complexity analysis of algorithms involves finding the number of operations performed by a loop as a function of the input size. This is usually done by determining the number of iterations of the loop and the number of operations performed in each iteration.
Here are the general steps to analyze loops for complexity analysis:
Determine the number of iterations of the loop. This is usually done by analyzing the loop control variables and the loop termination condition.
Determine the number of operations performed in each iteration of the loop. This can include both arithmetic operations and data access operations, such as array accesses or memory accesses.
Express the total number of operations performed by the loop as a function of the input size. This may involve using mathematical expressions or finding a closed-form expression for the number of operations performed by the loop.
Determine the order of growth of the expression for the number of operations performed by the loop. This can be done by using techniques such as big O notation or by finding the dominant term and ignoring lower-order terms.
Contact Us