Library Coq.ZArith.Znumtheory
For compatibility reasons, this Open Scope isn't local as it should
Open Scope Z_scope.
This file contains some notions of number theory upon Z numbers:
- a divisibility predicate Z.divide
- a gcd predicate gcd
- Euclid algorithm euclid
- a relatively prime predicate rel_prime
- a prime predicate prime
- properties of the efficient Z.gcd function
The former specialized inductive predicate Z.divide is now
a generic existential predicate.
Its former constructor is now a pseudo-constructor.
Results concerning divisibility
Auxiliary result.
Only 1 and -1 divide 1.
If a divides b and b divides a then a is b or -b.
If a divides b and b<>0 then |a| <= |b|.
Z.divide can be expressed using Z.modulo.
Z.divide is hence decidable
Greatest common divisor (gcd).
There is no unicity of the gcd; hence we define the predicate
Zis_gcd a b g expressing that
g is a gcd of
a and
b.
(We show later that the
gcd is actually unique if we discard its sign.)
Trivial properties of gcd
Extended Euclid algorithm.
Euclid's algorithm to compute the
gcd mainly relies on
the following property.
We implement the extended version of Euclid's algorithm,
i.e. the one computing Bezout's coefficients as it computes
the gcd. We follow the algorithm given in Knuth's
"Art of Computer Programming", vol 2, page 325.
The specification of Euclid's algorithm is the existence of
u, v and d such that ua+vb=d and (gcd a b d).
The recursive part of Euclid's algorithm uses well-founded
recursion of non-negative integers. It maintains 6 integers
u1,u2,u3,v1,v2,v3 such that the following invariant holds:
u1*a+u2*b=u3 and v1*a+v2*b=v3 and gcd(u3,v3)=gcd(a,b).
We get Euclid's algorithm by applying euclid_rec on
1,0,a,0,1,b when b>=0 and 1,0,a,0,-1,-b when b<0.
Existence of Bezout's coefficients for the gcd of a and b
gcd of ca and cb is c gcd(a,b).
Bezout's theorem: a and b are relatively prime if and
only if there exist u and v such that ua+vb = 1.
Gauss's theorem: if a divides bc and if a and b are
relatively prime, then a divides c.
If a is relatively prime to b and c, then it is to bc
After factorization by a gcd, the original numbers are relatively prime.
The sole divisors of a prime number p are -1, 1, p and -p.
A prime number is relatively prime with any number it does not divide
As a consequence, a prime number is relatively prime with smaller numbers
If a prime p divides ab then it divides either a or b
we now prove that Z.gcd is indeed a gcd in
the sense of Zis_gcd.
Notation Zgcd_is_pos :=
Z.gcd_nonneg (
compat "8.3").
Lemma Zgcd_is_gcd :
forall a b,
Zis_gcd a b (
Z.gcd a b).
Theorem Zgcd_spec :
forall x y :
Z,
{z : Z | Zis_gcd x y z /\ 0
<= z}.
Theorem Zdivide_Zgcd:
forall p q r :
Z,
(p | q) ->
(p | r) ->
(p | Z.gcd q r).
Theorem Zis_gcd_gcd:
forall a b c :
Z,
0
<= c ->
Zis_gcd a b c ->
Z.gcd a b = c.
Notation Zgcd_inv_0_l :=
Z.gcd_eq_0_l (
compat "8.3").
Notation Zgcd_inv_0_r :=
Z.gcd_eq_0_r (
compat "8.3").
Theorem Zgcd_div_swap0 :
forall a b :
Z,
0
< Z.gcd a b ->
0
< b ->
(a / Z.gcd a b) * b = a * (b/Z.gcd a b).
Theorem Zgcd_div_swap :
forall a b c :
Z,
0
< Z.gcd a b ->
0
< b ->
(c * a) / Z.gcd a b * b = c * a * (b/Z.gcd a b).
Notation Zgcd_comm :=
Z.gcd_comm (
compat "8.3").
Lemma Zgcd_ass a b c :
Z.gcd (
Z.gcd a b)
c = Z.gcd a (
Z.gcd b c).
Notation Zgcd_Zabs :=
Z.gcd_abs_l (
compat "8.3").
Notation Zgcd_0 :=
Z.gcd_0_r (
compat "8.3").
Notation Zgcd_1 :=
Z.gcd_1_r (
compat "8.3").
Hint Resolve Z.gcd_0_r Z.gcd_1_r :
zarith.
Theorem Zgcd_1_rel_prime :
forall a b,
Z.gcd a b = 1
<-> rel_prime a b.
Definition rel_prime_dec:
forall a b,
{ rel_prime a b }+{ ~ rel_prime a b }.
Definition prime_dec_aux:
forall p m,
{ forall n, 1
< n < m ->
rel_prime n p } +
{ exists n, 1
< n < m /\ ~ rel_prime n p }.
Definition prime_dec:
forall p,
{ prime p }+{ ~ prime p }.
Theorem not_prime_divide:
forall p, 1
< p ->
~ prime p ->
exists n, 1
< n < p /\ (n | p).