## What is a **logarithm**?

**logarithm**

The power to which a

needs to be raised to reach a givenbaseis called the logarithm of thatnumberfor the respectivenumberbase.

For finding logarithmic two necessary factors that need to be known areandbasenumber.

Algorithms are extremely important in computer programming because a whole computer model runs when several algorithms work together. Choosing an efficient algorithm can be a tough choice to make for this complex analysis of the algorithm. There is various order of time complexities for the determination of algorithm out of which some are most efficient and some are worst. So, we have to take care of this complexity for the better performance of any program. In this blog, we will look in-depth into the** Logarithmic Complexity. **We will also do various comparisons between different logarithmic complexities, when and where such logarithmic complexities are used, several examples of logarithmic complexities, and much more. So let’s get started.

**What is meant by Complexity Analysis?**

**What is meant by Complexity Analysis?**

The primary motive to use DSA is to solve a problem effectively and efficiently. How can you decide if a program written by you is efficient or not? This is measured by complexities. Complexity is of two types:

**What is Space Complexity?**

**What is Space Complexity?**

The space Complexity of an algorithm is the space taken by an algorithm to run the program for a given input size. The program has some space requirements necessary for its execution these include auxiliary space and input space. The important standard for comparison of algorithms is the space taken by the algorithm to run for a given input size Hence it needs to be optimized.

**What is Time Complexity?**

**What is Time Complexity?**

In Computer science, there are various problems and several ways to solve each of these problems using different algorithms. These algorithms may have varied approaches, some might be too complex to Implement while some may solve the problem in a lot simpler way than others. It is hard to select a suitable and efficient algorithm out of all that are available. To make the selection of the best algorithm easy, calculation of complexity and time consumption of an algorithm is important this is why ** time complexity analysis **is important, for this

**asymptotic analysis****of the algorithm is done.**

There are three cases denoted by three different notations of analysis:

**Big-oh(O) Notation**Denotes the upper bound of any algorithm’s runtime i.e. time is taken by the algorithm in the worst case.**:****Big-omega(Ω) Notation**Denotes the best runtime of an algorithm.**:**Denotes average case time complexity.**Big-Theta(Θ) notation:**

**How to measure complexities? **

**How to measure complexities?**

Below are some major order of Complexities are:

If the algorithm runs for the same amount of time every time irrespective of the input size. It is said to exhibit constant time complexity.**Constant:**If the algorithm runtime is linearly proportional to the input size then the algorithm is said to exhibit linear time complexity.**Linear:**If the algorithm runtime depends on the input value raised to an exponent then it is said to exhibit exponential time complexity.**Exponential:**When the algorithm runtime increases very slowly compared to an increase in input size i.e. logarithm of input size then the algorithm is said to exhibit logarithmic time complexity.**Logarithmic:**

O(1) | Constant |
---|---|

O(log N) | Logarithmic |

O(N) | Linear time |

O(N * log N) | Log linear |

O(N^2) | Quadratic |

O(N^3) | Cubic |

O(2^N) | Exponential |

O(N!) | Factorial |

## What is a **logarithm**?

**logarithm**

The power to which a

needs to be raised to reach a givenbaseis called the logarithm of thatnumberfor the respectivenumberbase.

For finding logarithmic two necessary factors that need to be known areandbasenumber.

Examples:

= loglogarithm of 8 for base 2_{2}(8) = 3,2Explanation:^{3}_{ }= 8 Since 2 needs to be raised to a power of 3 to give 8, Thus logarithm of 8 base 2 is 3.

= loglogarithm of 81 for base 9_{9}(81) = 2,9Explanation:^{2}_{ }= 81 Since 9 needs to be raised to a power of 2 to give 81, Thus logarithm of 81 base 9 is 2.

** Note:** An

**is the exact opposite of a**

**exponential function****. When a value is being multiplied repeatedly it is said to grow exponentially whereas when the value is being divided repeatedly it is said to grow logarithmically.**

**logarithmic function**## Different types of Logarithmic Complexities

Now that we know what is a logarithm, let’s deep dive into different types of logarithmic complexities that exists, such as:

__1. Simple Log Complexity (Log___{a}__ b)__

_{a}

