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].
202 intros.unfold divides_b.
204 intro.simplify.apply mod_O_to_divides.assumption.assumption.
205 intro.simplify.unfold Not.intro.apply H1.apply divides_to_mod_O.assumption.assumption.
208 theorem divides_b_true_to_divides1:
209 \forall n,m:nat. O < n \to
210 (divides_b n m = true ) \to n \divides m.
214 [ true \Rightarrow n \divides m
215 | false \Rightarrow n \ndivides m].
216 rewrite < H1.apply divides_b_to_Prop.
220 theorem divides_b_true_to_divides:
221 \forall n,m:nat. divides_b n m = true \to n \divides m.
222 intros 2.apply (nat_case n)
224 [intro.apply divides_n_n
225 |simplify.intros.apply False_ind.
226 apply not_eq_true_false.apply sym_eq.assumption
229 apply divides_b_true_to_divides1
230 [apply lt_O_S|assumption]
234 theorem divides_b_false_to_not_divides1:
235 \forall n,m:nat. O < n \to
236 (divides_b n m = false ) \to n \ndivides m.
240 [ true \Rightarrow n \divides m
241 | false \Rightarrow n \ndivides m].
242 rewrite < H1.apply divides_b_to_Prop.
246 theorem divides_b_false_to_not_divides:
247 \forall n,m:nat. divides_b n m = false \to n \ndivides m.
248 intros 2.apply (nat_case n)
250 [simplify.unfold Not.intros.
251 apply not_eq_true_false.assumption
252 |unfold Not.intros.elim H1.
253 apply (not_eq_O_S m1).apply sym_eq.
257 apply divides_b_false_to_not_divides1
258 [apply lt_O_S|assumption]
262 theorem decidable_divides: \forall n,m:nat.O < n \to
263 decidable (n \divides m).
264 intros.unfold decidable.
266 (match divides_b n m with
267 [ true \Rightarrow n \divides m
268 | false \Rightarrow n \ndivides m] \to n \divides m \lor n \ndivides m).
269 apply Hcut.apply divides_b_to_Prop.assumption.
270 elim (divides_b n m).left.apply H1.right.apply H1.
273 theorem divides_to_divides_b_true : \forall n,m:nat. O < n \to
274 n \divides m \to divides_b n m = true.
276 cut (match (divides_b n m) with
277 [ true \Rightarrow n \divides m
278 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = true)).
279 apply Hcut.apply divides_b_to_Prop.assumption.
280 elim (divides_b n m).reflexivity.
281 absurd (n \divides m).assumption.assumption.
284 theorem not_divides_to_divides_b_false: \forall n,m:nat. O < n \to
285 \lnot(n \divides m) \to (divides_b n m) = false.
287 cut (match (divides_b n m) with
288 [ true \Rightarrow n \divides m
289 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = false)).
290 apply Hcut.apply divides_b_to_Prop.assumption.
291 elim (divides_b n m).
292 absurd (n \divides m).assumption.assumption.
296 theorem divides_b_true_to_lt_O: \forall n,m. O < n \to divides_b m n = true \to O < m.
298 elim (le_to_or_lt_eq ? ? (le_O_n m))
304 apply (lt_to_not_eq O n H).
306 apply eqb_true_to_eq.
312 theorem divides_f_pi_f : \forall f:nat \to nat.\forall n,m,i:nat.
313 m \le i \to i \le n+m \to f i \divides pi n f m.
314 intros 5.elim n.simplify.
315 cut (i = m).rewrite < Hcut.apply divides_n_n.
316 apply antisymmetric_le.assumption.assumption.
318 cut (i < S n1+m \lor i = S n1 + m).
320 apply (transitive_divides ? (pi n1 f m)).
321 apply H1.apply le_S_S_to_le. assumption.
322 apply (witness ? ? (f (S n1+m))).apply sym_times.
324 apply (witness ? ? (pi n1 f m)).reflexivity.
325 apply le_to_or_lt_eq.assumption.
329 theorem mod_S_pi: \forall f:nat \to nat.\forall n,i:nat.
330 i < n \to (S O) < (f i) \to (S (pi n f)) \mod (f i) = (S O).
331 intros.cut (pi n f) \mod (f i) = O.
333 apply mod_S.apply trans_lt O (S O).apply le_n (S O).assumption.
334 rewrite > Hcut.assumption.
335 apply divides_to_mod_O.apply trans_lt O (S O).apply le_n (S O).assumption.
336 apply divides_f_pi_f.assumption.
340 (* divides and fact *)
341 theorem divides_fact : \forall n,i:nat.
342 O < i \to i \le n \to i \divides n!.
343 intros 3.elim n.absurd (O<i).assumption.apply (le_n_O_elim i H1).
344 apply (not_le_Sn_O O).
345 change with (i \divides (S n1)*n1!).
346 apply (le_n_Sm_elim i n1 H2).
348 apply (transitive_divides ? n1!).
349 apply H1.apply le_S_S_to_le. assumption.
350 apply (witness ? ? (S n1)).apply sym_times.
353 apply (witness ? ? n1!).reflexivity.
356 theorem mod_S_fact: \forall n,i:nat.
357 (S O) < i \to i \le n \to (S n!) \mod i = (S O).
358 intros.cut (n! \mod i = O).
360 apply mod_S.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
361 rewrite > Hcut.assumption.
362 apply divides_to_mod_O.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
363 apply divides_fact.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
367 theorem not_divides_S_fact: \forall n,i:nat.
368 (S O) < i \to i \le n \to i \ndivides S n!.
370 apply divides_b_false_to_not_divides.
372 rewrite > mod_S_fact[simplify.reflexivity|assumption|assumption].
376 definition prime : nat \to Prop \def
377 \lambda n:nat. (S O) < n \land
378 (\forall m:nat. m \divides n \to (S O) < m \to m = n).
380 theorem not_prime_O: \lnot (prime O).
381 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
384 theorem not_prime_SO: \lnot (prime (S O)).
385 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
388 theorem prime_to_lt_O: \forall p. prime p \to O < p.
389 intros.elim H.apply lt_to_le.assumption.
392 (* smallest factor *)
393 definition smallest_factor : nat \to nat \def
399 [ O \Rightarrow (S O)
400 | (S q) \Rightarrow min_aux q (S (S O)) (\lambda m.(eqb ((S(S q)) \mod m) O))]].
403 theorem example1 : smallest_factor (S(S(S O))) = (S(S(S O))).
404 normalize.reflexivity.
407 theorem example2: smallest_factor (S(S(S(S O)))) = (S(S O)).
408 normalize.reflexivity.
411 theorem example3 : smallest_factor (S(S(S(S(S(S(S O))))))) = (S(S(S(S(S(S(S O))))))).
412 simplify.reflexivity.
415 theorem lt_SO_smallest_factor:
416 \forall n:nat. (S O) < n \to (S O) < (smallest_factor n).
418 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
419 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
422 (S O < min_aux m1 (S (S O)) (\lambda m.(eqb ((S(S m1)) \mod m) O))).
423 apply (lt_to_le_to_lt ? (S (S O))).
424 apply (le_n (S(S O))).
425 cut ((S(S O)) = (S(S m1)) - m1).
428 apply sym_eq.apply plus_to_minus.
429 rewrite < sym_plus.simplify.reflexivity.
432 theorem lt_O_smallest_factor: \forall n:nat. O < n \to O < (smallest_factor n).
434 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_n O H).
435 intro.apply (nat_case m).intro.
436 simplify.unfold lt.apply le_n.
437 intros.apply (trans_lt ? (S O)).
438 unfold lt.apply le_n.
439 apply lt_SO_smallest_factor.unfold lt. apply le_S_S.
440 apply le_S_S.apply le_O_n.
443 theorem divides_smallest_factor_n :
444 \forall n:nat. O < n \to smallest_factor n \divides n.
446 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O O H).
447 intro.apply (nat_case m).intro. simplify.
448 apply (witness ? ? (S O)). simplify.reflexivity.
450 apply divides_b_true_to_divides.
452 (eqb ((S(S m1)) \mod (min_aux m1 (S (S O))
453 (\lambda m.(eqb ((S(S m1)) \mod m) O)))) O = true).
454 apply f_min_aux_true.
455 apply (ex_intro nat ? (S(S m1))).
457 apply (le_S_S_to_le (S (S O)) (S (S m1)) ?).
458 apply (minus_le_O_to_le (S (S (S O))) (S (S (S m1))) ?).
460 rewrite < sym_plus. simplify. apply le_n.
461 apply (eq_to_eqb_true (mod (S (S m1)) (S (S m1))) O ?).
462 apply (mod_n_n (S (S m1)) ?).
466 theorem le_smallest_factor_n :
467 \forall n:nat. smallest_factor n \le n.
468 intro.apply (nat_case n).simplify.apply le_n.
469 intro.apply (nat_case m).simplify.apply le_n.
470 intro.apply divides_to_le.
471 unfold lt.apply le_S_S.apply le_O_n.
472 apply divides_smallest_factor_n.
473 unfold lt.apply le_S_S.apply le_O_n.
476 theorem lt_smallest_factor_to_not_divides: \forall n,i:nat.
477 (S O) < n \to (S O) < i \to i < (smallest_factor n) \to i \ndivides n.
479 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
480 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
482 apply divides_b_false_to_not_divides.
483 apply (lt_min_aux_to_false
484 (\lambda i:nat.eqb ((S(S m1)) \mod i) O) (S (S O)) m1 i).
489 theorem prime_smallest_factor_n :
490 \forall n:nat. (S O) < n \to prime (smallest_factor n).
491 intro.change with ((S(S O)) \le n \to (S O) < (smallest_factor n) \land
492 (\forall m:nat. m \divides smallest_factor n \to (S O) < m \to m = (smallest_factor n))).
494 apply lt_SO_smallest_factor.assumption.
496 cut (le m (smallest_factor n)).
497 elim (le_to_or_lt_eq m (smallest_factor n) Hcut).
498 absurd (m \divides n).
499 apply (transitive_divides m (smallest_factor n)).
501 apply divides_smallest_factor_n.
502 apply (trans_lt ? (S O)). unfold lt. apply le_n. exact H.
503 apply lt_smallest_factor_to_not_divides.
504 exact H.assumption.assumption.assumption.
506 apply (trans_lt O (S O)).
508 apply lt_SO_smallest_factor.
513 theorem prime_to_smallest_factor: \forall n. prime n \to
514 smallest_factor n = n.
515 intro.apply (nat_case n).intro.apply False_ind.apply (not_prime_O H).
516 intro.apply (nat_case m).intro.apply False_ind.apply (not_prime_SO H).
519 ((S O) < (S(S m1)) \land
520 (\forall m:nat. m \divides S(S m1) \to (S O) < m \to m = (S(S m1))) \to
521 smallest_factor (S(S m1)) = (S(S m1))).
522 intro.elim H.apply H2.
523 apply divides_smallest_factor_n.
524 apply (trans_lt ? (S O)).unfold lt. apply le_n.assumption.
525 apply lt_SO_smallest_factor.
529 (* a number n > O is prime iff its smallest factor is n *)
530 definition primeb \def \lambda n:nat.
532 [ O \Rightarrow false
535 [ O \Rightarrow false
536 | (S q) \Rightarrow eqb (smallest_factor (S(S q))) (S(S q))]].
539 theorem example4 : primeb (S(S(S O))) = true.
540 normalize.reflexivity.
543 theorem example5 : primeb (S(S(S(S(S(S O)))))) = false.
544 normalize.reflexivity.
547 theorem example6 : primeb (S(S(S(S((S(S(S(S(S(S(S O)))))))))))) = true.
548 normalize.reflexivity.
551 theorem example7 : primeb (S(S(S(S(S(S((S(S(S(S((S(S(S(S(S(S(S O))))))))))))))))))) = true.
552 normalize.reflexivity.
555 theorem primeb_to_Prop: \forall n.
557 [ true \Rightarrow prime n
558 | false \Rightarrow \lnot (prime n)].
560 apply (nat_case n).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
561 intro.apply (nat_case m).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
564 match eqb (smallest_factor (S(S m1))) (S(S m1)) with
565 [ true \Rightarrow prime (S(S m1))
566 | false \Rightarrow \lnot (prime (S(S m1)))].
567 apply (eqb_elim (smallest_factor (S(S m1))) (S(S m1))).
570 apply prime_smallest_factor_n.
571 unfold lt.apply le_S_S.apply le_S_S.apply le_O_n.
573 change with (prime (S(S m1)) \to False).
575 apply prime_to_smallest_factor.
579 theorem primeb_true_to_prime : \forall n:nat.
580 primeb n = true \to prime n.
583 [ true \Rightarrow prime n
584 | false \Rightarrow \lnot (prime n)].
586 apply primeb_to_Prop.
589 theorem primeb_false_to_not_prime : \forall n:nat.
590 primeb n = false \to \lnot (prime n).
593 [ true \Rightarrow prime n
594 | false \Rightarrow \lnot (prime n)].
596 apply primeb_to_Prop.
599 theorem decidable_prime : \forall n:nat.decidable (prime n).
600 intro.unfold decidable.
603 [ true \Rightarrow prime n
604 | false \Rightarrow \lnot (prime n)] \to (prime n) \lor \lnot (prime n)).
605 apply Hcut.apply primeb_to_Prop.
606 elim (primeb n).left.apply H.right.apply H.
609 theorem prime_to_primeb_true: \forall n:nat.
610 prime n \to primeb n = true.
612 cut (match (primeb n) with
613 [ true \Rightarrow prime n
614 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = true)).
615 apply Hcut.apply primeb_to_Prop.
616 elim (primeb n).reflexivity.
617 absurd (prime n).assumption.assumption.
620 theorem not_prime_to_primeb_false: \forall n:nat.
621 \lnot(prime n) \to primeb n = false.
623 cut (match (primeb n) with
624 [ true \Rightarrow prime n
625 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = false)).
626 apply Hcut.apply primeb_to_Prop.
628 absurd (prime n).assumption.assumption.