Arithmetic Modulo

Division

If aa and bb are integers with a,/=0a \mathrlap{\\,/}{=} 0 then aa divides bb, written a∣ba|b, if there exists an integer cc such that b=acb = ac. Following this base theorem, we can extrapolate the following facts:

  • If a∣ba|b and a∣ca|c then a∣(b+c)a|(b+c)
  • If a∣ba|b then a∣bca|bc
  • If a∣ba|b and b∣cb|c then a∣ca|c

The algorithm

If aa is an integer and dd is a positive integer, then there are are unique integers qq and rr with 0≤r<d0 \le r \lt d such that:

a=dq+ra = dq + r

Where dd is called the divisor, qq is called the quotient and rr is called the remainder.

Modulo relation

If aa and bb are integers and mm is a positive integer, then aa is congruent to bb modulo mm. This can also be written as a≡b,( mod ,m)a \equiv b \\, (\bmod \\, m), iff ,m∣(a−b)\\, m | (a−b).

Congruence is an equivalence relation on integers.

Another rule is a≡b,( mod ,m)a \equiv b \\, (\bmod \\, m) iff there is an integer kk such that a=b+kma = b + km.

If a≡b,( mod ,m)a \equiv b \\, (\bmod \\, m) and c≡d,( mod ,m)c \equiv d \\, (\bmod \\, m) then a+c≡b+d,( mod ,m)a + c \equiv b + d \\, (\bmod \\, m) and ac≡bd,( mod ,m)ac \equiv bd \\, (\bmod \\, m).

  • (a+b), mod ,m=((a mod m)+(b mod m)) mod m(a + b) \\, \bmod \\, m = ((a \bmod m) + (b \bmod m)) \bmod m
  • ab mod m=((a mod m)(b mod m)) mod mab \bmod m = ((a \bmod m) (b \bmod m)) \bmod m

Primes

A positive integer p>1p \gt 1 is called prime iff the only positive factors of pp are 11 and pp. Otherwise, it is called composite.

Every positive integer greater than 11 can be written uniquely as a prime or as the product of its prime factors, written in order of nondecreasing size. The uniqueness means that every positive integer has only one prime decomposition.

There are infinitely many primes, as can be proved by contradiction of the statement above.

Greatest common divisor

Let a,b∈Z+a,b \in \Bbb{Z}^+. The largest integer dd such that d∣ad|a and d∣bd|b is called the greatest common divisor of aa and bb, written as gcd(a,b)\text{gcd}(a,b).

The integers aa and bb are coprime if gcd(a,b)=1\text{gcd}(a,b) = 1. Meaning they have no positive integer common factors other than 11.

Prime factorisation

Suppose that the prime factorisation of aa and bb are:

a=p1a1p2a2,...,pnana = {p_1}^{a_1} {p_2}^{a_2} \\, ... \\, {p_n}^{a_n}

b=p1b1p2b2,...,pnbnb = {p_1}^{b_1} {p_2}^{b_2} \\, ... \\, {p_n}^{b_n}

Where each exponent is a nonnegative integer. Then:

gcd(a,b)=p1min(a1,b1)p2min(a2,b2),...,pnmin(an,bn)\text{gcd}(a,b) = {p_1}^{\text{min}(a_1,b_1)} {p_2}^{\text{min}(a_2,b_2)} \\, ... \\, {p_n}^{\text{min}(a_n,b_n)}

This number would clearly divide aa and bb. No larger number can divide aa and bb. Factorisation is a very inefficient method since the factorisations must be computed.

Euclidean algorithm

This is another method of computing the gcd which uses recursion.

gcd(x,y):
 if y = 0
    return (x)
 else
    return gcd(y,(x mod y))

This logic uses facts covered earlier in this topic to draw together if x=qy+rx = qy + r and r=xmod  yr = x \mod y where xx and yy are integers. Then it follows that:

gcd(x,y)=gcd(y,r)\text{gcd}(x,y) = \text{gcd}(y,r)

Linear combinations

If xx and yy are positive integers, then there exist integers aa and bb such that gcd(x,y)=ax+by\text{gcd}(x,y) = ax + by. This is called Bézout’s theorem.

This can be proved using the extended Euclidean algorithm.

e-gcd(x,y):
    if y = 0
        return (x,1,0)
    else
        (d,a,b) = e-gcd(y,(x mod y))
        return ((d,b,a - ((x div y) * b)))