Simple logarithmic complexity refers to ** log of b to the base a**. As mentioned, it refers to the time complexity in terms of base a. In design and analysis of algorithms, we generally use 2 as the base for log time complexities. The below graph shows how the simple log complexity behaves.

There are several standard algorithms that have ** logarithmic **time complexity:

**2. Double Logarithm (log log N)**

**2. Double Logarithm (log log N)**Double logarithm is the power to which a ** base** must be raised to reach a value

**such that when the base is raised to a power**

**x****it reaches a value equal to given**

**x**

**number.****Example:**

=logarithm (logarithm (256)) for base 2log_{2}(log_{2}(256))= log_{2}(8) = 3.

2Explanation:^{8}_{ }= 256, Since 2 needs to be raised to a power of 8 to give 256, Thus logarithm of 256 base 2 is 8. Now 2 needs to be raised to a power of 3 to give 8 so log_{2}(8) = 3.

__3. N logarithm N (N * log N)__

N*logN complexity refers to ** product of N and log of N to the base 2**. N * log N time complexity is generally seen in sorting algorithms like Quick sort, Merge Sort, Heap sort. Here N is the size of data structure (array) to be sorted and log N is the average number of comparisons needed to place a value at its right place in the sorted array.

__4. logarithm__^{2}__ N (log__^{2}__ N)__

^{2}

^{2}

log^{2} N complexity refers to ** square of log of N to the base 2**.

__5. N__^{2}__ logarithm N (N__^{2}__ * log N)__

^{2}

^{2}

N^{2}*log N complexity refers to ** product of square of N and log of N to the base 2**. This Order of time complexity can be seen in case where an N * N * N 3D matrix needs to be sorted along the rows. The complexity of sorting each row would be N log N and for N rows it will be N * (N * log N). Thus the complexity will be N

^{2}log N,

__6. N__^{3 }__logarithm N (N__^{3}__ log N)__

^{3 }

^{3}

N^{3}*log N complexity refers to ** product of cube of N and log of N to the base 2**. This Order of time complexity can be seen in cases where an N * N matrix needs to be sorted along the rows. The complexity of sorting each row would be N log N and for N rows it will be N * (N * log N) and for N width it will be N * N * (N log N). Thus the complexity will be N

^{3}log N,

__7. logarithm √N (log √N)__

log √N complexity refers to ** log of square root of N to the base 2**.

## Examples To Demonstrate Logarithmic Time Complexity

__Example 1: log___{a}__ b__

_{a}

** Task:** We have a number

**which has an initial value of**

**N****and the task is to reduce the given number to 1 by repeated division of 2.**

**16**

**Approach:**- Initialize a variable number_of_operation with a value 0 .
- Run a for loop from N till 1.
- In each iteration reduce the value of N to half.
- Increment the number_of_operation variable by one.

- Return the number_of_operation variable.

**Implementation:**

## C++

`// C++ code for reducing a number to its logarithm ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 16; ` ` ` `int` `number_of_operations = 0; ` ` ` ` ` `cout << ` `"Logarithmic reduction of N: "` `; ` ` ` `for` `(` `int` `i = N; i > 1; i = i / 2) { ` ` ` `cout << i << ` `" "` `; ` ` ` `number_of_operations++; ` ` ` `} ` ` ` `cout << ` `'\n'` ` ` `<< ` `"Algorithm Runtime for reducing N to 1: "` ` ` `<< number_of_operations; ` `}` |

## Java

`/*package whatever //do not write package name here */` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `N = ` `16` `; ` ` ` `int` `number_of_operations = ` `0` `; ` ` ` ` ` `System.out.print(` `"Logarithmic reduction of N: "` `); ` ` ` `for` `(` `int` `i = N; i > ` `1` `; i = i / ` `2` `) { ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `number_of_operations++; ` ` ` `} ` ` ` `System.out.println(); ` ` ` `System.out.print(` `"Algorithm Runtime for reducing N to 1: "` `+ number_of_operations); ` ` ` `} ` `} ` |

## Python3

`# python3 code for the above approach ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `16` ` ` `number_of_operations ` `=` `0` ` ` `print` `(` `"Logarithmic reduction of N: "` `, end ` `=` `"") ` ` ` `i ` `=` `N ` ` ` `while` `(i>` `1` `) : ` ` ` `print` `( i , end ` `=` `" "` `) ` ` ` `number_of_operations ` `+` `=` `1` ` ` `i ` `=` `i ` `/` `/` `2` ` ` ` ` `print` `() ` ` ` `print` `(` `"Algorithm Runtime for reducing N to 1:"` `, number_of_operations) ` ` ` ` ` `# This code is contributed by sanjoy_62.` |

