recategorized by
13,998 views
50 votes
50 votes

The following function computes $X^{Y}$ for positive integers $X$ and $Y$.

int exp (int X, int Y) { 
     int res =1, a = X, b = Y;
   
     while (b != 0) { 
         if (b % 2 == 0) {a = a * a; b = b/2; } 
         else         {res = res * a; b = b - 1; } 
     } 
     return res; 
}

Which one of the following conditions is TRUE before every iteration of the loop?

  1. $X^{Y} = a^{b}$
  2. $(res * a)^{Y} = (res * X)^{b}$
  3. $X^{Y} = res * a^{b}$
  4. $X^{Y} = (res * a)^{b}$
recategorized by

7 Answers

3 votes
3 votes

Let us take x = X and Y = 27 then execution goes as follow -->

Now at few random steps you can cross verify. Answer is (C) Part.

Answer:

Related questions

24 votes
24 votes
3 answers
3
Kathleen asked Sep 12, 2014
4,489 views
Consider the following PASCAL program segment:if i mod 2 = 0 then while i >= 0 do begin i := i div 2; if i mod 2 < 0 then i := i - 1; else i := i – 2; end;An appropria...
36 votes
36 votes
6 answers
4