Euclidean Division
Let and be two integers with . Then there is always another integer and a natural number , with such that the following holds:
This decomposition of a given is called Euclidean Division, where is called the dividend, is called the divisor, is called the quotient and is called the remainder. It can be shown that both the quotient and the remainder always exist and are unique, as long as the divisor is different from 0.
Notation: Suppose that the numbers and satisfy above equation. We can then use the following notation for the quotient and the remainder of the Euclidean Division as follows:
We also say that an integer is divisible by another integer if holds. In this case, we write , and call the integer the cofactor of in .
The Euclidean Algorithm
The greatest common divisor of two non-zero integers and is defined as the largest non-zero natural number such that divides both and , that is, as well as are true. We use the notation for this number. Since the natural number 1 divides any other integer, 1 is always a common divisor of any two non-zero integers, but it is not necessarily the greatest.
A common method for computing the greatest common divisor is the so-called Euclidean Algorithm:
def gcd(a, b):
(r, rr) = (a, b)
while r % rr != 0:
(r, rr) = (rr, r % rr)
return rr
Sage example:
a.gcd(b)
Complexity:
The Extended Euclidean Algorithm
The Extended Euclidean Algorithm is a method for calculating the greatest common divisor of two natural numbers and , as well as two additional integers , such that the following equation holds:
def xgcd(a, b):
(r, rr) = (a, b)
(s, ss) = (1, 0)
(t, tt) = (0, 1)
while r % rr != 0:
q = r // rr
(r, rr) = (rr, r - q * rr)
(s, ss) = (ss, s - q * ss)
(t, tt) = (tt, t - q * tt)
return (rr, ss, tt)
Sage example:
a.xgcd(b)
Complexity:
Last updated