## C#

`// C# implementation of above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG { ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 16; ` ` ` `int` `number_of_operations = 0; ` ` ` ` ` `Console.Write(` `"Logarithmic reduction of N: "` `); ` ` ` `for` `(` `int` `i = N; i > 1; i = i / 2) { ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `number_of_operations++; ` ` ` `} ` ` ` `Console.WriteLine(); ` ` ` `Console.WriteLine(` `"Algorithm Runtime for reducing N to 1: "` `+ number_of_operations); ` `} ` `}` |

## Javascript

`let number_of_operations = 0; ` `let n= 16; ` `for` `(let i=n; i>1; i=i/2) { ` ` ` `console.log(i); ` ` ` `number_of_operations++; ` `} ` ` ` `console.log(number_of_operations);` |

**Output**

Logarithmic reduction of N: 16 8 4 2 Algorithm Runtime for reducing N to 1: 4

**Explanation:**

It is clear from the above algorithm that in each iteration the value is divided by a factor of 2 starting from 16 till it reaches 1, it takes 4 operations.

As the input value gets reduced by a factor of 2, In mathematical terms the number of operations required in this case is **log**_{2}** (N),** i.e.

**log**

_{2}**in terms of time complexity, the above algorithm takes logarithmic runtime to complete i.e.**

**(16) = 4.So,**

**log**

_{2}**.**

**(N)****Example 2: Binary search algorithm (log N)**

**Example 2: Binary search algorithm (log N)**Linearly Searching a value in an array of size ** N **can be very hectic, even when the array is sorted but using binary search this can be done in a lot easier way and in lesser time as the algorithm reduces the search space by half in each operation thus gives a complexity of

**log**

_{2}**Here**

**(N),****is 2 because process repeatedly reduces to half.**

**base**Consider an array ** Arr[] **= {2, 4, 6, 8, 10, 12, 14, 16, 18}, If it is required to find the index of 8 then the algorithm will work as following:

## C++

`// C++ program for finding the index of 8 ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `find_position(` `int` `val, ` `int` `Arr[], ` `int` `n, ` `int` `& steps) ` `{ ` ` ` `int` `l = 0, r = n - 1; ` ` ` ` ` `while` `(l <= r) { ` ` ` `steps++; ` ` ` `int` `m = l + (r - l) / 2; ` ` ` `if` `(Arr[m] == val) ` ` ` `return` `m; ` ` ` `else` `if` `(Arr[m] < val) ` ` ` `l = m + 1; ` ` ` `else` ` ` `r = m - 1; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `Arr[8] = { 2, 4, 6, 8, 10, 12, 14, 16 }; ` ` ` `int` `steps = 0; ` ` ` ` ` `// Function Call ` ` ` `int` `idx = find_position(8, Arr, 8, steps); ` ` ` `cout << ` `"8 was present on index: "` `<<idx << endl; ` ` ` ` ` `// Since the worst case runtime of Binary search is ` ` ` `// log(N) so the count of steps must be less than log(N) ` ` ` `cout << ` `"Algorithm Runtime: "` `<< steps << endl; ` ` ` ` ` `return` `0; ` `}` |

## Java

`// Java program for finding the index of 8 ` `import` `java.io.*; ` `class` `GFG { ` ` ` ` ` `static` `int` `steps = ` `0` `; ` ` ` `static` `int` `find_position(` `int` `val, ` `int` `Arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `l = ` `0` `, r = n - ` `1` `; ` ` ` ` ` `while` `(l <= r) { ` ` ` `steps++; ` ` ` `int` `m = l + (r - l) / ` `2` `; ` ` ` `if` `(Arr[m] == val) ` ` ` `return` `m; ` ` ` `else` `if` `(Arr[m] < val) ` ` ` `l = m + ` `1` `; ` ` ` `else` ` ` `r = m - ` `1` `; ` ` ` `} ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `int` `Arr[] = { ` `2` `, ` `4` `, ` `6` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `, ` `16` `}; ` ` ` `steps = ` `0` `; ` ` ` ` ` `// Function Call ` ` ` `int` `idx = find_position(` `8` `, Arr, ` `8` `); ` ` ` `System.out.println(` `"8 was present on index: "` `+idx); ` ` ` ` ` `// Since the worst case runtime of Binary search is ` ` ` `// log(N) so the count of steps must be less than log(N) ` ` ` `System.out.println(` `"Algorithm Runtime: "` `+ steps); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Aman Kumar ` |

