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 set "baseuri" "cic:/matita/nat/gcd".
17 include "nat/primes.ma".
19 let rec gcd_aux p m n: nat \def
20 match divides_b n m with
25 |(S q) \Rightarrow gcd_aux q n (m \mod n)]].
27 definition gcd : nat \to nat \to nat \def
33 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
37 | (S p) \Rightarrow gcd_aux (S p) n (S p) ]].
39 theorem divides_mod: \forall p,m,n:nat. O < n \to p \divides m \to p \divides n \to
40 p \divides (m \mod n).
41 intros.elim H1.elim H2.
42 apply (witness ? ? (n2 - n1*(m / n))).
43 rewrite > distr_times_minus.
45 rewrite < assoc_times.
54 theorem divides_mod_to_divides: \forall p,m,n:nat. O < n \to
55 p \divides (m \mod n) \to p \divides n \to p \divides m.
56 intros.elim H1.elim H2.
57 apply (witness p m ((n1*(m / n))+n2)).
58 rewrite > distr_times_plus.
60 rewrite < assoc_times.
61 rewrite < H4.rewrite < sym_times.
62 apply div_mod.assumption.
65 theorem divides_gcd_aux_mn: \forall p,m,n. O < n \to n \le m \to n \le p \to
66 gcd_aux p m n \divides m \land gcd_aux p m n \divides n.
68 absurd (O < n).assumption.apply le_to_not_lt.assumption.
69 cut ((n1 \divides m) \lor (n1 \ndivides m)).
71 elim Hcut.rewrite > divides_to_divides_b_true.
73 split.assumption.apply (witness n1 n1 (S O)).apply times_n_SO.
74 assumption.assumption.
75 rewrite > not_divides_to_divides_b_false.
77 cut (gcd_aux n n1 (m \mod n1) \divides n1 \land
78 gcd_aux n n1 (m \mod n1) \divides mod m n1).
80 split.apply (divides_mod_to_divides ? ? n1).
81 assumption.assumption.assumption.assumption.
83 cut (O \lt m \mod n1 \lor O = mod m n1).
84 elim Hcut1.assumption.
85 apply False_ind.apply H4.apply mod_O_to_divides.
86 assumption.apply sym_eq.assumption.
87 apply le_to_or_lt_eq.apply le_O_n.
89 apply lt_mod_m_m.assumption.
91 apply (trans_le ? n1).
92 change with (m \mod n1 < n1).
93 apply lt_mod_m_m.assumption.assumption.
94 assumption.assumption.
95 apply (decidable_divides n1 m).assumption.
98 theorem divides_gcd_nm: \forall n,m.
99 gcd n m \divides m \land gcd n m \divides n.
101 (*CSC: simplify simplifies too much because of a redex in gcd *)
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)).
164 theorem divides_gcd_aux: \forall p,m,n,d. O < n \to n \le m \to n \le p \to
165 d \divides m \to d \divides n \to d \divides gcd_aux p m n.
167 absurd (O < n).assumption.apply le_to_not_lt.assumption.
169 cut (n1 \divides m \lor n1 \ndivides m).
171 rewrite > divides_to_divides_b_true.
173 assumption.assumption.
174 rewrite > not_divides_to_divides_b_false.
177 cut (O \lt m \mod n1 \lor O = m \mod n1).
178 elim Hcut1.assumption.
179 absurd (n1 \divides m).apply mod_O_to_divides.
180 assumption.apply sym_eq.assumption.assumption.
181 apply le_to_or_lt_eq.apply le_O_n.
183 apply lt_mod_m_m.assumption.
185 apply (trans_le ? n1).
186 change with (m \mod n1 < n1).
187 apply lt_mod_m_m.assumption.assumption.
189 apply divides_mod.assumption.assumption.assumption.
190 assumption.assumption.
191 apply (decidable_divides n1 m).assumption.
194 theorem divides_d_gcd: \forall m,n,d.
195 d \divides m \to d \divides n \to d \divides gcd n m.
197 (*CSC: here simplify simplifies too much because of a redex in gcd *)
204 | (S p) \Rightarrow gcd_aux (S p) m (S p) ]
208 | (S p) \Rightarrow gcd_aux (S p) n (S p) ]]).
209 apply (leb_elim n m).
210 apply (nat_case1 n).simplify.intros.assumption.
212 change with (d \divides gcd_aux (S m1) m (S m1)).
213 apply divides_gcd_aux.
214 unfold lt.apply le_S_S.apply le_O_n.assumption.apply le_n.assumption.
215 rewrite < H2.assumption.
216 apply (nat_case1 m).simplify.intros.assumption.
218 change with (d \divides gcd_aux (S m1) n (S m1)).
219 apply divides_gcd_aux.
220 unfold lt.apply le_S_S.apply le_O_n.
221 apply lt_to_le.apply not_le_to_lt.assumption.apply le_n.assumption.
222 rewrite < H2.assumption.
225 theorem eq_minus_gcd_aux: \forall p,m,n.O < n \to n \le m \to n \le p \to
226 \exists a,b. a*n - b*m = gcd_aux p m n \lor b*m - a*n = gcd_aux p m n.
229 absurd (O < n).assumption.apply le_to_not_lt.assumption.
231 cut (n1 \divides m \lor n1 \ndivides m).
234 rewrite > divides_to_divides_b_true.
236 apply (ex_intro ? ? (S O)).
237 apply (ex_intro ? ? O).
238 left.simplify.rewrite < plus_n_O.
239 apply sym_eq.apply minus_n_O.
240 assumption.assumption.
241 rewrite > not_divides_to_divides_b_false.
244 a*n1 - b*m = gcd_aux n n1 (m \mod n1)
246 b*m - a*n1 = gcd_aux n n1 (m \mod n1)).
249 a*(m \mod n1) - b*n1= gcd_aux n n1 (m \mod n1)
251 b*n1 - a*(m \mod n1) = gcd_aux n n1 (m \mod n1)).
252 elim Hcut2.elim H5.elim H6.
255 apply (ex_intro ? ? (a1+a*(m / n1))).
256 apply (ex_intro ? ? a).
259 rewrite < (sym_times n1).
260 rewrite > distr_times_plus.
261 rewrite > (sym_times n1).
262 rewrite > (sym_times n1).
263 rewrite > (div_mod m n1) in \vdash (? ? (? % ?) ?).
264 rewrite > assoc_times.
266 rewrite > distr_times_plus.
267 rewrite < eq_minus_minus_minus_plus.
269 rewrite < plus_minus.
270 rewrite < minus_n_n.reflexivity.
275 apply (ex_intro ? ? (a1+a*(m / n1))).
276 apply (ex_intro ? ? a).
278 (* clear Hcut2.clear H5.clear H6.clear H. *)
280 rewrite > distr_times_plus.
282 rewrite > (sym_times n1).
283 rewrite > (div_mod m n1) in \vdash (? ? (? ? %) ?).
284 rewrite > distr_times_plus.
285 rewrite > assoc_times.
286 rewrite < eq_minus_minus_minus_plus.
288 rewrite < plus_minus.
289 rewrite < minus_n_n.reflexivity.
292 apply (H n1 (m \mod n1)).
293 cut (O \lt m \mod n1 \lor O = m \mod n1).
294 elim Hcut2.assumption.
295 absurd (n1 \divides m).apply mod_O_to_divides.
297 symmetry.assumption.assumption.
298 apply le_to_or_lt_eq.apply le_O_n.
300 apply lt_mod_m_m.assumption.
302 apply (trans_le ? n1).
303 change with (m \mod n1 < n1).
305 assumption.assumption.assumption.assumption.
306 apply (decidable_divides n1 m).assumption.
307 apply (lt_to_le_to_lt ? n1).assumption.assumption.
310 theorem eq_minus_gcd:
311 \forall m,n.\exists a,b.a*n - b*m = (gcd n m) \lor b*m - a*n = (gcd n m).
314 apply (leb_elim n m).
317 apply (ex_intro ? ? O).
318 apply (ex_intro ? ? (S O)).
321 apply sym_eq.apply minus_n_O.
325 a*(S m1) - b*m = (gcd_aux (S m1) m (S m1))
326 \lor b*m - a*(S m1) = (gcd_aux (S m1) m (S m1))).
327 apply eq_minus_gcd_aux.
328 unfold lt. apply le_S_S.apply le_O_n.
329 assumption.apply le_n.
332 apply (ex_intro ? ? (S O)).
333 apply (ex_intro ? ? O).
336 apply sym_eq.apply minus_n_O.
340 a*n - b*(S m1) = (gcd_aux (S m1) n (S m1))
341 \lor b*(S m1) - a*n = (gcd_aux (S m1) n (S m1))).
344 a*(S m1) - b*n = (gcd_aux (S m1) n (S m1))
346 b*n - a*(S m1) = (gcd_aux (S m1) n (S m1))).
347 elim Hcut.elim H2.elim H3.
348 apply (ex_intro ? ? a1).
349 apply (ex_intro ? ? a).
351 apply (ex_intro ? ? a1).
352 apply (ex_intro ? ? a).
354 apply eq_minus_gcd_aux.
355 unfold lt. apply le_S_S.apply le_O_n.
356 apply lt_to_le.apply not_le_to_lt.assumption.
360 (* some properties of gcd *)
362 theorem gcd_O_n: \forall n:nat. gcd O n = n.
363 intro.simplify.reflexivity.
366 theorem gcd_O_to_eq_O:\forall m,n:nat. (gcd m n) = O \to
368 intros.cut (O \divides n \land O \divides m).
369 elim Hcut.elim H2.split.
370 assumption.elim H1.assumption.
372 apply divides_gcd_nm.
375 theorem lt_O_gcd:\forall m,n:nat. O < n \to O < gcd m n.
377 apply (nat_case1 (gcd m n)).
379 generalize in match (gcd_O_to_eq_O m n H1).
381 rewrite < H4 in \vdash (? ? %).assumption.
382 intros.unfold lt.apply le_S_S.apply le_O_n.
385 theorem symmetric_gcd: symmetric nat gcd.
386 (*CSC: bug here: unfold symmetric does not work *)
388 (\forall n,m:nat. gcd n m = gcd m n).
390 cut (O < (gcd n m) \lor O = (gcd n m)).
392 cut (O < (gcd m n) \lor O = (gcd m n)).
395 apply divides_to_le.assumption.
396 apply divides_d_gcd.apply divides_gcd_n.apply divides_gcd_m.
397 apply divides_to_le.assumption.
398 apply divides_d_gcd.apply divides_gcd_n.apply divides_gcd_m.
401 elim Hcut2.rewrite > H2.rewrite > H3.reflexivity.
402 apply gcd_O_to_eq_O.apply sym_eq.assumption.
403 apply le_to_or_lt_eq.apply le_O_n.
406 elim Hcut1.rewrite > H1.rewrite > H2.reflexivity.
407 apply gcd_O_to_eq_O.apply sym_eq.assumption.
408 apply le_to_or_lt_eq.apply le_O_n.
411 variant sym_gcd: \forall n,m:nat. gcd n m = gcd m n \def
414 theorem le_gcd_times: \forall m,n,p:nat. O< p \to gcd m n \le gcd m (n*p).
416 apply (nat_case n).reflexivity.
420 rewrite > (times_n_O O).
421 apply lt_times.unfold lt.apply le_S_S.apply le_O_n.assumption.
423 apply (transitive_divides ? (S m1)).
425 apply (witness ? ? p).reflexivity.
429 theorem gcd_times_SO_to_gcd_SO: \forall m,n,p:nat. O < n \to O < p \to
430 gcd m (n*p) = (S O) \to gcd m n = (S O).
432 apply antisymmetric_le.
434 apply le_gcd_times.assumption.
435 change with (O < gcd m n).
436 apply lt_O_gcd.assumption.
439 (* for the "converse" of the previous result see the end of this development *)
441 theorem gcd_SO_n: \forall n:nat. gcd (S O) n = (S O).
443 apply antisym_le.apply divides_to_le.unfold lt.apply le_n.
445 cut (O < gcd (S O) n \lor O = gcd (S O) n).
446 elim Hcut.assumption.
448 apply (not_eq_O_S O).
449 cut ((S O)=O \land n=O).
450 elim Hcut1.apply sym_eq.assumption.
451 apply gcd_O_to_eq_O.apply sym_eq.assumption.
452 apply le_to_or_lt_eq.apply le_O_n.
455 theorem divides_gcd_mod: \forall m,n:nat. O < n \to
456 divides (gcd m n) (gcd n (m \mod n)).
459 apply divides_mod.assumption.
465 theorem divides_mod_gcd: \forall m,n:nat. O < n \to
466 divides (gcd n (m \mod n)) (gcd m n) .
470 apply (divides_mod_to_divides ? ? n).
476 theorem gcd_mod: \forall m,n:nat. O < n \to
477 (gcd n (m \mod n)) = (gcd m n) .
479 apply antisymmetric_divides.
480 apply divides_mod_gcd.assumption.
481 apply divides_gcd_mod.assumption.
486 theorem prime_to_gcd_SO: \forall n,m:nat. prime n \to n \ndivides m \to
488 intros.unfold prime in H.
491 apply not_lt_to_le.unfold Not.unfold lt.
493 apply H1.rewrite < (H3 (gcd n m)).
495 apply divides_gcd_n.assumption.
496 cut (O < gcd n m \lor O = gcd n m).
497 elim Hcut.assumption.
499 apply (not_le_Sn_O (S O)).
501 elim Hcut1.rewrite < H5 in \vdash (? ? %).assumption.
502 apply gcd_O_to_eq_O.apply sym_eq.assumption.
503 apply le_to_or_lt_eq.apply le_O_n.
506 theorem divides_times_to_divides: \forall n,p,q:nat.prime n \to n \divides p*q \to
507 n \divides p \lor n \divides q.
509 cut (n \divides p \lor n \ndivides p)
513 cut (\exists a,b. a*n - b*p = (S O) \lor b*p - a*n = (S O))
514 [elim Hcut1.elim H3.elim H4
516 rewrite > (times_n_SO q).rewrite < H5.
517 rewrite > distr_times_minus.
518 rewrite > (sym_times q (a1*p)).
519 rewrite > (assoc_times a1).
520 elim H1.rewrite > H6.
521 (* applyS (witness n (n*(q*a-a1*n2)) (q*a-a1*n2))
523 applyS (witness n ? ? (refl_eq ? ?)).
525 rewrite < (sym_times n).rewrite < assoc_times.
526 rewrite > (sym_times q).rewrite > assoc_times.
527 rewrite < (assoc_times a1).rewrite < (sym_times n).
528 rewrite > (assoc_times n).
529 rewrite < distr_times_minus.
530 apply (witness ? ? (q*a-a1*n2)).reflexivity
533 rewrite > (times_n_SO q).rewrite < H5.
534 rewrite > distr_times_minus.
535 rewrite > (sym_times q (a1*p)).
536 rewrite > (assoc_times a1).
537 elim H1.rewrite > H6.
538 rewrite < sym_times.rewrite > assoc_times.
539 rewrite < (assoc_times q).
540 rewrite < (sym_times n).
541 rewrite < distr_times_minus.
542 apply (witness ? ? (n2*a1-q*a)).reflexivity
543 ](* end second case *)
544 |rewrite < (prime_to_gcd_SO n p)
545 [apply eq_minus_gcd|assumption|assumption
549 |apply (decidable_divides n p).
550 apply (trans_lt ? (S O))
551 [unfold lt.apply le_n
552 |unfold prime in H.elim H. assumption
557 theorem eq_gcd_times_SO: \forall m,n,p:nat. O < n \to O < p \to
558 gcd m n = (S O) \to gcd m p = (S O) \to gcd m (n*p) = (S O).
560 apply antisymmetric_le.
563 cut (divides (smallest_factor (gcd m (n*p))) n \lor
564 divides (smallest_factor (gcd m (n*p))) p).
566 apply (not_le_Sn_n (S O)).
567 change with ((S O) < (S O)).
568 rewrite < H2 in \vdash (? ? %).
569 apply (lt_to_le_to_lt ? (smallest_factor (gcd m (n*p)))).
570 apply lt_SO_smallest_factor.assumption.
572 rewrite > H2.unfold lt.apply le_n.
573 apply divides_d_gcd.assumption.
574 apply (transitive_divides ? (gcd m (n*p))).
575 apply divides_smallest_factor_n.
576 apply (trans_lt ? (S O)). unfold lt. apply le_n. assumption.
578 apply (not_le_Sn_n (S O)).
579 change with ((S O) < (S O)).
580 rewrite < H3 in \vdash (? ? %).
581 apply (lt_to_le_to_lt ? (smallest_factor (gcd m (n*p)))).
582 apply lt_SO_smallest_factor.assumption.
584 rewrite > H3.unfold lt.apply le_n.
585 apply divides_d_gcd.assumption.
586 apply (transitive_divides ? (gcd m (n*p))).
587 apply divides_smallest_factor_n.
588 apply (trans_lt ? (S O)). unfold lt. apply le_n. assumption.
590 apply divides_times_to_divides.
591 apply prime_smallest_factor_n.
593 apply (transitive_divides ? (gcd m (n*p))).
594 apply divides_smallest_factor_n.
595 apply (trans_lt ? (S O)).unfold lt. apply le_n. assumption.
597 change with (O < gcd m (n*p)).
599 rewrite > (times_n_O O).
600 apply lt_times.assumption.assumption.