Given the following code -:
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j = j+i)
{
//Do something
}
I know that the outer loop runs N
times, and that the inner loop runs approximately log(N)
times. This is because on each iteration of i
, j
runs ceil(N)
, ceil(N/2)
, ceil(N/4)
times and so on. This is just a rough calculation through which one can guess that the time complexity will definitely be O(N log(N))
.
How would I mathematically prove the same?
I know that for the ith
iteration, j
increments by ceil(N/2(i - 1))
.
log
to base 2.