## Python3

`# Python program for finding the index of 8 ` ` ` `def` `find_position(val,Arr,n): ` ` ` `global` `steps ` ` ` `l` `=` `0` ` ` `r` `=` `n` `-` `1` ` ` ` ` `while` `(l<` `=` `r): ` ` ` `steps` `+` `=` `1` ` ` `m` `=` `l` `+` `(r` `-` `l)` `/` `/` `2` ` ` `if` `(Arr[m] ` `=` `=` `val): ` ` ` `return` `m ` ` ` `elif` `(Arr[m] < val): ` ` ` `l` `=` `m` `+` `1` ` ` `else` `: ` ` ` `l` `=` `m` `-` `1` ` ` ` ` `return` `-` `1` ` ` `# Driver code ` `Arr` `=` `[` `2` `, ` `4` `, ` `6` `, ` `8` `, ` `10` `, ` `12` `, ` `14` `, ` `16` `] ` `steps` `=` `0` ` ` `# Function Call ` `idx ` `=` `find_position(` `8` `, Arr, ` `8` `) ` ` ` `print` `(` `"8 was present on index: {0}"` `.` `format` `(idx)) ` ` ` `# Since the worst case runtime of Binary search is ` `# log(N) so the count of steps must be less than log(N) ` `print` `(` `"Algorithm Runtime: {0}"` `.` `format` `(steps)) ` ` ` `# This code is contributed by Pushpesh Raj. ` |

## C#

`using` `System; ` ` ` `namespace` `GFG { ` ` ` `class` `Program { ` ` ` ` ` `static` `int` `steps = 0; ` ` ` ` ` `static` `int` `FindPosition(` `int` `val, ` `int` `[] arr, ` `int` `n) { ` ` ` `int` `l = 0, r = n - 1; ` ` ` ` ` `while` `(l <= r) { ` ` ` `steps++; ` ` ` `int` `m = l + (r - l) / 2; ` ` ` `if` `(arr[m] == val) { ` ` ` `return` `m; ` ` ` `} ` ` ` `else` `if` `(arr[m] < val) { ` ` ` `l = m + 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `r = m - 1; ` ` ` `} ` ` ` `} ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `static` `void` `Main(` `string` `[] args) { ` ` ` `int` `[] arr = { 2, 4, 6, 8, 10, 12, 14, 16 }; ` ` ` `steps = 0; ` ` ` ` ` `int` `idx = FindPosition(8, arr, 8); ` ` ` `Console.WriteLine(` `"8 was present on index: "` `+ idx); ` ` ` ` ` `Console.WriteLine(` `"Algorithm runtime: "` `+ steps); ` ` ` `} ` ` ` `} ` `} ` `//This code is contributed by Edula Vinay Kumar Reddy ` |

## Javascript

`// JavaScript program for finding the index of 8 ` `let steps = 0; ` ` ` `function` `find_position(val, Arr, n) { ` ` ` `let l = 0; ` ` ` `let r = n - 1; ` ` ` ` ` `while` `(l <= r) { ` ` ` `steps += 1; ` ` ` `let m = Math.floor(l + (r - l) / 2); ` ` ` `if` `(Arr[m] === val) { ` ` ` `return` `m; ` ` ` `} ` `else` `if` `(Arr[m] < val) { ` ` ` `l = m + 1; ` ` ` `} ` `else` `{ ` ` ` `r = m - 1; ` ` ` `} ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `let Arr = [2, 4, 6, 8, 10, 12, 14, 16]; ` ` ` `// Function Call ` `let idx = find_position(8, Arr, 8); ` ` ` `console.log(`8 was present on index: ${idx}`); ` ` ` `// Since the worst case runtime of Binary search is ` `// log(N) so the count of steps must be less than log(N) ` `console.log(`Algorithm Runtime: ${steps}`);` |

