Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

le Problem 4. Give a closed expression, in terms of n, which corresponds to the

ID: 3878079 • Letter: L

Question

le Problem 4. Give a closed expression, in terms of n, which corresponds to the exact final value returned by the method shown below. The value of n is supposed to be a non-negative value. You must show how is your answer derived. Your final expression should be a closed expression in terms of n; that is, it cannot be a summation or a recurrence expression; although you can use those, if needed, to derive the final result Derive your answer here public static int v(int n) t int r = 0; for (int i=1 ; i

Explanation / Answer

In this loop, i is getting incremented by 2 every time. So the
value of is is increaing as 1,2,4,8,16,....

So the statement r = r+1 is executed
   0 times for n = 1
   1 times for n = 2
   2 times for n = 3
   2 times for n = 4

   So basically looking at the above data number of times the loop is getting executed is log(n)
   to the base 2 with rounded to appropriate whole number.

   n = 1   log(n) Base 2) times   (0 Times)
   n = 2   log(n) Base 2) times   (1 Times)
   n = 3   log(n) Base 2) times   (2 Times) (rounded of to apprpriate whole number)