Homepage › Solution manuals › Ivan Niven › An Introduction to the Theory of Numbers › Exercise 2.4.21* (Modular product)
Exercise 2.4.21* (Modular product)
Let be a large positive integer. Suppose that , and that . Explain why the number determined by the following algorithm satisfies , and . Verify that in executing the algorithm, all numbers encountered lie in the interval .
- 1.
- Set .
- 2.
-
As long as
, perform the following operations:
- (a)
- Set .
- (b)
- Choose so that and .
- (c)
- Replace by .
- (d)
- If , replace by .
- (e)
- Replace by .
- (f)
- Replace by .
Answers
Notations:
- if and .
- if for some ( is the remainder of the Euclidean division of by ). Then .
- : Replace by (in the algorithm).
Proof. We reformulate the algorithm:
- 1.
- Set .
- 2.
-
While
,
- (a)
- .
- (b)
- .
- (c)
- .
- (d)
- If , .
- (e)
- .
- (f)
- .
With python:
def modular_product(a, b, m, X): k, c, g = b, 0, X // m while a>0: r = a % g s = (k * r) % m c = c + s if c >= m: c = c - m k = (g * k) % m a = a // g return c
Now we explain the algorithm. Note that does not change during the execution of the algorithm. We interpret as the base of the system of numeration, so that
where is the numbers of digits in base , and .
Then
where (that is ).
Define , and for .
Now we verify that are the actual values the parameters at the -th passing in the loop (the first passing has index ), if we test these values after the step (d).
The rightmost digits of is , the remainder of by . To obtain the following digits, we replace by . This gives the well known algorithm to decompose a positive integer in the base :
This corresponds to the steps (a) and (f) in the loop, so and are the actual values of and in the loop. Moreover, step (e) gives .
At each passing of the loop, , so that , and when , .
We prove by induction that are in the interval .
, so the property is true for .
Assume that . Then , so . Therefore, after the instruction (d), . Moreover , and . The induction is done.
Finally, , and , therefore , since satisfies . Similarly, , so all numbers encountered lie in the interval .
Since at the end , and , the result is the waited result. □
To give an example, take so that the base is .
If we compute modulo for , with an instruction ’print’ after the step (d), we obtain the following intermediate results
The result is , which is right, since .