**Output**

8 was present on index: 3 Algorithm Runtime: 1

**Explanation:**

Binary search works on ** Divide and conquer** approach, In above example In worst case 3 comparisons will be needed to find any value in array. Also the value of log (N) where N is input size i.e. 8 for above example will be 3. Hence the algorithm can be said to exhibit logarithmic time complexity.

**Example 3: Binary search algorithm (log log N)**

**Example 3: Binary search algorithm (log log N)**An example where the time complexity of algorithm is ** Double logarithmic **along with a length factor

**is when prime numbers from 1 to**

**N****need to be found.**

**N**## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `long` `long` `MAX_SIZE = 1000001; ` ` ` `// isPrime[] : isPrime[i] is true if number is prime ` `// prime[] : stores all prime number less than N ` `// SPF[] that store smallest prime factor of number ` `// [for Exp : smallest prime factor of '8' and '16' ` `// is '2' so we put SPF[8] = 2 , SPF[16] = 2 ] ` `vector<` `long` `long` `> isprime(MAX_SIZE, ` `true` `); ` `vector<` `long` `long` `> prime; ` `vector<` `long` `long` `> SPF(MAX_SIZE); ` ` ` `// Function generate all prime number less than N in O(n) ` `void` `manipulated_seive(` `int` `N) ` `{ ` ` ` `// 0 and 1 are not prime ` ` ` `isprime[0] = isprime[1] = ` `false` `; ` ` ` ` ` `// Fill rest of the entries ` ` ` `for` `(` `long` `long` `int` `i = 2; i < N; i++) { ` ` ` `// If isPrime[i] == True then i is ` ` ` `// prime number ` ` ` `if` `(isprime[i]) { ` ` ` `// put i into prime[] vector ` ` ` `prime.push_back(i); ` ` ` ` ` `// A prime number is its own smallest ` ` ` `// prime factor ` ` ` `SPF[i] = i; ` ` ` `} ` ` ` ` ` `// Remove all multiples of i*prime[j] which are ` ` ` `// not prime by making isPrime[i*prime[j]] = false ` ` ` `// and put smallest prime factor of i*Prime[j] as ` ` ` `// prime[j] [ for exp :let i = 5 , j = 0 , prime[j] ` ` ` `// = 2 [ i*prime[j] = 10 ] so smallest prime factor ` ` ` `// of '10' is '2' that is prime[j] ] this loop run ` ` ` `// only one time for number which are not prime ` ` ` `for` `(` `long` `long` `int` `j = 0; ` ` ` `j < (` `int` `)prime.size() && i * prime[j] < N ` ` ` `&& prime[j] <= SPF[i]; ` ` ` `j++) { ` ` ` `isprime[i * prime[j]] = ` `false` `; ` ` ` ` ` `// put smallest prime factor of i*prime[j] ` ` ` `SPF[i * prime[j]] = prime[j]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `N = 13; ` `// Must be less than MAX_SIZE ` ` ` ` ` `manipulated_seive(N); ` ` ` ` ` `// Print all prime number less than N ` ` ` `for` `(` `int` `i = 0; i < prime.size() && prime[i] <= N; i++) ` ` ` `cout << prime[i] << ` `" "` `; ` ` ` ` ` `return` `0; ` `}` |

## Java

