Euclidean Algorithm
| Resources | |||||
|---|---|---|---|---|---|
| cp-algo | |||||
Suppose we have non-negative integers and . The original Euclidean Algorithm computes and looks like this:
def euclid(a: int, b: int) -> int:assert (int(a) > 0 and int(b) > 0), "Arguments must be positive, non-zero numeric values."while b > 0:k = a // b# subtract multiples of one equation from the other.a -= b * ka, b = b, areturn a
Extended Euclidean Algorithm
| Resources | |||||
|---|---|---|---|---|---|
| cp-algo | |||||
| Wikipedia | |||||
The extended Euclidean algorithm computes integers and such that
We can slightly modify the version of the Euclidean algorithm given above to return more information!
def extend_euclid(a: int, b: int) -> list[int]:assert (int(a) > 0 and int(b) > 0), "Arguments must be positive, non-zero numeric values."# we know that 1 * a + 0 * b = a and 0 * a + 1 * b = b.x_arr = [1, 0, int(a)]y_arr = [0, 1, int(b)]q = -1
Recursive Version
def euclid(a: int, b: int) -> int:"""Recursive Euclidean GCD."""return a if b == 0 else euclid(b, a % b)
becomes
def extend_euclid(a: int, b: int) -> list[int]:if not b:return [1, 0]p = extend_euclid(b, a % b)return [p[1], p[0] - (a // b) * p[1]]
The pair will equal the first two returned elements of the array in the iterative version. Looking at this version, we can prove by induction that when and are distinct positive integers, the returned pair will satisfy and . Furthermore, there can only exist one pair that satisfies these conditions!
Note that if we do all calculations in 64-bit integers, we won't wind up with overflow issues even when are quite large (say, ).
Application - Modular Inverse
| Resources | |||||
|---|---|---|---|---|---|
| cp-algo | |||||
Focus Problem – try your best to solve this problem before continuing!
Additionally, it seems that when multiplication / division is involved in this problem, .
Solution
Hint
Solution
Application - Chinese Remainder Theorem
| Resources | |||||
|---|---|---|---|---|---|
| Stanford | In-depth explanation | ||||
| cp-algo | |||||
| CMU | |||||
The Chinese Remainder Theorem (CRT) states that there is a unique solution to a system of simultaneous modular congruences with pairwise coprime moduli. Mathematically, suppose we are given pairwise coprime positive integers , , and the system
If we let
then there is exactly one solution for modulo .
It suffices to solve only the case , because for greater we can just combine pairs of modular congruences until only one remains.
Focus Problem – try your best to solve this problem before continuing!
Solution
Hint
Solution
Application - Linear Diophantine Equation
| Resources | |||||
|---|---|---|---|---|---|
| Brilliant | In-depth explanation | ||||
| cp-algo | |||||
A Linear Diophantine Equation is an equation of the form
where , and are known constants, and we want to find integer values of and satisfying the equation. Each number in the equation has to be an integer.
We can solve a Linear Diophantine Equation using the extended Euclidean algorithm, which calculates values and that satisfy the equation:
A Linear Diophantine Equation can be solved if is divisible by , and otherwise it cannot be solved. This observation is derived from Bézout's Identity.
Let's calculate the value of and assuming divides . Since we can multiply the Bézout equation through by , resulting in:
So one solution to is .
A solution to the Diophantine equation is never unique, because we can form an infinite number of solutions if we know one solution.
Theorem: If is an integer solution of the Diophantine Equation , then all integer solutions to the equation are of the form
where is any integer.
Proof: We have
here note that the two -terms cancel each other out, so no matter what integer we choose, our equation results back in .
But we still need to check that we haven't missed any other solutions. So suppose is some solution to the equation — any solution at all. Since it agrees with on the right-hand side, we can subtract one from the other:
Here, note that the numbers and are coprime (i.e., they have no factors left in common except ). That means has no choice but to divide evenly into . In other words, must be some whole multiple of ; we call that multiple . Once we have fixed the value of , the equation above tells us has to equal , proving the theorem.
Problems
This section is not complete.
TODO: more problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| AC | Medium | Show TagsModular Arithmetic | ||||
| Kattis | Medium | Show TagsModular Arithmetic | ||||