Modular arithmetic and Euclid
The following statement is important for modular arithmetic:
.1
The number is called integer quotient and the remainder. This just says something very intuitive: Dividing by gives us the quotient and remainder .
We say that is a common divisor of and if 2 and . Integer is a greatest common divisor of and if is a common divisor of and , and every common divisor of and divides . We remark that the existence of a greatest common divisor is not obvious (everyone has learned them since grade school and took it for granted that they exist). But really, how can we know that there is one common divisor where everyone else divides it?
Notice that is a greatest common divisor of and iff is also one (and these are the only two!). Denote by the unique non-negative greatest common divisor of and .
For all , there exists a greatest common divisor of and .
We will again show two proofs. The first proof is purely mathematical and does not directly imply an algorithm that computes a greatest common divisor. The second proof is algorithmic and the existence of a greatest common divisor in fact follows from the fact that the algorithm will be proved to return it.
First proof via algebra
For denote by the set of all multiples of , e.g., . Fix integers . We will explicitly define a greatest common divisor of and . First, we claim that every set that is closed under subtraction (i.e. a subtraction of two elements in the set also belongs to the set) must be of a certain form.
If is nonempty and closed under subtraction, then there exists such that .
If is closed under subtraction, notice that
- : Since (here we use that is nonempty, do you see why?)
- (do you see why?)
- (why?)
- (this can be proven by induction!)
Now, we define to be the smallest positive integer in . From the last property above, we have that . We claim that in fact . Consider any number . Apply the division theorem (to divide by ) to write where . Notice that, since , we know that . If , this would be impossible since is positive and smaller than , so we must have , which means that , and thus .
For subsets , we define . For instance would be exactly the set that contains all numbers of the form . The following property is simple to verify.
The set is closed under subtraction.
The lemma implies that for some . We claim that is a greatest common divisor of and . First, notice that because ; by the same reasoning . So, is a common divisor. Now consider any common divisor of and . Since , we know that for some integers . Since and , we have that as well. This concludes the proof.
Notice that this proof implies the following fundamental result in elementary number theory.
For all there exist such that .
Let . Prove that for some . Determine the value of (in terms of other 's).
Second Proof via Euclid's algorithm
Interestingly, the second proof is much older than the first one. The following efficient algorithm is due to Euclid (from around 350 B.C.)
- and
- while do
- return
Given , the algorithm always terminates in finitely many steps.
The above algorithm correctly returns .
We would need to develop some preliminary notation and facts to facilitate the analysis of this algorithm. For integer , denote by the set of all integers that divide , i.e., .
is a greatest common divisor of and if and only if .
It is straightforward to show that for every common divisor of and . The implication follows from this. To see the converse, suppose . Consider any common divisor of and , so we must have that . Therefore, , which means that every common divisor divides . Moreover, since , is a common divisor of and , so is a greatest common divisor.
For simplicity of notation, denote by .
Let . For all integer , .
We argue that the following is loop invariant: Notice that the algorithm updates to and to in the while-loop, so it suffices to argue that . But since , we have that for some integer . From Exercise 33, we have that , so the statement is indeed a loop invariant. Using the loop invariant, when the algorithm leaves the while loop, we have that (recall the reminder is always nonnegative). Therefore, . Lemma 9 implies that is a greatest common divisor of and .
Let be the value of after the -th iteration of the while loop in Euclid's algorithm, starting with . Prove that .
Use the previous exercise, deduce that the Euclid's algorithm terminates in at most iterations where is the number of binary digits of .
Given integers , the operation can be computed in time where is the number of digits of and .
These exercises imply that Euclid's algorithm is very efficient, implementable to run in time where is the number of digits of the input. The Euclidean algorithm is a great showcase of an intimate connection between algorithms and mathematics from more than 2000 years ago (Euclid seemed to think like a computer scientist when coming up with the gcd algorithm ☺️).
Prove that .
The Fibonacci number is given by where and . Prove that for all .
We say that a sequence is eventually periodic with period if there exists such that for all , we have . In other words, the sequence can be seen as infinite copies of a finite sequence. Prove: For every , the Fibonacci sequence modulo is eventually periodic. The period is at most .
Define the sequence and . Prove that, for every integer , the sequence modulo is eventually periodic.
Let be the number of positive divisors of , e.g., . Prove your answers on the following questions:
- For what values of do we have ?
- For what values of do we have ?