`import` `java.util.*; ` ` ` `public` `class` `Main { ` ` ` `static` `final` `int` `MAX_SIZE = ` `1000001` `; ` ` ` ` ` `// isprime[] : isprime[i] is true if number is prime ` ` ` `// prime[] : stores all prime numbers less than N ` ` ` `// SPF[] that store smallest prime factor of number ` ` ` `// [for Exp : smallest prime factor of '8' and '16' ` ` ` `// is '2' so we put SPF[8] = 2 , SPF[16] = 2 ] ` ` ` `static` `boolean` `[] isprime = ` `new` `boolean` `[MAX_SIZE]; ` ` ` `static` `List<Integer> prime = ` `new` `ArrayList<Integer>(); ` ` ` `static` `int` `[] SPF = ` `new` `int` `[MAX_SIZE]; ` ` ` ` ` `// Function generate all prime numbers less than N in ` ` ` `// O(n) ` ` ` `static` `void` `manipulated_seive(` `int` `N) ` ` ` `{ ` ` ` `Arrays.fill(isprime, ` `true` `); ` ` ` ` ` `// 0 and 1 are not prime ` ` ` `isprime[` `0` `] = isprime[` `1` `] = ` `false` `; ` ` ` ` ` `// Fill rest of the entries ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) { ` ` ` `// If isprime[i] is true then i is prime number ` ` ` `if` `(isprime[i]) { ` ` ` `// put i into prime[] list ` ` ` `prime.add(i); ` ` ` ` ` `// A prime number is its own smallest prime ` ` ` `// factor ` ` ` `SPF[i] = i; ` ` ` `} ` ` ` ` ` `// Remove all multiples of i*prime[j] which are ` ` ` `// not prime by making isprime[i*prime[j]] = ` ` ` `// false and put the smallest prime factor of ` ` ` `// i*Prime[j] as prime[j] [for example: let i = ` ` ` `// 5, j = 0, prime[j] = 2 [i*prime[j] = 10] so ` ` ` `// the smallest prime factor of '10' is '2' that ` ` ` `// is prime[j]] this loop runs only one time for ` ` ` `// numbers which are not prime ` ` ` `for` `(` `int` `j = ` `0` `; ` ` ` `j < prime.size() && i * prime.get(j) < N ` ` ` `&& prime.get(j) <= SPF[i]; ` ` ` `j++) { ` ` ` `isprime[i * prime.get(j)] = ` `false` `; ` ` ` ` ` `// put the smallest prime factor of ` ` ` `// i*prime[j] ` ` ` `SPF[i * prime.get(j)] = prime.get(j); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `13` `; ` `// Must be less than MAX_SIZE ` ` ` ` ` `manipulated_seive(N); ` ` ` ` ` `// Print all prime numbers less than N ` ` ` `for` `(` `int` `i = ` `0` `; ` ` ` `i < prime.size() && prime.get(i) <= N; i++) { ` ` ` `System.out.print(prime.get(i) + ` `" "` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `// This code is contributed by divyansh2212` |

## Python3

`import` `math ` ` ` `MAX_SIZE ` `=` `1000001` ` ` `# isprime[]: isprime[i] is True if number is prime ` `# prime[]: stores all prime numbers less than N ` `# SPF[] that store smallest prime factor of number ` `# [for Exp: smallest prime factor of '8' and '16' ` `# is '2' so we put SPF[8] = 2, SPF[16] = 2] ` `isprime ` `=` `[` `True` `] ` `*` `MAX_SIZE ` `prime ` `=` `[] ` `SPF ` `=` `[` `0` `] ` `*` `MAX_SIZE ` ` ` `# Function generate all prime numbers less than N in O(n) ` `def` `manipulated_seive(N): ` ` ` `global` `isprime, prime, SPF ` ` ` ` ` `# 0 and 1 are not prime ` ` ` `isprime[` `0` `] ` `=` `isprime[` `1` `] ` `=` `False` ` ` ` ` `# Fill rest of the entries ` ` ` `for` `i ` `in` `range` `(` `2` `, N): ` ` ` `# If isprime[i] is True then i is prime number ` ` ` `if` `isprime[i]: ` ` ` `# put i into prime[] list ` ` ` `prime.append(i) ` ` ` ` ` `# A prime number is its own smallest prime factor ` ` ` `SPF[i] ` `=` `i ` ` ` ` ` `# Remove all multiples of i*prime[j] which are ` ` ` `# not prime by making isprime[i*prime[j]] = False ` ` ` `# and put the smallest prime factor of i*Prime[j] as ` ` ` `# prime[j] [for example: let i = 5, j = 0, prime[j] ` ` ` `# = 2 [i*prime[j] = 10] so the smallest prime factor ` ` ` `# of '10' is '2' that is prime[j]] this loop runs ` ` ` `# only one time for numbers which are not prime ` ` ` `j ` `=` `0` ` ` `while` `j < ` `len` `(prime) ` `and` `i ` `*` `prime[j] < N ` `and` `prime[j] <` `=` `SPF[i]: ` ` ` `isprime[i ` `*` `prime[j]] ` `=` `False` ` ` ` ` `# put the smallest prime factor of i*prime[j] ` ` ` `SPF[i ` `*` `prime[j]] ` `=` `prime[j] ` ` ` ` ` `j ` `+` `=` `1` ` ` `# Driver program to test above function ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `N ` `=` `13` `# Must be less than MAX_SIZE ` ` ` ` ` `manipulated_seive(N) ` ` ` ` ` `# Print all prime numbers less than N ` ` ` `for` `i ` `in` `range` `(` `len` `(prime)): ` ` ` `if` `prime[i] <` `=` `N: ` ` ` `print` `(prime[i], end` `=` `" "` `) ` ` ` `else` `: ` ` ` `break` |

