for(i=1, count=0;i<=n;i=i k)
for(j=1;j<=k;j )
count ;
I can't figure out how many times the inner for loop is executing. I need the answer in terms of n and k
CodePudding user response:
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
.
CodePudding user response:
answer : floor(n/k) * k To check it exactly, I think you can print with print