search
Log In
3 votes
798 views

Consider the following pseudo-code fragment in which an invariant for the loop is “ $m ^*x^k=p^n$ and $k \geq 0$ ” (here, $p$ and $n$ are integer variable that have been initialized):

/* Pre-conditions : $p \geq 1 \wedge n \geq 0$ */

/* Assume that overflow never occurs */

int $x=p$; int $k=n$; int $m=1$;

while $(k < >0)$ {

if (k is odd) then $m=m^*x$;

$x=x^*x$;

$k=\lfloor k/2 \rfloor$;       /* floor$(k/2)$ */

}

Which of the following must be true ar the end of the while loop?

  1. $x=p^n$
  2. $m=p^n$
  3. $p=x^n$
  4. $p=m^n$ 
in Programming
edited by
798 views

1 Answer

0 votes

This pseudo code basically represents the exponentiation algorithm. With p as the base and n as the power and the final result is stored in m.

Answer : B

Related questions

4 votes
3 answers
1
1.2k views
Consider the following C-code fragment running on a $32$-bit $X86$ machine: typedef struct { union { unsigned char a; unsigned short b; } U; unsigned char c; }S; S B[10]; S*p=&B[4]; S*q=&B[5]; p → U.b=0x1234; /* structure S takes 32-bits */ If M is the value of $q-p$ and $N$ is the value of $((\text{int )&} ( p \rightarrow c)) – ((\text{int})p)$, then $(M,N)$ is $(1,1)$ $(3,2)$ $(1,2)$ $(4,4)$
asked Jul 2, 2019 in Programming Arjun 1.2k views
8 votes
3 answers
2
2.7k views
Consider the poset $( \{3,5,9,15,24,45 \}, \mid).$ Which of the following is correct for the given poset ? There exist a greatest element and a least element There exist a greatest element but not a least element There exist a least element but not a greatest element There does not exist a greatest element and a least element
asked Jul 2, 2019 in Set Theory & Algebra Arjun 2.7k views
2 votes
2 answers
3
2.2k views
How many ways are there to place $8$ indistinguishable balls into four distinguishable bins? $70$ $165$ $^8C_4$ $^8P_4$
asked Jul 2, 2019 in Combinatory Arjun 2.2k views
2 votes
2 answers
4
1.3k views
How many bit strings of length ten either start with a $1$ bit or end with two bits $00$ ? $320$ $480$ $640$ $768$
asked Jul 2, 2019 in Combinatory Arjun 1.3k views
...