## C#

`using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `MainClass ` `{ ` ` ` `static` `readonly` `int` `MAX_SIZE = 1000001; ` ` ` ` ` `// isprime[] : isprime[i] is true if number is prime ` ` ` `// prime[] : stores all prime numbers less than N ` ` ` `// SPF[] that store smallest prime factor of number ` ` ` `// [for Exp : smallest prime factor of '8' and '16' ` ` ` `// is '2' so we put SPF[8] = 2 , SPF[16] = 2 ] ` ` ` `static` `bool` `[] isprime = ` `new` `bool` `[MAX_SIZE]; ` ` ` `static` `List<` `int` `> prime = ` `new` `List<` `int` `>(); ` ` ` `static` `int` `[] SPF = ` `new` `int` `[MAX_SIZE]; ` ` ` ` ` `// Function generate all prime numbers less than N in ` ` ` `// O(n) ` ` ` `static` `void` `manipulated_seive(` `int` `N) ` ` ` `{ ` ` ` `Array.Fill(isprime, ` `true` `); ` ` ` ` ` `// 0 and 1 are not prime ` ` ` `isprime[0] = isprime[1] = ` `false` `; ` ` ` ` ` `// Fill rest of the entries ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` `{ ` ` ` `// If isprime[i] is true then i is prime number ` ` ` `if` `(isprime[i]) ` ` ` `{ ` ` ` `// put i into prime[] list ` ` ` `prime.Add(i); ` ` ` ` ` `// A prime number is its own smallest prime ` ` ` `// factor ` ` ` `SPF[i] = i; ` ` ` `} ` ` ` ` ` `// Remove all multiples of i*prime[j] which are ` ` ` `// not prime by making isprime[i*prime[j]] = ` ` ` `// false and put the smallest prime factor of ` ` ` `// i*Prime[j] as prime[j] [for example: let i = ` ` ` `// 5, j = 0, prime[j] = 2 [i*prime[j] = 10] so ` ` ` `// the smallest prime factor of '10' is '2' that ` ` ` `// is prime[j]] this loop runs only one time for ` ` ` `// numbers which are not prime ` ` ` `for` `(` `int` `j = 0; ` ` ` `j < prime.Count && i * prime[j] < N ` ` ` `&& prime[j] <= SPF[i]; ` ` ` `j++) ` ` ` `{ ` ` ` `isprime[i * prime[j]] = ` `false` `; ` ` ` ` ` `// put the smallest prime factor of ` ` ` `// i*prime[j] ` ` ` `SPF[i * prime[j]] = prime[j]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `N = 13; ` `// Must be less than MAX_SIZE ` ` ` ` ` `manipulated_seive(N); ` ` ` ` ` `// Print all prime numbers less than N ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < prime.Count && prime[i] <= N; i++) ` ` ` `{ ` ` ` `Console.Write(prime[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` `} ` |

## Javascript

`const MAX_SIZE = 1000001; ` ` ` `// isprime[]: isprime[i] is true if number is prime ` `// prime[]: stores all prime numbers less than N ` `// SPF[] that store smallest prime factor of number ` `// [for Exp: smallest prime factor of '8' and '16' ` `// is '2' so we put SPF[8] = 2, SPF[16] = 2] ` `let isprime = Array(MAX_SIZE).fill(` `true` `); ` `let prime = []; ` `let SPF = Array(MAX_SIZE).fill(0); ` ` ` `// Function generate all prime numbers less than N in O(n) ` `function` `manipulated_seive(N) { ` ` ` `// 0 and 1 are not prime ` ` ` `isprime[0] = isprime[1] = ` `false` `; ` ` ` ` ` `// Fill rest of the entries ` ` ` `for` `(let i = 2; i < N; i++) { ` ` ` `// If isprime[i] is true then i is prime number ` ` ` `if` `(isprime[i]) { ` ` ` `// put i into prime[] list ` ` ` `prime.push(i); ` ` ` ` ` `// A prime number is its own smallest prime factor ` ` ` `SPF[i] = i; ` ` ` `} ` ` ` ` ` `// Remove all multiples of i*prime[j] which are ` ` ` `// not prime by making isprime[i*prime[j]] = false ` ` ` `// and put the smallest prime factor of i*Prime[j] as ` ` ` `// prime[j] [for example: let i = 5, j = 0, prime[j] ` ` ` `// = 2 [i*prime[j] = 10] so the smallest prime factor ` ` ` `// of '10' is '2' that is prime[j]] this loop runs ` ` ` `// only one time for numbers which are not prime ` ` ` `let j = 0; ` ` ` `while` `(j < prime.length && i * prime[j] < N && prime[j] <= SPF[i]) { ` ` ` `isprime[i * prime[j]] = ` `false` `; ` ` ` ` ` `// put the smallest prime factor of i*prime[j] ` ` ` `SPF[i * prime[j]] = prime[j]; ` ` ` ` ` `j++; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver program to test above function ` `const N = 13; ` `// Must be less than MAX_SIZE ` ` ` `manipulated_seive(N); ` ` ` `// Print all prime numbers less than N ` `console.log(prime.join(` `' '` `)); ` ` ` `// Contributed by adityasha4x71` |

**Output**

2 3 5 7 11

In above example the complexity of finding prime numbers in a range of 0 to ** N **is O(N * log (log (N))).

## Practice Problems for Logarithmic Time Complexity

Articles | Practice | Time Complexity |

Search an element in a sorted and rotated Array | Solve | O(log N) |

Sieve of Eratosthenes – GeeksforGeeks | Solve | O(n*log(log(n))) |

Count Inversions in an array | Solve | O(n*log n) |

QuickSort | Solve | O(n*log n) |

Prim’s Minimum Spanning Tree | Solve | O(E log V) |

## Comparison between various Logarithmic Time Complexities

Below is a graph to show the comparison between different logarithmic time complexities that have been discussed above:

**Frequently Asked Questions(FAQ’s) on Logarithmic Time Complexity:**

**Frequently Asked Questions(FAQ’s) on Logarithmic Time Complexity:**

**1) **Why does logarithmic complexity need no base?

