(**************************************************************)
(*   Copyright Dominique Larchey-Wendling [*]                 *)
(*                                                            *)
(*                             [*] Affiliation LORIA -- CNRS  *)
(**************************************************************)
(*      This file is distributed under the terms of the       *)
(*         CeCILL v2 FREE SOFTWARE LICENSE AGREEMENT          *)
(**************************************************************)
(** ** Euclidian division and Bezout's identity *)
Set Implicit Arguments.
Section Euclid.
Definition euclid n d : d <> 0 -> { q : nat & { r | n = q*d+r /\ r < d } }.
End Euclid.
Definition arem n d q j := j <= d /\ (n = 2*q*d+j \/ q <> 0 /\ n = 2*q*d-j).
Fact division_by_even n d : d <> 0 -> { q : nat & { j | arem n d q j } }.
Fact own_multiple x p : x = p*x -> x = 0 \/ p = 1.
Fact mult_is_one p q : p*q = 1 -> p = 1 /\ q = 1.
Definition divides n k := exists p, k = p*n.
Section divides.
Infix "div" := divides (at level 70, no associativity).
Fact divides_refl x : x div x.
Fact divides_trans x y z : x div y -> y div z -> x div z.
Fact divides_0 p : p div 0.
Fact divides_2_inv p : p div 2 -> p = 1 \/ p = 2.
Fact divides_mult p q k : p div q -> p div k*q.
Fact divides_mult_r p q k : p div q -> p div q*k.
Fact divides_mult_compat a b c d : a div b -> c div d -> a*c div b*d.
Fact divides_minus p q1 q2 : p div q1 -> p div q2 -> p div q1 - q2.
Fact divides_plus p q1 q2 : p div q1 -> p div q2 -> p div q1+q2.
Fact divides_plus_inv p q1 q2 : p div q1 -> p div q1+q2 -> p div q2.
Fact divides_le p q : q <> 0 -> p div q -> p <= q.
Fact divides_mult_inv k p q : k <> 0 -> k*p div k*q -> p div q.

(* Specifications: *)
Lemma divides_fact m p : 1 < p <= m -> p div fact m.
Lemma divides_mult_inv_l p q r : p * q div r -> p div r /\ q div r.
End divides.