# Documentation

Mathlib.Data.PNat.Xgcd

# Euclidean algorithm for ℕ #

This file sets up a version of the Euclidean algorithm that only works with natural numbers. Given 0 < a, b, it computes the unique (w, x, y, z, d) such that the following identities hold:

• a = (w + x) d
• b = (y + z) d
• w * z = x * y + 1 d is then the gcd of a and b, and a' := a / d = w + x and b' := b / d = y + z are coprime.

This story is closely related to the structure of SL₂(ℕ) (as a free monoid on two generators) and the theory of continued fractions.

## Main declarations #

• XgcdType: Helper type in defining the gcd. Encapsulates (wp, x, y, zp, ap, bp). where wp zp, ap, bp are the variables getting changed through the algorithm.
• IsSpecial: States wp * zp = x * y + 1
• IsReduced: States ap = a ∧ bp = b∧ bp = b

## Notes #

See Nat.Xgcd for a very similar algorithm allowing values in ℤ.

structure PNat.XgcdType :
• wp is a variable which changes through the algorithm.

wp :
• x satisfies a / d = w + x at the final step.

x :
• y satisfies b / d = z + y at the final step.

y :
• zp is a variable which changes through the algorithm.

zp :
• ap is a variable which changes through the algorithm.

ap :
• bp is a variable which changes through the algorithm.

bp :

A term of XgcdType is a system of six naturals. They should be thought of as representing the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] together with the vector [a, b] = [ap + 1, bp + 1].

Instances For
Equations
Equations

The Repr instance converts terms to strings in a way that reflects the matrix/vector interpretation as above.

Equations
• One or more equations did not get rendered due to their size.
def PNat.XgcdType.mk' (w : ℕ+) (x : ) (y : ) (z : ℕ+) (a : ℕ+) (b : ℕ+) :

Another mk using ℕ and ℕ+

Equations

w = wp + 1

Equations

z = zp + 1

Equations

a = ap + 1

Equations

b = bp + 1

Equations

r = a % b: remainder

Equations
• = (u.ap + 1) % (u.bp + 1)

q = ap / bp: quotient

Equations
• = (u.ap + 1) / (u.bp + 1)

qp = q - 1

Equations

The map v gives the product of the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] and the vector [a, b] = [ap + 1, bp + 1]. The map vp gives [sp, tp] such that v = [sp + 1, tp + 1].

Equations
• = (u.wp + u.x + u.ap + u.wp * u.ap + u.x * u.bp, u.y + u.zp + u.bp + u.y * u.ap + u.zp * u.bp)

v = [sp + 1, tp + 1], check vp

Equations
• = (↑() * ↑() + u.x * ↑(), u.y * ↑() + ↑() * ↑())

succ₂ [t.1, t.2] = [t.1.succ, t.2.succ]

Equations

IsSpecial holds if the matrix has determinant one.

Equations
• = (u.wp + u.zp + u.wp * u.zp = u.x * u.y)

IsSpecial' is an alternative of IsSpecial.

Equations

IsReduced holds if the two entries in the vector are the same. The reduction algorithm will produce a system with this property, whose product vector is the same as for the original system.

Equations
• = (u.ap = u.bp)

IsReduced' is an alternative of IsReduced.

Equations

flip flips the placement of variables during the algorithm.

Equations
• = { wp := u.zp, x := u.y, y := u.x, zp := u.wp, ap := u.bp, bp := u.ap }
@[simp]
theorem PNat.XgcdType.flip_x (u : PNat.XgcdType) :
().x = u.y
@[simp]
theorem PNat.XgcdType.flip_y (u : PNat.XgcdType) :
().y = u.x
theorem PNat.XgcdType.rq_eq (u : PNat.XgcdType) :
+ (u.bp + 1) * = u.ap + 1

Properties of division with remainder for a / b.

The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying IsReduced. The gcd can be read off from this final system.

Equations
• = { wp := 0, x := 0, y := 0, zp := 0, ap := a - 1, bp := b - 1 }
theorem PNat.XgcdType.start_v (a : ℕ+) (b : ℕ+) :
= (a, b)

finish happens when the reducing process ends.

Equations
• = { wp := u.wp, x := (u.wp + 1) * + u.x, y := u.y, zp := u.y * + u.zp, ap := u.bp, bp := u.bp }

This is the main reduction step, which is used when u.r ≠ 0, or equivalently b does not divide a.

Equations
• = { wp := u.y * + u.zp, x := u.y, y := (u.wp + 1) * + u.x, zp := u.wp, ap := u.bp, bp := }
theorem PNat.XgcdType.step_wf (u : PNat.XgcdType) (hr : ) :

We will apply the above step recursively. The following result is used to ensure that the process terminates.

theorem PNat.XgcdType.step_v (u : PNat.XgcdType) (hr : ) :

The reduction step does not change the product vector.

We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below.

Equations
• = if x : then else
def PNat.xgcd (a : ℕ+) (b : ℕ+) :

Extended Euclidean algorithm

Equations
def PNat.gcdD (a : ℕ+) (b : ℕ+) :

gcdD a b = gcd a b

Equations
def PNat.gcdW (a : ℕ+) (b : ℕ+) :

Final value of w

Equations
def PNat.gcdX (a : ℕ+) (b : ℕ+) :

Final value of x

Equations
• = ().x
def PNat.gcdY (a : ℕ+) (b : ℕ+) :

Final value of y

Equations
• = ().y
def PNat.gcdZ (a : ℕ+) (b : ℕ+) :

Final value of z

Equations
def PNat.gcdA' (a : ℕ+) (b : ℕ+) :

Final value of a / d

Equations
def PNat.gcdB' (a : ℕ+) (b : ℕ+) :

Final value of b / d

Equations
theorem PNat.gcdA'_coe (a : ℕ+) (b : ℕ+) :
↑() = ↑() +
theorem PNat.gcdB'_coe (a : ℕ+) (b : ℕ+) :
↑() = + ↑()
theorem PNat.gcd_props (a : ℕ+) (b : ℕ+) :
let d := ; let w := ; let x := ; let y := ; let z := ; let a' := ; let b' := ; w * z = Nat.succPNat (x * y) a = a' * d b = b' * d z * a' = Nat.succPNat (x * b') w * b' = Nat.succPNat (y * a') z * a = x * b + d w * b = y * a + d
theorem PNat.gcd_eq (a : ℕ+) (b : ℕ+) :
= PNat.gcd a b
theorem PNat.gcd_det_eq (a : ℕ+) (b : ℕ+) :
theorem PNat.gcd_a_eq (a : ℕ+) (b : ℕ+) :
a = * PNat.gcd a b
theorem PNat.gcd_b_eq (a : ℕ+) (b : ℕ+) :
b = * PNat.gcd a b
theorem PNat.gcd_rel_left' (a : ℕ+) (b : ℕ+) :
* = Nat.succPNat ( * ↑())
theorem PNat.gcd_rel_right' (a : ℕ+) (b : ℕ+) :
* = Nat.succPNat ( * ↑())
theorem PNat.gcd_rel_left (a : ℕ+) (b : ℕ+) :
↑() * a = * b + ↑(PNat.gcd a b)
theorem PNat.gcd_rel_right (a : ℕ+) (b : ℕ+) :
↑() * b = * a + ↑(PNat.gcd a b)