How many times will a function be executed?

advertisements

I have this piece of code

for ( i = 0; i < N ; i+=3 ){
   foo();
   if( i % 5 ==0) foo();
}

and i am to decide how many times will be foo() executed. I have this options

ceil(N/3)+floor(N/5)
ceil(N/3)+ceil(N/5)
floor((N+2)/3)+floor((N+14)/15)
floor(N/3)+floor(N/5)
floor(N/3)+ceilt(N/5)

what is the right way for deciding the right outcome? Outer loop executes foo() outside of condition N/3 times , but how to decide how many times will condition execute the function?


Just to make it easier to understand we can split this cycle into 2:

for ( i = 0; i < N ; i += 3 ){
   foo();
}

for ( i = 0; i < N ; i += 15 ){
   foo();
}

For any natural N and K cycle (i=0; i< N; i += k) will run 1 + floor((N - 1)/k) times.

This way total number of runs will be: 1 + floor( (N-1) / 3) for first and 1 + floor( (N-1) / 15) for second cycle which is:

2 + floor((N-1) / 3) + floor((N-1) / 15)