Let’s look at the the two loops individually.
The outer loop starts with i=1
, and increments it by k
in each iteration, until it’s greater than n
. In other words it can run n/k
times, or, to be accurate, floor(n/k)
times, since a loop can’t run a non-whole number of times.
The inner loop is relatively simpler – it starts with j=1
and increments it by one in each iteration until it’s greater than k
, for a total of k
times.
Put these two together and you’ll get floor(n/k)*k
.
EDIT:
As pointed out in the comment, this analysis is true if n>=k
. If n<k
the outer loop will run exactly once. I.e., the total times run would be: max(1, floor(n/k))*k
.
4
solved How many time is the inner loop running? [closed]