1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / Matita is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 include "nat/primes.ma".
16 include "nat/lt_arith.ma".
18 let rec gcd_aux p m n: nat \def
19 match divides_b n m with
24 |(S q) \Rightarrow gcd_aux q n (m \mod n)]].
26 definition gcd : nat \to nat \to nat \def
32 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
36 | (S p) \Rightarrow gcd_aux (S p) n (S p) ]].
38 theorem divides_mod: \forall p,m,n:nat. O < n \to p \divides m \to p \divides n \to
39 p \divides (m \mod n).
40 intros.elim H1.elim H2.
41 (* apply (witness ? ? (n2 - n1*(m / n))). *)
43 rewrite > distr_times_minus.
44 rewrite < H3 in \vdash (? ? ? (? % ?)).
45 rewrite < assoc_times.
46 rewrite < H4 in \vdash (? ? ? (? ? (? % ?))).
47 apply sym_eq.apply plus_to_minus.
50 rewrite < (div_mod ? ? H).
55 theorem divides_mod_to_divides: \forall p,m,n:nat. O < n \to
56 p \divides (m \mod n) \to p \divides n \to p \divides m.
57 intros.elim H1.elim H2.
58 apply (witness p m ((n2*(m / n))+n1)).
59 rewrite > distr_times_plus.
61 rewrite < assoc_times.
62 rewrite < H4.rewrite < sym_times.
63 apply div_mod.assumption.
66 theorem divides_gcd_aux_mn: \forall p,m,n. O < n \to n \le m \to n \le p \to
67 gcd_aux p m n \divides m \land gcd_aux p m n \divides n.
69 absurd (O < n).assumption.apply le_to_not_lt.assumption.
70 cut ((n1 \divides m) \lor (n1 \ndivides m)).
72 elim Hcut.rewrite > divides_to_divides_b_true.
74 split.assumption.apply (witness n1 n1 (S O)).apply times_n_SO.
75 assumption.assumption.
76 rewrite > not_divides_to_divides_b_false.
78 cut (gcd_aux n n1 (m \mod n1) \divides n1 \land
79 gcd_aux n n1 (m \mod n1) \divides mod m n1).
81 split.apply (divides_mod_to_divides ? ? n1).
82 assumption.assumption.assumption.assumption.
84 cut (O \lt m \mod n1 \lor O = mod m n1).
85 elim Hcut1.assumption.
86 apply False_ind.apply H4.apply mod_O_to_divides.
87 assumption.apply sym_eq.assumption.
88 apply le_to_or_lt_eq.apply le_O_n.
90 apply lt_mod_m_m.assumption.
92 apply (trans_le ? n1).
93 change with (m \mod n1 < n1).
94 apply lt_mod_m_m.assumption.assumption.
95 assumption.assumption.
96 apply (decidable_divides n1 m).assumption.
99 theorem divides_gcd_nm: \forall n,m.
100 gcd n m \divides m \land gcd n m \divides n.
107 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
111 | (S p) \Rightarrow gcd_aux (S p) n (S p) ] ] \divides m
117 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
121 | (S p) \Rightarrow gcd_aux (S p) n (S p) ] ] \divides n).
122 apply (leb_elim n m).
124 simplify.intros.split.
125 apply (witness m m (S O)).apply times_n_SO.
126 apply (witness m O O).apply times_n_O.
128 (gcd_aux (S m1) m (S m1) \divides m
130 gcd_aux (S m1) m (S m1) \divides (S m1)).
131 apply divides_gcd_aux_mn.
132 unfold lt.apply le_S_S.apply le_O_n.
133 assumption.apply le_n.
136 simplify.intros.split.
137 apply (witness n O O).apply times_n_O.
138 apply (witness n n (S O)).apply times_n_SO.
140 (gcd_aux (S m1) n (S m1) \divides (S m1)
142 gcd_aux (S m1) n (S m1) \divides n).
143 cut (gcd_aux (S m1) n (S m1) \divides n
145 gcd_aux (S m1) n (S m1) \divides S m1).
146 elim Hcut.split.assumption.assumption.
147 apply divides_gcd_aux_mn.
148 unfold lt.apply le_S_S.apply le_O_n.
149 apply not_lt_to_le.unfold Not. unfold lt.intro.apply H.
150 rewrite > H1.apply (trans_le ? (S n)).
151 apply le_n_Sn.assumption.apply le_n.
154 theorem divides_gcd_n: \forall n,m. gcd n m \divides n.
156 exact (proj2 ? ? (divides_gcd_nm n m)).
159 theorem divides_gcd_m: \forall n,m. gcd n m \divides m.
161 exact (proj1 ? ? (divides_gcd_nm n m)).
165 theorem divides_times_gcd_aux: \forall p,m,n,d,c.
166 O \lt c \to O < n \to n \le m \to n \le p \to
167 d \divides (c*m) \to d \divides (c*n) \to d \divides c*gcd_aux p m n.
172 | apply le_to_not_lt.
176 cut (n1 \divides m \lor n1 \ndivides m)
178 [ rewrite > divides_to_divides_b_true
184 | rewrite > not_divides_to_divides_b_false
188 | cut (O \lt m \mod n1 \lor O = m \mod n1)
191 | absurd (n1 \divides m)
192 [ apply mod_O_to_divides
200 | apply le_to_or_lt_eq.
206 | apply le_S_S_to_le.
207 apply (trans_le ? n1)
208 [ change with (m \mod n1 < n1).
214 | rewrite < times_mod
215 [ rewrite < (sym_times c m).
216 rewrite < (sym_times c n1).
218 [ rewrite > (S_pred c)
219 [ rewrite > (S_pred n1)
220 [ apply (lt_O_times_S_S)
236 | apply (decidable_divides n1 m).
242 (*a particular case of the previous theorem (setting c=1)*)
243 theorem divides_gcd_aux: \forall p,m,n,d. O < n \to n \le m \to n \le p \to
244 d \divides m \to d \divides n \to d \divides gcd_aux p m n.
246 rewrite > (times_n_SO (gcd_aux p m n)).
247 rewrite < (sym_times (S O)).
248 apply (divides_times_gcd_aux)
253 | rewrite > (sym_times (S O)).
254 rewrite < (times_n_SO m).
256 | rewrite > (sym_times (S O)).
257 rewrite < (times_n_SO n).
262 theorem divides_d_times_gcd: \forall m,n,d,c.
263 O \lt c \to d \divides (c*m) \to d \divides (c*n) \to d \divides c*gcd n m.
271 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
275 | (S p) \Rightarrow gcd_aux (S p) n (S p) ]]).
277 [ apply (nat_case1 n)
282 change with (d \divides c*gcd_aux (S m1) m (S m1)).
283 apply divides_times_gcd_aux
289 | apply (le_n (S m1))
295 | apply (nat_case1 m)
300 change with (d \divides c * gcd_aux (S m1) n (S m1)).
301 apply divides_times_gcd_aux
303 change with (O \lt c).
309 | apply (le_n (S m1)).
318 (*a particular case of the previous theorem (setting c=1)*)
319 theorem divides_d_gcd: \forall m,n,d.
320 d \divides m \to d \divides n \to d \divides gcd n m.
322 rewrite > (times_n_SO (gcd n m)).
323 rewrite < (sym_times (S O)).
324 apply (divides_d_times_gcd)
326 | rewrite > (sym_times (S O)).
327 rewrite < (times_n_SO m).
329 | rewrite > (sym_times (S O)).
330 rewrite < (times_n_SO n).
335 theorem eq_minus_gcd_aux: \forall p,m,n.O < n \to n \le m \to n \le p \to
336 \exists a,b. a*n - b*m = gcd_aux p m n \lor b*m - a*n = gcd_aux p m n.
341 |apply le_to_not_lt.assumption
344 [cut (n1 \divides m \lor n1 \ndivides m)
347 [rewrite > divides_to_divides_b_true
349 apply (ex_intro ? ? (S O)).
350 apply (ex_intro ? ? O).
359 |rewrite > not_divides_to_divides_b_false
361 (\exists a,b.a*n1 - b*m = gcd_aux n n1 (m \mod n1)
362 \lor b*m - a*n1 = gcd_aux n n1 (m \mod n1)).
364 (\exists a,b.a*(m \mod n1) - b*n1= gcd_aux n n1 (m \mod n1)
365 \lor b*n1 - a*(m \mod n1) = gcd_aux n n1 (m \mod n1))
366 [elim Hcut2.elim H5.elim H6
369 apply (ex_intro ? ? (a1+a*(m / n1))).
370 apply (ex_intro ? ? a).
373 rewrite < (sym_times n1).
374 rewrite > distr_times_plus.
375 rewrite > (sym_times n1).
376 rewrite > (sym_times n1).
377 rewrite > (div_mod m n1) in \vdash (? ? (? % ?) ?)
378 [rewrite > assoc_times.
380 rewrite > distr_times_plus.
381 rewrite < eq_minus_minus_minus_plus.
384 [rewrite < minus_n_n.reflexivity
391 apply (ex_intro ? ? (a1+a*(m / n1))).
392 apply (ex_intro ? ? a).
394 (* clear Hcut2.clear H5.clear H6.clear H. *)
396 rewrite > distr_times_plus.
398 rewrite > (sym_times n1).
399 rewrite > (div_mod m n1) in \vdash (? ? (? ? %) ?)
400 [rewrite > distr_times_plus.
401 rewrite > assoc_times.
402 rewrite < eq_minus_minus_minus_plus.
405 [rewrite < minus_n_n.reflexivity
411 |apply (H n1 (m \mod n1))
412 [cut (O \lt m \mod n1 \lor O = m \mod n1)
415 |absurd (n1 \divides m)
416 [apply mod_O_to_divides
423 |apply le_to_or_lt_eq.
430 apply (trans_le ? n1)
431 [change with (m \mod n1 < n1).
442 |apply (decidable_divides n1 m).
445 |apply (lt_to_le_to_lt ? n1);assumption
450 theorem eq_minus_gcd:
451 \forall m,n.\exists a,b.a*n - b*m = (gcd n m) \lor b*m - a*n = (gcd n m).
454 apply (leb_elim n m).
457 apply (ex_intro ? ? O).
458 apply (ex_intro ? ? (S O)).
461 apply sym_eq.apply minus_n_O.
465 a*(S m1) - b*m = (gcd_aux (S m1) m (S m1))
466 \lor b*m - a*(S m1) = (gcd_aux (S m1) m (S m1))).
467 apply eq_minus_gcd_aux.
468 unfold lt. apply le_S_S.apply le_O_n.
469 assumption.apply le_n.
472 apply (ex_intro ? ? (S O)).
473 apply (ex_intro ? ? O).
476 apply sym_eq.apply minus_n_O.
480 a*n - b*(S m1) = (gcd_aux (S m1) n (S m1))
481 \lor b*(S m1) - a*n = (gcd_aux (S m1) n (S m1))).
484 a*(S m1) - b*n = (gcd_aux (S m1) n (S m1))
486 b*n - a*(S m1) = (gcd_aux (S m1) n (S m1))).
487 elim Hcut.elim H2.elim H3.
488 apply (ex_intro ? ? a1).
489 apply (ex_intro ? ? a).
491 apply (ex_intro ? ? a1).
492 apply (ex_intro ? ? a).
494 apply eq_minus_gcd_aux.
495 unfold lt. apply le_S_S.apply le_O_n.
496 apply lt_to_le.apply not_le_to_lt.assumption.
500 (* some properties of gcd *)
502 theorem gcd_O_n: \forall n:nat. gcd O n = n.
503 intro.simplify.reflexivity.
506 theorem gcd_O_to_eq_O:\forall m,n:nat. (gcd m n) = O \to
508 intros.cut (O \divides n \land O \divides m).
509 elim Hcut.elim H2.split.
510 assumption.elim H1.assumption.
512 apply divides_gcd_nm.
515 theorem lt_O_gcd:\forall m,n:nat. O < n \to O < gcd m n.
517 apply (nat_case1 (gcd m n)).
519 generalize in match (gcd_O_to_eq_O m n H1).
521 rewrite < H4 in \vdash (? ? %).assumption.
522 intros.unfold lt.apply le_S_S.apply le_O_n.
525 theorem gcd_n_n: \forall n.gcd n n = n.
528 |apply le_to_le_to_eq
534 [apply lt_O_gcd.apply lt_O_S
536 [apply divides_n_n|apply divides_n_n]
542 theorem gcd_SO_to_lt_O: \forall i,n. (S O) < n \to gcd i n = (S O) \to
545 elim (le_to_or_lt_eq ? ? (le_O_n i))
547 |absurd ((gcd i n) = (S O))
552 apply (lt_to_not_eq (S O) n H).
553 apply sym_eq.assumption
558 theorem gcd_SO_to_lt_n: \forall i,n. (S O) < n \to i \le n \to gcd i n = (S O) \to
561 elim (le_to_or_lt_eq ? ? H1)
563 |absurd ((gcd i n) = (S O))
568 apply (lt_to_not_eq (S O) n H).
569 apply sym_eq.assumption
574 theorem gcd_n_times_nm: \forall n,m. O < m \to gcd n (n*m) = n.
575 intro.apply (nat_case n)
580 [apply lt_O_S|apply divides_gcd_n]
582 [apply lt_O_gcd.rewrite > (times_n_O O).
583 apply lt_times[apply lt_O_S|assumption]
585 [apply (witness ? ? m1).reflexivity
593 theorem symmetric_gcd: symmetric nat gcd.
595 (\forall n,m:nat. gcd n m = gcd m n).
597 cut (O < (gcd n m) \lor O = (gcd n m)).
599 cut (O < (gcd m n) \lor O = (gcd m n)).
602 apply divides_to_le.assumption.
603 apply divides_d_gcd.apply divides_gcd_n.apply divides_gcd_m.
604 apply divides_to_le.assumption.
605 apply divides_d_gcd.apply divides_gcd_n.apply divides_gcd_m.
608 elim Hcut2.rewrite > H2.rewrite > H3.reflexivity.
609 apply gcd_O_to_eq_O.apply sym_eq.assumption.
610 apply le_to_or_lt_eq.apply le_O_n.
613 elim Hcut1.rewrite > H1.rewrite > H2.reflexivity.
614 apply gcd_O_to_eq_O.apply sym_eq.assumption.
615 apply le_to_or_lt_eq.apply le_O_n.
618 variant sym_gcd: \forall n,m:nat. gcd n m = gcd m n \def
621 theorem le_gcd_times: \forall m,n,p:nat. O< p \to gcd m n \le gcd m (n*p).
623 apply (nat_case n).apply le_n.
627 rewrite > (times_n_O O).
628 apply lt_times.unfold lt.apply le_S_S.apply le_O_n.assumption.
630 apply (transitive_divides ? (S m1)).
632 apply (witness ? ? p).reflexivity.
636 theorem gcd_times_SO_to_gcd_SO: \forall m,n,p:nat. O < n \to O < p \to
637 gcd m (n*p) = (S O) \to gcd m n = (S O).
639 apply antisymmetric_le.
641 apply le_gcd_times.assumption.
642 change with (O < gcd m n).
643 apply lt_O_gcd.assumption.
646 (* for the "converse" of the previous result see the end of this development *)
648 theorem eq_gcd_SO_to_not_divides: \forall n,m. (S O) < n \to
649 (gcd n m) = (S O) \to \lnot (divides n m).
652 generalize in match H1.
656 [elim (gcd_times_SO_to_gcd_SO n n n1 ? ? H4)
657 [cut (gcd n (n*n1) = n)
658 [apply (lt_to_not_eq (S O) n)
659 [assumption|rewrite < H4.assumption]
660 |apply gcd_n_times_nm.assumption
662 |apply (trans_lt ? (S O))[apply le_n|assumption]
665 |elim (le_to_or_lt_eq O n1 (le_O_n n1));
668 apply (le_to_not_lt n (S O))
671 [rewrite > H4.apply lt_O_S
673 [apply (witness ? ? n1).reflexivity
683 theorem gcd_SO_n: \forall n:nat. gcd (S O) n = (S O).
685 apply antisym_le.apply divides_to_le.unfold lt.apply le_n.
687 cut (O < gcd (S O) n \lor O = gcd (S O) n).
688 elim Hcut.assumption.
690 apply (not_eq_O_S O).
691 cut ((S O)=O \land n=O).
692 elim Hcut1.apply sym_eq.assumption.
693 apply gcd_O_to_eq_O.apply sym_eq.assumption.
694 apply le_to_or_lt_eq.apply le_O_n.
697 theorem divides_gcd_mod: \forall m,n:nat. O < n \to
698 divides (gcd m n) (gcd n (m \mod n)).
701 apply divides_mod.assumption.
707 theorem divides_mod_gcd: \forall m,n:nat. O < n \to
708 divides (gcd n (m \mod n)) (gcd m n) .
712 apply (divides_mod_to_divides ? ? n).
718 theorem gcd_mod: \forall m,n:nat. O < n \to
719 (gcd n (m \mod n)) = (gcd m n) .
721 apply antisymmetric_divides.
722 apply divides_mod_gcd.assumption.
723 apply divides_gcd_mod.assumption.
728 theorem prime_to_gcd_SO: \forall n,m:nat. prime n \to n \ndivides m \to
730 intros.unfold prime in H.
733 apply not_lt_to_le.unfold Not.unfold lt.
735 apply H1.rewrite < (H3 (gcd n m)).
737 apply divides_gcd_n.assumption.
738 cut (O < gcd n m \lor O = gcd n m).
739 elim Hcut.assumption.
741 apply (not_le_Sn_O (S O)).
743 elim Hcut1.rewrite < H5 in \vdash (? ? %).assumption.
744 apply gcd_O_to_eq_O.apply sym_eq.assumption.
745 apply le_to_or_lt_eq.apply le_O_n.
748 (* primes and divides *)
749 theorem divides_times_to_divides: \forall n,p,q:nat.prime n \to n \divides p*q \to
750 n \divides p \lor n \divides q.
752 cut (n \divides p \lor n \ndivides p)
756 cut (\exists a,b. a*n - b*p = (S O) \lor b*p - a*n = (S O))
757 [elim Hcut1.elim H3.elim H4
759 rewrite > (times_n_SO q).rewrite < H5.
760 rewrite > distr_times_minus.
761 rewrite > (sym_times q (a1*p)).
762 rewrite > (assoc_times a1).
766 applyS (witness n (n*(q*a-a1*n2)) (q*a-a1*n2))
768 applyS (witness n ? ? (refl_eq ? ?)) (* timeout=50 *).
770 rewrite < (sym_times n).rewrite < assoc_times.
771 rewrite > (sym_times q).rewrite > assoc_times.
772 rewrite < (assoc_times a1).rewrite < (sym_times n).
773 rewrite > (assoc_times n).
774 rewrite < distr_times_minus.
775 apply (witness ? ? (q*a-a1*n2)).reflexivity
778 rewrite > (times_n_SO q).rewrite < H5.
779 rewrite > distr_times_minus.
780 rewrite > (sym_times q (a1*p)).
781 rewrite > (assoc_times a1).
782 elim H1.rewrite > H6.
783 rewrite < sym_times.rewrite > assoc_times.
784 rewrite < (assoc_times q).
785 rewrite < (sym_times n).
786 rewrite < distr_times_minus.
787 apply (witness ? ? (n1*a1-q*a)).reflexivity
788 ](* end second case *)
789 |rewrite < (prime_to_gcd_SO n p)
790 [apply eq_minus_gcd|assumption|assumption
794 |apply (decidable_divides n p).
795 apply (trans_lt ? (S O))
796 [unfold lt.apply le_n
797 |unfold prime in H.elim H. assumption
802 theorem divides_exp_to_divides:
803 \forall p,n,m:nat. prime p \to
804 p \divides n \sup m \to p \divides n.
805 intros 3.elim m.simplify in H1.
806 apply (transitive_divides p (S O)).assumption.
808 cut (p \divides n \lor p \divides n \sup n1).
809 elim Hcut.assumption.
810 apply H.assumption.assumption.
811 apply divides_times_to_divides.assumption.
815 theorem divides_exp_to_eq:
816 \forall p,q,m:nat. prime p \to prime q \to
817 p \divides q \sup m \to p = q.
821 apply (divides_exp_to_divides p q m).
822 assumption.assumption.
823 unfold prime in H.elim H.assumption.
826 theorem eq_gcd_times_SO: \forall m,n,p:nat. O < n \to O < p \to
827 gcd m n = (S O) \to gcd m p = (S O) \to gcd m (n*p) = (S O).
829 apply antisymmetric_le.
832 cut (divides (smallest_factor (gcd m (n*p))) n \lor
833 divides (smallest_factor (gcd m (n*p))) p).
835 apply (not_le_Sn_n (S O)).
836 change with ((S O) < (S O)).
837 rewrite < H2 in \vdash (? ? %).
838 apply (lt_to_le_to_lt ? (smallest_factor (gcd m (n*p)))).
839 apply lt_SO_smallest_factor.assumption.
841 rewrite > H2.unfold lt.apply le_n.
842 apply divides_d_gcd.assumption.
843 apply (transitive_divides ? (gcd m (n*p))).
844 apply divides_smallest_factor_n.
845 apply (trans_lt ? (S O)). unfold lt. apply le_n. assumption.
847 apply (not_le_Sn_n (S O)).
848 change with ((S O) < (S O)).
849 rewrite < H3 in \vdash (? ? %).
850 apply (lt_to_le_to_lt ? (smallest_factor (gcd m (n*p)))).
851 apply lt_SO_smallest_factor.assumption.
853 rewrite > H3.unfold lt.apply le_n.
854 apply divides_d_gcd.assumption.
855 apply (transitive_divides ? (gcd m (n*p))).
856 apply divides_smallest_factor_n.
857 apply (trans_lt ? (S O)). unfold lt. apply le_n. assumption.
859 apply divides_times_to_divides.
860 apply prime_smallest_factor_n.
862 apply (transitive_divides ? (gcd m (n*p))).
863 apply divides_smallest_factor_n.
864 apply (trans_lt ? (S O)).unfold lt. apply le_n. assumption.
866 change with (O < gcd m (n*p)).
868 rewrite > (times_n_O O).
869 apply lt_times.assumption.assumption.
872 theorem gcd_SO_to_divides_times_to_divides: \forall m,n,p:nat. O < n \to
873 gcd n m = (S O) \to n \divides (m*p) \to n \divides p.
875 cut (n \divides p \lor n \ndivides p)
878 |cut (\exists a,b. a*n - b*m = (S O) \lor b*m - a*n = (S O))
879 [elim Hcut1.elim H4.elim H5
881 rewrite > (times_n_SO p).rewrite < H6.
882 rewrite > distr_times_minus.
883 rewrite > (sym_times p (a1*m)).
884 rewrite > (assoc_times a1).
886 applyS (witness n ? ? (refl_eq ? ?)) (* timeout=50 *).
888 rewrite > (times_n_SO p).rewrite < H6.
889 rewrite > distr_times_minus.
890 rewrite > (sym_times p (a1*m)).
891 rewrite > (assoc_times a1).
893 applyS (witness n ? ? (refl_eq ? ?)).
894 ](* end second case *)
895 |rewrite < H1.apply eq_minus_gcd.
898 |apply (decidable_divides n p).
904 theorem divides_to_divides_times1: \forall p,q,n. prime p \to prime q \to p \neq q \to
905 divides p n \to divides q n \to divides (p*q) n.
908 elim (divides_times_to_divides ? ? ? H1 H4)
909 [elim H.apply False_ind.
910 apply H2.apply sym_eq.apply H8
912 |apply prime_to_lt_SO.assumption
915 apply (witness ? ? n1).
916 rewrite > assoc_times.
917 rewrite < H7.assumption
922 theorem divides_to_divides_times: \forall p,q,n. prime p \to p \ndivides q \to
923 divides p n \to divides q n \to divides (p*q) n.
926 elim (divides_times_to_divides ? ? ? H H2)
927 [apply False_ind.apply H1.assumption
929 apply (witness ? ? n2).
930 rewrite > sym_times in ⊢ (? ? ? (? % ?)).
931 rewrite > assoc_times.
932 rewrite < H6.assumption