366 views
1 votes
1 votes

Consider the $fast \: square$ and $multiply \: algorithm$ to calculate $x^y \: mod \: N$ as given below, where $x, \: y,\: N$ are positive integers and $1 \leq x, y < N$.

Input: $x, \:y, \:N$
Output: $x^y \: mod \:N$
1. z = y, u = 1, v = x;
2. while z > 0 do
3. if z $\equiv$ 1 mod 2 then
4. u = uv mod N;
end
5. $v = v^2 \: mod \: N; \: z = \lfloor \frac{z}{2} \rfloor$
end
6. return u
  1. Write a C function to implement the algorithm. Your function should take three arguments $x, y$ and $N$, and return the value $x^y \: mod \: N$, all of which are of type $unsigned long long$ (i.e., 64-bit unsigned integers). 
  2. Discuss whether your program works perfectly for all possible input combinations.
 

Please log in or register to answer this question.

Related questions

566
views
0 answers
1 votes
go_editor asked Jun 1, 2016
566 views
A connected, simple, undirected planar graph $G(V, E)$ is given where $V$ denotes the set of vertices and E denotes the set of edges. In $V$, ... .]
523
views
0 answers
1 votes
go_editor asked Jun 1, 2016
523 views
Let $M$ be an $(n \times n)$ matrix where each element is a distinct positive integer. Construct another matrix $M'$ by permuting the rows and/or ... how much additional storage, other than the matrix itself, is required in your algorithm.
524
views
0 answers
2 votes
go_editor asked Jun 1, 2016
524 views
Draw a complete binary tree $T$ with $(N − 1)$ nodes where $N = 2^n$. Suppose each node in $T$ is a processor and each edge of $T$ is a physical link between two processors ... $i$ in $O(\log N + M)$ time.
339
views
0 answers
1 votes
go_editor asked May 31, 2016
339 views
Consider the $\text{fast square}$ and $\text{multiply algorithm}$ to calculate $x^y \text{ mod } N$ as given below, where $x, \: y,\: N$ are positive integers ... is $O(log^2 \: N)$, when the positive integers involved are less than $N$.]