Modular Arithmetic
Ths chapter covers notes on modular arithmetic.
Remark: In the notes below, we often use the term "division" for ease of communication alongside writing expressions like This is an abuse of notation and language. When we use the word division, we mean the following:
division. Let Then is defined if and only if there exists a such that with
We state this definition because at a formal level, the operation of division that we usually think of does not exist in the world of integers. The standard definition of division is There is no integer that satisfies so there's no such thing as "division" in the usual sense. In fact, the very lack of division is why we have a seperate set of numbers, called the rationals.
Divisors
We begin this section by defining a few special sets.
divisors. Let Then the poset denotes the set of all divisors of
Example.
Example.
Following the definition above, all sets of divisors are non-empty. They will always contain the integer and itself.
common divisors. Let Given the sets of divisors and then the set is the set of all common divisors of and Given an integer we say that is a common divisor of and
example.
Since all sets of divisors contain the integer all intersections of divisor sets are non-empty β they all contain the integer There is, however, a special case where the intersection consists of only
coprimality. Let If then we say that and are coprime or relatively prime.
Example. The integers and are coprime, since and
We're often interested in the largest element of a common divisor set, called the greatest common divisor.
greatest common divisor. Let and If is the maximum element of then is the greatest common divisor of and and we write
Example.
Example.
Multiples
multiples. Let be an integer and . Then the set denotes the set of multiples of If then we say that is a multiple of
Example.
Example.
Example.
From the definition of a multiple, we can determine that the sum of any two multiples of is also a multiple of Suppose and are integers, and and are multiples of It follows that:
Since addition is closed on the integers, it follows that is an integer. Thus, The same idea extends to subtraction: The difference between any two multiples of is also a multiple of Given the -multiples and it follows that And since subtraction is closed on the integers, the term is an integer. Thus, is also a multiple of
This leads to an alternative reading of the relation When we write we're effectively implying that is a multiple of Thus, another way of viewing the "divides" relation is that it's the "multiple of" relation.
" is a multiple of "
We thus have the following lemma:
lemma. Given if and then and
It's helpful to know an alternative framing of the lemma above. Suppose and and Then following our previous lemma. Thus, we have:
lemma. Given if and then
This leads us to the notion of common multiples:
common multiples. Let and be integers, the set of all multiples of and the set of all multiples of Then denotes the set of all common multiples of and Given an integer we call a common multiple of and
Example.
There's a particularly special multiple when we work with positive multiples. Because the positive integers are just the natural numbers without they have the special property of being well-ordered. That is, there's always a least element in any set of positive integers. This means that, the intersection where and are positive integers always contains a least element called the least common multiple.
least common multiple. Let and the sets and the denoting the positive multiples of and respectively. Then the least element of is called the least common multiple of and which we denote as
Example. since
The link between multiples and common divisors is encapsulated in the following theorem.
euclid's theorem. For all with it follows that
The proof is fairly straightfoward. Given with if and then Similarly, if and then These are merely the two lemmas we proved earlier. If it were instead the case that and but was not a common divisor of both and then it must be the case that is not a divisor of But this would contradict the lemma we just proved. If it were instead the case that and but and then it must be the case that is not a divisor of But this would contradict another lemma we just proved β that Thus, it must be the case that set of all the common divisors of and and the common divisors of and are the exact same sets. And since they're the exact same sets, it follows that their maximum element is also the same.
The theorem above is a particularly useful theorem because it's almost always the case that finding the greatest common divisor of two integers and is much easier for smaller integers than it is for larger integers. This is particularly true for computers; they can only handle integers so large.
Remainders
Consider the following equations:
If and what are the pairs of that satisfy the equations respectively? Well, let's lay them out:
equation | |||
---|---|---|---|
Question: Given and is there an integer such that no pair satisfies the equation Well, let's think about the integer for a moment. If then That is, is a multiple of We can think of a multiple of some integer as long-jumping along the integer number line by places, where is the multiple. In this case, means that there are certain places that we land on by default:
-6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | |
If we want to land on an integer other outside of that is, an integer that is not a multiple of then we must add either or For example, to go from to must add To go from to must add
If we think of addition as moving along the number line, this phenomenon is intuitive. If we start at the integer and want to get to the integer we take steps, where each step is one.
Thus, to get from point to a destination we perform to get to where is the number of steps. That is, If we start at a point then that means we took steps from a total of times. And if we start from and want to get to a destination then we have If we rearrange this equation, we get We can read this as "Take steps back to return to " And since it must be the case that And since stands for the number of steps, it's a nonnegative quantity, so we can be even more specific: As it turns out, this is leads to an extremely important theorem in number theory called the Division Theorem.
division theorem. Let Then there is exactly one pair of integers with such that Given we call the dividend, the divisor, the quotient, and the remainder.
The division theorem leads to a natural extension of a theorem we've seen before.
euclid's theorem ii. Let with such that Then
Floors and Ceilings
We begin by defining the floors and ceilings of real numbers. The definitions below are presented formally to ensure we aren't sweeping anything under the rug or hiding the ball. For our purposes, the floor function returns the closest integer to a real number that is less than Its sibling is the ceiling function, which returns the closest integer to a real number that is greater than
floor. Given a real number there exists a function whose image under is an integer satisfying the relation We call the floor of denoted and the floor function.
Example.
Example.
Example.
Example. (Remember that the floor returns the closest integer less than its argument).
ceiling. Let be a real number. Then there exists a function whose image under is an integer satisfying the relation We call the ceiling of denoted and the ceiling function.
Example.
Example.
Example.
Example. (The ceiling function returns the closest integer greater than its argument).
Remainders and Quotients
Having defined the floor and ceiling functions, we can now define the remainder operation. Informally, the remainder operation returns the remainder after division.
remainder operation. Let and Then
Example.
Example.
Example.
The remainder operation's partner is the integer division operation. Informally, it returns the integer quotient after division.
integer division. Let and Then
As an aside, many programming languages (e.g., Python) use %
for the remainder operation and //
for integer division. These operations behave differently from the mathematical definitions. Some languages, for example, may return a negative result given some expression a % b
where a
and b
are integers. This stems from two rules that many computer architects and software engineers want:
(1)
(2)
The first rule is sensible. It's exactly how normal division works. The second rule, however, is suspect. As we saw, the floor should return the closest integer to a real number that is less than Thus, the expression is most certainly a very different expression from So if the second rule is mathematically silly (some mathematicians, rather unkindly, go so far as to call it "dirty"), why is its implementation incentivized? Because the vast majority of programmers β and arguably, the vast majority of integer division applications β do not use integer division the way its mathematically defined. If a language did use the mathematically correct definition, then the line of code
must be false.
There are two significant problems with this result from an engineering standpoint. First, it's a breeding ground for bugs. Anyone who's had experience designing products β e.g., a programming language β knows that the first rule of design is to never make the user think. This nuance to parentheses and integer division won't just make the user think; it may very well make them smash their keyboards. Those who leave their keyboards intact will either move to another language or try to "fix" the language with various hacks and publishing them β the designer's worst nightmare.
The second major problem is that implementing the mathematically correct definition requires a separate parsing rule for parentheses and integer division. That incurs a time and space cost for likely very little reward β most users do not expect a rule where is a binary operator on a number type. This also means that the designers have to decide how they'd like to parse Is it or All of this translates to time, money, and labor; things that we don't have to worry about with mathematical definitions.
Congruence
Suppose we had the following, somewhat strange, clock. The cock only has one hand, and it moves around the clock from to then from to then from to and finally from to Assume it moves only in that order.
If the clock ticks by 1, then the hand lands on 1. If the clock ticks by 2, then the hand lands on 2. Let's take a look at some of the patterns:
Here's another way to look at the pattern:
The first row, separated from all the others, corresponds to a number on the clock. The integers beneath each clock number correspond to the number of ticks. Thus, at 5 clicks, we land on 0. At 19 clicks, we land on 4. At 46 clicks, we land on 1. Counting with only is called counting modulo 5. It's an example of a broader notion called arithmetic modulo-, where is the set with In general, we call the set the integers modulo-, and we call the modulus. We aren't limited to just counting forwards β we can count backwards as well:
Given clock numbers, is there a way to determine which clock number we land on after ticks? Yes: For example, for our sample clock, there are clock numbers. After ticks, we land on which is what our pattern indicates. At this point, we can now introduction some notation.
congruence. Let and If, and only if, we say that is congruent to modulo and write
We call the proposition a congruence and call its modulus. If and are not congruent modulo , then we write
Let's iron out some wrinkles with this definition. First, the notation
is akin to saying " is equivalent to ... -wise, that is." The is like an adjective. "Pope Francis and Marilyn Manson are equivalent ... human-wise, that is." A more consistent notation for the same idea would've been
The symbol is another symbol for the remainder operation, which, in these materials, we denoted We avoid using for the remainder operation because it causes unnecessary confusion. However, now that we know that is just another symbol for the remainder operation, we can conjecture as to why the notation is the way it is. It's not too difficult to imagine a lecturer writing
on a chalkboard, then, just to make sure things are clear, writing
This would also explain why there's that irregular spacing (again, just conjecture).
Second, in the expression the notation denotes a set of integers. This is another reason why we avoid using for the remainder operation.