Euclid
O(b⋅b3), improvable to quadratic.
Correctness proof sketch
show that both sides of the following equation are divisible by the other side
gcd(a,b)=gcd(b,a%b)
can use the fact that a%b=a−b⋅floor(a/b)
and also that when x∣p and x∣q then x∣gcd(p,q).
We should know Bezeout coefficients and how to compute them.
More on it here.
Extension of the Euclid algorithm can compute inverses.
Euler’s Theorem
"Intuitively it’s clear, less intuitively it’s the Lagrange’s Theorem" — Martin Mareš
We want to prove that:
∀N:∀x coprime to N:xφ(N)=1(mod N)
Monsieur Lagrange said that for a finite group G and H its subgroup
the size of H divides the size of G.
Now let’s have the following sequence:
x0,x1,…,xk−1,1
we are in ZN∗ so surely after some number of
steps some number repeats.
The first repeated number is one that comes from the following
xi=xj
xj−i=1
now the sequence forms a subgroup of ZN∗ thus
k∣φ(N)
φ(N)=k⋅l
from here it should be straightforward.
Chinese Remainder Theorem
Let N1,N2,...,Nk be coprime then the following system of equations
xi=aimodNi
has exactly one unique solution in ZN, where N is the product of all Nis.
A different view on this is that there is a function
f:ZN→XiZNi
this function is a ring isomorphism.
Proof 1
We will show that the function is really a bijection.
injective
Suppose f(x)=f(y), then from the linearity
f(x−y)=0.
Thus ∀Ni:x−y∣Ni and x−y∣N and because x,y∈ZN
we have that x−y=0.
f is a homomorphism between two sets with the same cardinality
and because it is injective it is surely also surjective and therefore bijective.
Proof 2 constructive (for two equations)
Suppose we have f(u1)=(1,0), f(u2)=(0,1)
then f(a1u1+a2u2modN)=a1f(u1)+a2f(u2)=(a1,a2).
But how to find u?
Let f(N2)=(z,0). If z=1 we must put in little bit more work.
But all we need to do is notice that we can take a multiplicative inverse of
zmodN1 because z is surely coprime to it,
from this we get that u1=N2⋅z−1, similarly for u2.
Proof 3 (similar to proof 2)
There surely exists N1−1modN2,N2−1modN1 because
Nis are coprime.
Then we claim that the following solves the system
y=a1N2N2−1+a2N1N1−1modN1N2
Why? Let’s just show it only for x1:
ymodN1=a1+0
The last thing we need to do is show that this solution is unique.
But this can be shown in the way that was presented in the first proof.
Calculating φ(N)
φ(p)=p−1
φ(pk)=(1−1/p)pk=pk−1(p−1)
φ(xy)=φ(x)φ(y)
for coprime x,y because
Zxy→Zx×Zy
we count in the first and then in the second.
Interestingly for not coprime x,y
φ(xy)=φ(x)φ(y)φ(gcd(x,y))gcd(x,y)