Question

Suppose c=(c[0],…,c[k−1])c = (c[0], \dots, c[k - 1]) is an array of length kk, where all the entries are from the set {0,1}\{0, 1\}. For any positive integers aa and nn, consider the following pseudocode:

DOSOMETHING (c, a, n)
z = 1
for i = 0 to k - 1
    do z = z^2 mod n
    if c[i] == 1
        then z = (z * a) mod n
return z

If k=4,c=(1,0,1,1),a=2k = 4, c = (1, 0, 1, 1), a = 2 and n=8n = 8, then the output of DOSOMETHING(c, a, n) is _____.

Official answer and solution
Official answer
0
From
Official GATE 2015 answer key

Solution

Trace the loop iterations of the modular exponentiation algorithm.

Given inputs:

  • k=4k = 4
  • Array c=(1,0,1,1)c = (1, 0, 1, 1) so c[0]=1,c[1]=0,c[2]=1,c[3]=1c[0] = 1, c[1] = 0, c[2] = 1, c[3] = 1
  • Base a=2a = 2
  • Modulus n=8n = 8

Trace execution:

  • Initial: z=1z = 1
  • i=0i = 0: z←(12) mod 8=1z \leftarrow (1^2) \bmod 8 = 1.
    Since c[0]=1c[0] = 1, z←(1×2) mod 8=2z \leftarrow (1 \times 2) \bmod 8 = 2.
  • i=1i = 1: z←(22) mod 8=4z \leftarrow (2^2) \bmod 8 = 4.
    Since c[1]=0c[1] = 0, zz remains 4.
  • i=2i = 2: z←(42) mod 8=16 mod 8=0z \leftarrow (4^2) \bmod 8 = 16 \bmod 8 = 0.
    Since c[2]=1c[2] = 1, z←(0×2) mod 8=0z \leftarrow (0 \times 2) \bmod 8 = 0.
  • i=3i = 3: z←(02) mod 8=0z \leftarrow (0^2) \bmod 8 = 0.
    Since c[3]=1c[3] = 1, z←(0×2) mod 8=0z \leftarrow (0 \times 2) \bmod 8 = 0.

The function returns z=0z = 0.

Final answer

0

Common trap

Overlooking that 4^2 = 16 is divisible by 8 (16 mod 8 = 0), collapsing all subsequent multiplications to 0.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2015 Computer Science and Information Technology (CS) Session 3
Question
59 of 65, printed as CS Q.49
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.