edited by
12,497 views
44 44 votes

Suppose $c = \langle c[0], \dots, c[k-1]\rangle$ is an array of length $k$, where all the entries are from the set $\{0, 1\}$. For any positive integers $a \text{ and } n$, consider the following pseudocode.

DOSOMETHING (c, a, n)

z ← 1
for i ← 0 to k-1
     do z ← z² mod n
     if c[i]=1
           then z ← (z × a) mod n
return z

If $k=4, c = \langle 1, 0, 1, 1 \rangle , a = 2, \text{ and } n=8$, then the output of DOSOMETHING(c, a, n) is _______.

7 Answers

Best answer
50 50 votes
Initially $k = 4$, $c = [1, 0, 1, 1]$, $a = 2$, $n = 8$.

Now let's iterate through the function step by step :

$z = 1$ (at the start of do-something)

$i = 0$ (start of external for loop)

In the do loop

$z = 1*1 % 8 = 1$ (non zero value so considered as true and continue)

$c[0] = 1$, so in the if clause, $z = 1*2 \% 8 = 2$

In the do loop

$z = 2*2 \% 8 = 4$ (since now $z = 2$) (non zero value so considered as true and continue)

$c[0] = 1$, so in the if clause, $z = 4*2 \% 8 = 0$

Now no need to check further :

Reason :  All the operations that update $Z$ are multiplicative operations and hence the value of $Z$ will never change from $0$.
edited by
23 23 votes

z=1  k = 4, c = [1, 0, 1, 1], a = 2, n = 8

now if we analyze the code we will get table like this Hence Ans is 0.

i z
0 2
1 4
2 0
3 0
6 6 votes
Answer is 0. By manually iterating through the steps by pencil and paper we can get this answer
edited by
4 4 votes
we can do it mentally , at one stage value of z will be zero , beyond that , for any value of K it will be 0 only.

Ans :0
4 4 votes

this is the algorithm to find  amod n.

here a =2, c =(1011)2 = 11, n=8

hence 211 mod 8 = 0

Answer:
Position:
Show:

Related questions

90 90 votes
14 answers 14 answers
31.1k
31.1k views
go_editor asked Feb 15, 2015
31,077 views
Let $f(n) = n$ and $g(n) = n^{(1 + \sin \: n)}$, where $n$ is a positive integer. Which of the following statements is/are correct?$f(n) = O(g(n))$$f(n) = \Omega(g(n))$On...
80 80 votes
5 answers 5 answers
29.1k
29.1k views
go_editor asked Feb 15, 2015
29,127 views
Consider the following recursive C function.void get(int n) { if (n<1) return; get (n-1); get (n-3); printf("%d", n); }If $get(6)$ function is being called in $main()$ th...
93 93 votes
6 answers 6 answers
28.3k
28.3k views
go_editor asked Feb 14, 2015
28,258 views
Consider the equality $\displaystyle{\sum_{i=0}^n} i^3 = X$ and the following choices for $X$:$\Theta(n^4)$$\Theta(n^5)$$O(n^5)$$\Omega(n^3)$The equality above remains co...
84 84 votes
12 answers 12 answers
33.1k
33.1k views
go_editor asked Feb 12, 2015
33,073 views
Consider the following C function.int fun(int n) { int x=1, k; if (n==1) return x; for (k=1; k<n; ++k) x = x + fun(k) * fun (n-k); return x; }The return value of $fun(5)$...