Consider the following C function
int fun(int n) { int i, j; for(i=1; i<=n; i++) { for (j=1; j<n; j+=i) { printf("%d %d", i, j); } } }
Time complexity of $fun$ in terms of $\Theta$ notation is
https://math.stackexchange.com/questions/5035/is-there-any-formula-for-the-series-1-frac12-frac13-cdots-frac-1-n
$i=1 \rightarrow j : 1 ..2..3..4..5..6..7..8..9..10......approx (n times)$
$i=2 \rightarrow j : 1 ..3..5..7..9..11..13.................approx (n/2 times)$
$i=3 \rightarrow j : 1 ..4..7..10..13..14..17..............approx (n/3 times)$
$T(n)= n + n/2 + (n/3) + (n/4)+ (n/5) + (n/6)......... =\Theta (nlogn)$ {best option}
We can do this using integration :-
Its a summation of n term harmonic series :-
Refer:- https://en.wikipedia.org/wiki/Harmonic_number
how this summation become log n, source : https://www.quora.com/What-is-the-sum-of-the-series-1+-1-2-+-1-3-+-1-4-+-1-5-up-to-infinity-How-can-it-be-calculated Edit : solved : its not approaching infinite rather going to n
Ans) c
First time inner loop run -----n times
2nd time '' '' ''----n/2 times
Then,n/3,n/4....till n/n=1 time
Total=n+n/2+n/3+n/4+n/5+......+n/n=n(1+1/2+1/3+...+1/n)=⊝(nlogn) (approx)
64.3k questions
77.9k answers
244k comments
80.0k users