Note: \(a \mid b\) doesn’t mean that \(\frac{b}{a}\) is defined. Also note that \(a \mid 0\) is only true if \(a = 0\).
Example
Show that \(6 \ | \ n(n+1)(n+2)\).
Proof: One way to see this is to observe that \(n(n+1)\) is an even number since if \(n\) is odd, then \(n+1\) is even so the product is even. Otherwise \(n\) is even so the product is even again. Therefore, \(n(n+1)\) is divisible by 2. Also observe that \(n(n+1)(n+2)\) must be divisible by 3. This is because say \(n\) leaves a remainder of 1 when divided by 3, then \(n+2\) is divisible by 3. If \(n\) leaves a remainder of 2, then \(n+1\) is divisible by 3. So in a set of 3 consecutive integers, we will always have one of them divisible by 3. So now 2 and 3 are coprime so the product must be divisible by 6. (This fact isn’t obvious and needs a proof)
Another way to show this to recognize that this product is the binomial coefficient
This is obviously an integer so \(n(n+1)(n+2)\) must be divisible by 6.
Example
Show that \(8 \ | \ n^2 - 1\) if \(n\) is odd.
Proof: Since \(n\) is odd, then we can write \(n = 2k+1\) for some integer \(k\). So now we can expand \(n^2 - 1\) to see that
Recall that \(k(k+1)\) is even. So it is divisible by 2. Therefore, we can re-write the previous expression as
From this we see that \(n^2 - 1\) is divisible by 8.
Euclid's Divison Algorithm
Proof: TODO (number line)
Ideals in \(\mathbb{Z}\)
Given some \(d \in \mathbb{Z}\), the set of all \(x\) such that \(d \mid x\) forms an ideal in \(\mathbb{Z}\) that is closed under addition and subtraction. Conversely, given an ideal \(I\) in \(\mathbb{Z}\), then \(I\) is the set of multiples of some number \(d \geq 0\).
To show this, we can use Euclid’s division algorithm. So suppose \(I\) is an ideal, then either \(I = \{0\}\) and we’re done or \(I\) contains an element \(a > 0\). Pick the smallest positive element \(a\) in \(I\) (By the well ordering principle, this exists). We will show that \(I\) is generated by \(a\) so \(I = (a)\). To see this, let \(b\) be an element in \(I\). Apply Euclid’s division algorithm to write \(b = qa + r\) where \(q, r \in \mathbb{Z}\) and \(0 < r \leq a\). Write \(r = b - qa\). Observe that \(qa \in I\) since \(a \in I\) and any multiple of \(a\) must be in \(I\). We also know that \(b \in I\). Therefore, \(r\) must be in \(I\) since \(I\) is closed under subtraction. But this is a contradiction since we assumed that \(a\) is the smallest positive element in \(I\) and \(r < a\). Therefore, we must have \(r = 0\). So \(b = qa\) which means that \(b\) is a multiple of \(a\). So \(I = (a)\) as we wanted to show. \(\ \blacksquare\)
The Greatest Common Divisor
Unlike what we learned in Math417, the greatest common divisor here is defined as the largest integer \(d\) such that \(d \mid a\) and \(d \mid b\). Though the professor does note that when \(a = b = 0\), then we define \(gcd(a,b)=0\) and here the gcd is not the largest. Also note that \(gcd(a,0)=a\) for any \(a\).
How do we find the greatest common divisor of two integers \(a\) and \(b\)? the naive way is to check all the numbers from \(1\) to \(a\) and see if any divides \(b\), then pick the largest one. This method is obviously too slow.
A better method is factoring both \(a\) and \(b\) into primes and then picking the all the common primes. So if \(a = 24 = 2^3(3)\) and \(b = 180 = 2^2(3^2)5\). Then the greatest common divisor is \(2^2(3) = 12\). The problem with this method is factoring large numbers is a difficult problem.
Euclid's Algorithm
The fastest way to compute the greatest common divisor is Euclid’s Algorithm. Suppose we’re trying to find the greatest common divisor of 78 and 14. Then we use division with remainder where in each iteration, we divide again the quotient by the remainder from the previous step. We stop the algorithm when the last remainder is 0. So
The greatest common divisor in this case is 2. This works because \((78,14) = (14,8) = (8,6) = (6, 2) = (2, 0) = 2\). Moreover, this algorithm terminates since the remainders are decreasing positive integers \(8, 6, 2, 0\). Generally, given integers \(a\) and \(b\)
The greatest common divisor will be \(r_n\). What is the worst case? The worst case happens when all the quotients in every step are 1. For example take the integers 13 and 8 and observe that
The greatest common divisor is 1 and you can see that \(q=1\) in every step. To find out the number of steps, notice here that each remainder is the sum of the next two remainders. So \(r_i = r_{i+1} + r_{i+2}\). And in the worst case specifically, notice that if we work backward, then we get the sequence
This is the fibonacci sequence and this happens in the worst case for Euclid’s algorithm. To get a sense of how fast this sequence growth notice that
Sequence | 0 | 1 | 1 | 2 | 3 | 5 | 8 | \(\cdots\) |
Upper Bound | 0 | 1 | 2 | 4 | 8 | 16 | 32 | \(\cdots\) |
Lower Bound | 0 | 1 | 1 | 2 | 2 | 4 | 4 | \(\cdots\) |
From this we see that \(F_n\) is bounded below and above by
Again
- \(n\) is the number of steps in Euclid's algorithm.
- \(n\) is also the index of the \(n\)th term Fibonacci sequence.
We’re interested in a bound on \(n\), so we want a bound on the \(n\)th fibonacci number. Using the above inequality, we see that
So \(n\) is at most \(c\log(F_n)\) where \(c\) is a constant. Therefore, in the worst case, the number of steps in Euclid’s algorithm is bounded above by a constant times the number of digits in that \(n\)th Fibonacci number. For example, if one of the numbers has a 1000 digits, then Euclid’s algorithm will take at most 1000 iterations (times a constant) in the worst case.
Solving \(ax + by = d\)
We can use also Euclid’s algorithm to solve equations like 71x + 17y = 1. The first thing we want to do is to find the gcd of 71 and 17 using Euclid’s algorithm. So
The greatest common divisor in this case is 1. So now we work backwards. Starting from the greatest common divisor 1, we look at the previous equation and we write 1 in terms of 3 and 2 to get
Next, we write 2 in terms of the previous equation
Now, we look at the equation before that (the first equation) and write 3 above in terms of it
Therefore, the solution is \(x = 6\) and \(y = -25\).
Suppose now that we want to solve \(72x + 26y = 1\). We should immediately notice that since both 72 and 26 are even, then there can’t be a solution satisfying this. In general, if \(ax + by = c\) is solvable and we have that \(d \mid a\) and \(d \mid b\), then we must have \(d \mid c\) as well. In particular, \(c\) must be divisible by the greatest common divisor of \(a\) and \(b\). Conversely, if \((a,b) \ | \ c\), then the equation is solvable. This is because we know that the greatest common divisor can be written as a linear combination of \(a\) and \(b\).
Linear Equations in 3 Variables
What about solving a linear equation in 3 variables. For example
We first solve the following equation using Euclid’s method
The solution for this is \(x = 2\) and \(y = -1\) so \(6 \cdot 2 - 1 \cdot 10 = 2\). Next, we solve
The solution for this is \(x = -7\) and \(y = 1\). At this point, we can substitute the solution from the first equation into the solution we have
In general, \(ax + by + cz = n\) has a solution if and only if \((a,b,c) \mid n\). This method also works for equations in \(n\) variables.
Alternative to Long Division
We still have an issue with Euclid’s algorithm because long division is slow and complicated when the numbers have hundreds of digits. So a better algorithm is to avoid long division. Instead, we do the following to find the greatest common divisor of \(a\) and \(b\)
- Take out all factors of 2 from \(a\) and \(b\). This is easy to do since it's a simple shift operator. So now both \(a\) and \(b\) are odd. Also we can assume that \(a > b\), otherwise swap them.
- Change \(a\) to \(a - b\). If the result \(a - b\), we stop. Otherwise notice here that \(a\) is even so it at least have a factor of \(2\).
- Take all factors of 2 from \(a\). Go back to step 2.
Since we keep dividing by 2, this algorithm is still fast and the number of steps is approximately \(c\log(a)\) where \(c\) is some constant.
Example
Suppose we want to find the greatest common divisor of 68 and 142. Then we take out the factors of 2 out. So 68 becomes 34 and then 17. While 141 becomes 71. Notice how there is a common factor of 2 between the two numbers. Next, we find the greatest common divisor of 71 and 17 using the subtraction method
- Subtract \(71 - 17 = 54\). The result is not zero so we continue.
- Take all factors of 2 from \(54\). So 54 becomes 27.
- We repeat the subtraction step so \(27 - 17 = 10\). The result is not zero so we continue by taking 2 out of 10 to get 5.
- We subtract again, \(17 - 5 = 12\). Take the 2 factor out twice to get 3
- We subtract \(5 - 3 = 2\). Take the factor 2 out to get 1.
- We subtract \(3 - 1 = 2\), Take the factor 2 out to get 1.
- Finally \(1 - 1 = 0\) so. we stop.
So the greatest common of 17 and 71 is 1. The greatest common divisor of 68 and 142 is therefore \(2 \cdot 1 = 2\).
The Least Common Multiple
The least common multiple of two numbers \(a\) and \(b\) is defined to be the smallest multiple of \(a\) and \(b\). So this means it will be at most \(ab\). In fact,