**1)**

Logarithms from any base i.e. 2, 10, e can be transformed to any other base with an addition of a constant, So the base of log doesn’t matter.

**2) How are logarithms used in real life?**

**2) How are logarithms used in real life?**

In Real Life scenario like measuring the acidic, basic or neutral behavior of a substance that describes a chemical property in terms of pH value logarithm is used.

**3) Is logarithm repeated division?**

**3) Is logarithm repeated division?**

Logarithm is repeated division by the base ** b** until 1 is reached. The logarithm is the number of divisions by b. Repeated division doesn’t always result in exactly 1.

**4) What is the difference between logarithm and algorithm?**

**4) What is the difference between logarithm and algorithm?**

Algorithm is a step by step process to solve a certain problem whereas logarithm is an exponent.

**5) Why is binary search logarithmic?**

**5) Why is binary search logarithmic?**

Binary search is a Divide and Conquer method of searching, its key idea is to reduce the search space to half after each comparison to find the key. Thus the search space repeatedly drops by half and the complexity is logarithmic.

**6) What is faster N or log N?**

**6) What is faster N or log N?**

log N is faster than N as the value of log N is smaller than N.

**7) What is faster O(1) or O(log N)?**

**7) What is faster O(1) or O(log N)?**

O(1) is faster than O(log N), as O(1) constant time complexity and fastest possible .

**8) What is best case time complexity?**

**8) What is best case time complexity?**

In the best case constant number of operations need to be performed irrespective of value of N. So time complexity in the best case would be O(1) i.e. Most optimal time complexity.

## Conclusion

From the above discussion, we conclude that the analysis of an algorithm is very important for choosing an appropriate algorithm and** the **Logarithm order of complexities is one of the most optimal order of time complexities.