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/primes".
17 include "nat/div_and_mod.ma".
18 include "nat/minimization.ma".
19 include "nat/sigma_and_pi.ma".
20 include "nat/factorial.ma".
22 inductive divides (n,m:nat) : Prop \def
23 witness : \forall p:nat.m = times n p \to divides n m.
25 interpretation "divides" 'divides n m = (cic:/matita/nat/primes/divides.ind#xpointer(1/1) n m).
26 interpretation "not divides" 'ndivides n m =
27 (cic:/matita/logic/connectives/Not.con (cic:/matita/nat/primes/divides.ind#xpointer(1/1) n m)).
29 theorem reflexive_divides : reflexive nat divides.
32 exact (witness x x (S O) (times_n_SO x)).
35 theorem divides_to_div_mod_spec :
36 \forall n,m. O < n \to n \divides m \to div_mod_spec m n (m / n) O.
37 intros.elim H1.rewrite > H2.
38 constructor 1.assumption.
39 apply (lt_O_n_elim n H).intros.
41 rewrite > div_times.apply sym_times.
44 theorem div_mod_spec_to_divides :
45 \forall n,m,p. div_mod_spec m n p O \to n \divides m.
47 apply (witness n m p).
49 rewrite > (plus_n_O (p*n)).assumption.
52 theorem divides_to_mod_O:
53 \forall n,m. O < n \to n \divides m \to (m \mod n) = O.
54 intros.apply (div_mod_spec_to_eq2 m n (m / n) (m \mod n) (m / n) O).
55 apply div_mod_spec_div_mod.assumption.
56 apply divides_to_div_mod_spec.assumption.assumption.
59 theorem mod_O_to_divides:
60 \forall n,m. O< n \to (m \mod n) = O \to n \divides m.
62 apply (witness n m (m / n)).
63 rewrite > (plus_n_O (n * (m / n))).
66 (* Andrea: perche' hint non lo trova ?*)
71 theorem divides_n_O: \forall n:nat. n \divides O.
72 intro. apply (witness n O O).apply times_n_O.
75 theorem divides_n_n: \forall n:nat. n \divides n.
76 intro. apply (witness n n (S O)).apply times_n_SO.
79 theorem divides_SO_n: \forall n:nat. (S O) \divides n.
80 intro. apply (witness (S O) n n). simplify.apply plus_n_O.
83 theorem divides_plus: \forall n,p,q:nat.
84 n \divides p \to n \divides q \to n \divides p+q.
86 elim H.elim H1. apply (witness n (p+q) (n2+n1)).
87 rewrite > H2.rewrite > H3.apply sym_eq.apply distr_times_plus.
90 theorem divides_minus: \forall n,p,q:nat.
91 divides n p \to divides n q \to divides n (p-q).
93 elim H.elim H1. apply (witness n (p-q) (n2-n1)).
94 rewrite > H2.rewrite > H3.apply sym_eq.apply distr_times_minus.
97 theorem divides_times: \forall n,m,p,q:nat.
98 n \divides p \to m \divides q \to n*m \divides p*q.
100 elim H.elim H1. apply (witness (n*m) (p*q) (n2*n1)).
101 rewrite > H2.rewrite > H3.
102 apply (trans_eq nat ? (n*(m*(n2*n1)))).
103 apply (trans_eq nat ? (n*(n2*(m*n1)))).
106 apply (trans_eq nat ? ((n2*m)*n1)).
107 apply sym_eq. apply assoc_times.
108 rewrite > (sym_times n2 m).apply assoc_times.
109 apply sym_eq. apply assoc_times.
112 theorem transitive_divides: transitive ? divides.
115 elim H.elim H1. apply (witness x z (n2*n)).
116 rewrite > H3.rewrite > H2.
120 variant trans_divides: \forall n,m,p.
121 n \divides m \to m \divides p \to n \divides p \def transitive_divides.
123 theorem eq_mod_to_divides:\forall n,m,p. O< p \to
124 mod n p = mod m p \to divides p (n-m).
126 cut (n \le m \or \not n \le m).
130 apply (witness p O O).
132 apply eq_minus_n_m_O.
134 apply (witness p (n-m) ((div n p)-(div m p))).
135 rewrite > distr_times_minus.
137 rewrite > (sym_times p).
138 cut ((div n p)*p = n - (mod n p)).
140 rewrite > eq_minus_minus_minus_plus.
143 rewrite < div_mod.reflexivity.
150 apply (decidable_le n m).
153 theorem antisymmetric_divides: antisymmetric nat divides.
154 unfold antisymmetric.intros.elim H. elim H1.
155 apply (nat_case1 n2).intro.
156 rewrite > H3.rewrite > H2.rewrite > H4.
157 rewrite < times_n_O.reflexivity.
159 apply (nat_case1 n).intro.
160 rewrite > H2.rewrite > H3.rewrite > H5.
161 rewrite < times_n_O.reflexivity.
163 apply antisymmetric_le.
164 rewrite > H2.rewrite > times_n_SO in \vdash (? % ?).
165 apply le_times_r.rewrite > H4.apply le_S_S.apply le_O_n.
166 rewrite > H3.rewrite > times_n_SO in \vdash (? % ?).
167 apply le_times_r.rewrite > H5.apply le_S_S.apply le_O_n.
171 theorem divides_to_le : \forall n,m. O < m \to n \divides m \to n \le m.
172 intros. elim H1.rewrite > H2.cut (O < n2).
173 apply (lt_O_n_elim n2 Hcut).intro.rewrite < sym_times.
174 simplify.rewrite < sym_plus.
176 elim (le_to_or_lt_eq O n2).
178 absurd (O<m).assumption.
179 rewrite > H2.rewrite < H3.rewrite < times_n_O.
180 apply (not_le_Sn_n O).
184 theorem divides_to_lt_O : \forall n,m. O < m \to n \divides m \to O < n.
186 elim (le_to_or_lt_eq O n (le_O_n n)).
188 rewrite < H3.absurd (O < m).assumption.
189 rewrite > H2.rewrite < H3.
190 simplify.exact (not_le_Sn_n O).
193 (* boolean divides *)
194 definition divides_b : nat \to nat \to bool \def
195 \lambda n,m :nat. (eqb (m \mod n) O).
197 theorem divides_b_to_Prop :
198 \forall n,m:nat. O < n \to
199 match divides_b n m with
200 [ true \Rightarrow n \divides m
201 | false \Rightarrow n \ndivides m].
204 match eqb (m \mod n) O with
205 [ true \Rightarrow n \divides m
206 | false \Rightarrow n \ndivides m].
208 intro.simplify.apply mod_O_to_divides.assumption.assumption.
209 intro.simplify.unfold Not.intro.apply H1.apply divides_to_mod_O.assumption.assumption.
212 theorem divides_b_true_to_divides :
213 \forall n,m:nat. O < n \to
214 (divides_b n m = true ) \to n \divides m.
218 [ true \Rightarrow n \divides m
219 | false \Rightarrow n \ndivides m].
220 rewrite < H1.apply divides_b_to_Prop.
224 theorem divides_b_false_to_not_divides :
225 \forall n,m:nat. O < n \to
226 (divides_b n m = false ) \to n \ndivides m.
230 [ true \Rightarrow n \divides m
231 | false \Rightarrow n \ndivides m].
232 rewrite < H1.apply divides_b_to_Prop.
236 theorem decidable_divides: \forall n,m:nat.O < n \to
237 decidable (n \divides m).
238 intros.change with ((n \divides m) \lor n \ndivides m).
240 (match divides_b n m with
241 [ true \Rightarrow n \divides m
242 | false \Rightarrow n \ndivides m] \to n \divides m \lor n \ndivides m).
243 apply Hcut.apply divides_b_to_Prop.assumption.
244 elim (divides_b n m).left.apply H1.right.apply H1.
247 theorem divides_to_divides_b_true : \forall n,m:nat. O < n \to
248 n \divides m \to divides_b n m = true.
250 cut (match (divides_b n m) with
251 [ true \Rightarrow n \divides m
252 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = true)).
253 apply Hcut.apply divides_b_to_Prop.assumption.
254 elim (divides_b n m).reflexivity.
255 absurd (n \divides m).assumption.assumption.
258 theorem not_divides_to_divides_b_false: \forall n,m:nat. O < n \to
259 \lnot(n \divides m) \to (divides_b n m) = false.
261 cut (match (divides_b n m) with
262 [ true \Rightarrow n \divides m
263 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = false)).
264 apply Hcut.apply divides_b_to_Prop.assumption.
265 elim (divides_b n m).
266 absurd (n \divides m).assumption.assumption.
271 theorem divides_f_pi_f : \forall f:nat \to nat.\forall n,m,i:nat.
272 m \le i \to i \le n+m \to f i \divides pi n f m.
273 intros 5.elim n.simplify.
274 cut (i = m).rewrite < Hcut.apply divides_n_n.
275 apply antisymmetric_le.assumption.assumption.
277 cut (i < S n1+m \lor i = S n1 + m).
279 apply (transitive_divides ? (pi n1 f m)).
280 apply H1.apply le_S_S_to_le. assumption.
281 apply (witness ? ? (f (S n1+m))).apply sym_times.
283 apply (witness ? ? (pi n1 f m)).reflexivity.
284 apply le_to_or_lt_eq.assumption.
288 theorem mod_S_pi: \forall f:nat \to nat.\forall n,i:nat.
289 i < n \to (S O) < (f i) \to (S (pi n f)) \mod (f i) = (S O).
290 intros.cut (pi n f) \mod (f i) = O.
292 apply mod_S.apply trans_lt O (S O).apply le_n (S O).assumption.
293 rewrite > Hcut.assumption.
294 apply divides_to_mod_O.apply trans_lt O (S O).apply le_n (S O).assumption.
295 apply divides_f_pi_f.assumption.
299 (* divides and fact *)
300 theorem divides_fact : \forall n,i:nat.
301 O < i \to i \le n \to i \divides n!.
302 intros 3.elim n.absurd (O<i).assumption.apply (le_n_O_elim i H1).
303 apply (not_le_Sn_O O).
304 change with (i \divides (S n1)*n1!).
305 apply (le_n_Sm_elim i n1 H2).
307 apply (transitive_divides ? n1!).
308 apply H1.apply le_S_S_to_le. assumption.
309 apply (witness ? ? (S n1)).apply sym_times.
312 apply (witness ? ? n1!).reflexivity.
315 theorem mod_S_fact: \forall n,i:nat.
316 (S O) < i \to i \le n \to (S n!) \mod i = (S O).
317 intros.cut (n! \mod i = O).
319 apply mod_S.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
320 rewrite > Hcut.assumption.
321 apply divides_to_mod_O.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
322 apply divides_fact.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
326 theorem not_divides_S_fact: \forall n,i:nat.
327 (S O) < i \to i \le n \to i \ndivides S n!.
329 apply divides_b_false_to_not_divides.
330 apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
331 change with ((eqb ((S n!) \mod i) O) = false).
332 rewrite > mod_S_fact.simplify.reflexivity.
333 assumption.assumption.
337 definition prime : nat \to Prop \def
338 \lambda n:nat. (S O) < n \land
339 (\forall m:nat. m \divides n \to (S O) < m \to m = n).
341 theorem not_prime_O: \lnot (prime O).
342 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
345 theorem not_prime_SO: \lnot (prime (S O)).
346 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
349 (* smallest factor *)
350 definition smallest_factor : nat \to nat \def
356 [ O \Rightarrow (S O)
357 | (S q) \Rightarrow min_aux q (S(S q)) (\lambda m.(eqb ((S(S q)) \mod m) O))]].
360 theorem example1 : smallest_prime_factor (S(S(S O))) = (S(S(S O))).
361 normalize.reflexivity.
364 theorem example2: smallest_prime_factor (S(S(S(S O)))) = (S(S O)).
365 normalize.reflexivity.
368 theorem example3 : smallest_prime_factor (S(S(S(S(S(S(S O))))))) = (S(S(S(S(S(S(S O))))))).
369 simplify.reflexivity.
372 theorem lt_SO_smallest_factor:
373 \forall n:nat. (S O) < n \to (S O) < (smallest_factor n).
375 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
376 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
379 (S O < min_aux m1 (S(S m1)) (\lambda m.(eqb ((S(S m1)) \mod m) O))).
380 apply (lt_to_le_to_lt ? (S (S O))).
381 apply (le_n (S(S O))).
382 cut ((S(S O)) = (S(S m1)) - m1).
385 apply sym_eq.apply plus_to_minus.
386 rewrite < sym_plus.simplify.reflexivity.
389 theorem lt_O_smallest_factor: \forall n:nat. O < n \to O < (smallest_factor n).
391 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_n O H).
392 intro.apply (nat_case m).intro.
393 simplify.unfold lt.apply le_n.
394 intros.apply (trans_lt ? (S O)).
395 unfold lt.apply le_n.
396 apply lt_SO_smallest_factor.unfold lt. apply le_S_S.
397 apply le_S_S.apply le_O_n.
400 theorem divides_smallest_factor_n :
401 \forall n:nat. O < n \to smallest_factor n \divides n.
403 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O O H).
404 intro.apply (nat_case m).intro. simplify.
405 apply (witness ? ? (S O)). simplify.reflexivity.
407 apply divides_b_true_to_divides.
408 apply (lt_O_smallest_factor ? H).
410 (eqb ((S(S m1)) \mod (min_aux m1 (S(S m1))
411 (\lambda m.(eqb ((S(S m1)) \mod m) O)))) O = true).
412 apply f_min_aux_true.
413 apply (ex_intro nat ? (S(S m1))).
415 apply le_minus_m.apply le_n.
416 rewrite > mod_n_n.reflexivity.
417 apply (trans_lt ? (S O)).apply (le_n (S O)).unfold lt.
418 apply le_S_S.apply le_S_S.apply le_O_n.
421 theorem le_smallest_factor_n :
422 \forall n:nat. smallest_factor n \le n.
423 intro.apply (nat_case n).simplify.reflexivity.
424 intro.apply (nat_case m).simplify.reflexivity.
425 intro.apply divides_to_le.
426 unfold lt.apply le_S_S.apply le_O_n.
427 apply divides_smallest_factor_n.
428 unfold lt.apply le_S_S.apply le_O_n.
431 theorem lt_smallest_factor_to_not_divides: \forall n,i:nat.
432 (S O) < n \to (S O) < i \to i < (smallest_factor n) \to i \ndivides n.
434 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
435 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
437 apply divides_b_false_to_not_divides.
438 apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
439 change with ((eqb ((S(S m1)) \mod i) O) = false).
440 apply (lt_min_aux_to_false
441 (\lambda i:nat.eqb ((S(S m1)) \mod i) O) (S(S m1)) m1 i).
442 cut ((S(S O)) = (S(S m1)-m1)).
443 rewrite < Hcut.exact H1.
444 apply sym_eq. apply plus_to_minus.
445 rewrite < sym_plus.simplify.reflexivity.
449 theorem prime_smallest_factor_n :
450 \forall n:nat. (S O) < n \to prime (smallest_factor n).
451 intro. change with ((S(S O)) \le n \to (S O) < (smallest_factor n) \land
452 (\forall m:nat. m \divides smallest_factor n \to (S O) < m \to m = (smallest_factor n))).
454 apply lt_SO_smallest_factor.assumption.
456 cut (le m (smallest_factor n)).
457 elim (le_to_or_lt_eq m (smallest_factor n) Hcut).
458 absurd (m \divides n).
459 apply (transitive_divides m (smallest_factor n)).
461 apply divides_smallest_factor_n.
462 apply (trans_lt ? (S O)). unfold lt. apply le_n. exact H.
463 apply lt_smallest_factor_to_not_divides.
464 exact H.assumption.assumption.assumption.
466 apply (trans_lt O (S O)).
468 apply lt_SO_smallest_factor.
473 theorem prime_to_smallest_factor: \forall n. prime n \to
474 smallest_factor n = n.
475 intro.apply (nat_case n).intro.apply False_ind.apply (not_prime_O H).
476 intro.apply (nat_case m).intro.apply False_ind.apply (not_prime_SO H).
479 ((S O) < (S(S m1)) \land
480 (\forall m:nat. m \divides S(S m1) \to (S O) < m \to m = (S(S m1))) \to
481 smallest_factor (S(S m1)) = (S(S m1))).
482 intro.elim H.apply H2.
483 apply divides_smallest_factor_n.
484 apply (trans_lt ? (S O)).unfold lt. apply le_n.assumption.
485 apply lt_SO_smallest_factor.
489 (* a number n > O is prime iff its smallest factor is n *)
490 definition primeb \def \lambda n:nat.
492 [ O \Rightarrow false
495 [ O \Rightarrow false
496 | (S q) \Rightarrow eqb (smallest_factor (S(S q))) (S(S q))]].
499 theorem example4 : primeb (S(S(S O))) = true.
500 normalize.reflexivity.
503 theorem example5 : primeb (S(S(S(S(S(S O)))))) = false.
504 normalize.reflexivity.
507 theorem example6 : primeb (S(S(S(S((S(S(S(S(S(S(S O)))))))))))) = true.
508 normalize.reflexivity.
511 theorem example7 : primeb (S(S(S(S(S(S((S(S(S(S((S(S(S(S(S(S(S O))))))))))))))))))) = true.
512 normalize.reflexivity.
515 theorem primeb_to_Prop: \forall n.
517 [ true \Rightarrow prime n
518 | false \Rightarrow \lnot (prime n)].
520 apply (nat_case n).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
521 intro.apply (nat_case m).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
524 match eqb (smallest_factor (S(S m1))) (S(S m1)) with
525 [ true \Rightarrow prime (S(S m1))
526 | false \Rightarrow \lnot (prime (S(S m1)))].
527 apply (eqb_elim (smallest_factor (S(S m1))) (S(S m1))).
528 intro.change with (prime (S(S m1))).
530 apply prime_smallest_factor_n.
531 unfold lt.apply le_S_S.apply le_S_S.apply le_O_n.
532 intro.change with (\lnot (prime (S(S m1)))).
533 change with (prime (S(S m1)) \to False).
535 apply prime_to_smallest_factor.
539 theorem primeb_true_to_prime : \forall n:nat.
540 primeb n = true \to prime n.
543 [ true \Rightarrow prime n
544 | false \Rightarrow \lnot (prime n)].
546 apply primeb_to_Prop.
549 theorem primeb_false_to_not_prime : \forall n:nat.
550 primeb n = false \to \lnot (prime n).
553 [ true \Rightarrow prime n
554 | false \Rightarrow \lnot (prime n)].
556 apply primeb_to_Prop.
559 theorem decidable_prime : \forall n:nat.decidable (prime n).
560 intro.change with ((prime n) \lor \lnot (prime n)).
563 [ true \Rightarrow prime n
564 | false \Rightarrow \lnot (prime n)] \to (prime n) \lor \lnot (prime n)).
565 apply Hcut.apply primeb_to_Prop.
566 elim (primeb n).left.apply H.right.apply H.
569 theorem prime_to_primeb_true: \forall n:nat.
570 prime n \to primeb n = true.
572 cut (match (primeb n) with
573 [ true \Rightarrow prime n
574 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = true)).
575 apply Hcut.apply primeb_to_Prop.
576 elim (primeb n).reflexivity.
577 absurd (prime n).assumption.assumption.
580 theorem not_prime_to_primeb_false: \forall n:nat.
581 \lnot(prime n) \to primeb n = false.
583 cut (match (primeb n) with
584 [ true \Rightarrow prime n
585 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = false)).
586 apply Hcut.apply primeb_to_Prop.
588 absurd (prime n).assumption.assumption.