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++)` ` ` `printf` `(` `"GeeksforGeeks"` `);` `}` |

*chevron_right*

*filter_none*

Time Complexity of the above function can be written as ?(log 1) + ?(log 2) + ?(log 3) + . . . . + ?(log n) which is ? (log n!)

Order of growth of ‘log n!’ and ‘n log n’ is same for large values of n, i.e., ? (log n!) = ?(n log n). So time complexity of fun() is ?(n log n).

The expression ?(log n!) = ?(n log n) can be easily derived from following Stirling’s approximation (or Stirling’s formula).

log n! = n*log n - n = O(n*log(n))

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

Sources:

http://en.wikipedia.org/wiki/Stirling%27s_approximation

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- An interesting time complexity question
- Time Complexity of building a heap
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- 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
- Time Complexity of Loop with Powers
- What does 'Space Complexity' mean?
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Knowing the complexity in competitive programming
- Cyclomatic Complexity
- Complexity Analysis of Binary Search
- Complexity analysis of various operations of Binary Min Heap
- Time taken by Loop unrolling vs Normal loop
- Measure execution time with high precision in C/C++
- Microsoft Interview experience for full time position of software engineer at Microsoft Ireland Research