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_divides :
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_false_to_not_divides :
221 \forall n,m:nat. O < n \to
222 (divides_b n m = false ) \to n \ndivides m.
226 [ true \Rightarrow n \divides m
227 | false \Rightarrow n \ndivides m].
228 rewrite < H1.apply divides_b_to_Prop.
232 theorem decidable_divides: \forall n,m:nat.O < n \to
233 decidable (n \divides m).
234 intros.unfold decidable.
236 (match divides_b n m with
237 [ true \Rightarrow n \divides m
238 | false \Rightarrow n \ndivides m] \to n \divides m \lor n \ndivides m).
239 apply Hcut.apply divides_b_to_Prop.assumption.
240 elim (divides_b n m).left.apply H1.right.apply H1.
243 theorem divides_to_divides_b_true : \forall n,m:nat. O < n \to
244 n \divides m \to divides_b n m = true.
246 cut (match (divides_b n m) with
247 [ true \Rightarrow n \divides m
248 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = true)).
249 apply Hcut.apply divides_b_to_Prop.assumption.
250 elim (divides_b n m).reflexivity.
251 absurd (n \divides m).assumption.assumption.
254 theorem not_divides_to_divides_b_false: \forall n,m:nat. O < n \to
255 \lnot(n \divides m) \to (divides_b n m) = false.
257 cut (match (divides_b n m) with
258 [ true \Rightarrow n \divides m
259 | false \Rightarrow n \ndivides m] \to ((divides_b n m) = false)).
260 apply Hcut.apply divides_b_to_Prop.assumption.
261 elim (divides_b n m).
262 absurd (n \divides m).assumption.assumption.
267 theorem divides_f_pi_f : \forall f:nat \to nat.\forall n,m,i:nat.
268 m \le i \to i \le n+m \to f i \divides pi n f m.
269 intros 5.elim n.simplify.
270 cut (i = m).rewrite < Hcut.apply divides_n_n.
271 apply antisymmetric_le.assumption.assumption.
273 cut (i < S n1+m \lor i = S n1 + m).
275 apply (transitive_divides ? (pi n1 f m)).
276 apply H1.apply le_S_S_to_le. assumption.
277 apply (witness ? ? (f (S n1+m))).apply sym_times.
279 apply (witness ? ? (pi n1 f m)).reflexivity.
280 apply le_to_or_lt_eq.assumption.
284 theorem mod_S_pi: \forall f:nat \to nat.\forall n,i:nat.
285 i < n \to (S O) < (f i) \to (S (pi n f)) \mod (f i) = (S O).
286 intros.cut (pi n f) \mod (f i) = O.
288 apply mod_S.apply trans_lt O (S O).apply le_n (S O).assumption.
289 rewrite > Hcut.assumption.
290 apply divides_to_mod_O.apply trans_lt O (S O).apply le_n (S O).assumption.
291 apply divides_f_pi_f.assumption.
295 (* divides and fact *)
296 theorem divides_fact : \forall n,i:nat.
297 O < i \to i \le n \to i \divides n!.
298 intros 3.elim n.absurd (O<i).assumption.apply (le_n_O_elim i H1).
299 apply (not_le_Sn_O O).
300 change with (i \divides (S n1)*n1!).
301 apply (le_n_Sm_elim i n1 H2).
303 apply (transitive_divides ? n1!).
304 apply H1.apply le_S_S_to_le. assumption.
305 apply (witness ? ? (S n1)).apply sym_times.
308 apply (witness ? ? n1!).reflexivity.
311 theorem mod_S_fact: \forall n,i:nat.
312 (S O) < i \to i \le n \to (S n!) \mod i = (S O).
313 intros.cut (n! \mod i = O).
315 apply mod_S.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
316 rewrite > Hcut.assumption.
317 apply divides_to_mod_O.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
318 apply divides_fact.apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
322 theorem not_divides_S_fact: \forall n,i:nat.
323 (S O) < i \to i \le n \to i \ndivides S n!.
325 apply divides_b_false_to_not_divides.
326 apply (trans_lt O (S O)).apply (le_n (S O)).assumption.
328 rewrite > mod_S_fact.simplify.reflexivity.
329 assumption.assumption.
333 definition prime : nat \to Prop \def
334 \lambda n:nat. (S O) < n \land
335 (\forall m:nat. m \divides n \to (S O) < m \to m = n).
337 theorem not_prime_O: \lnot (prime O).
338 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
341 theorem not_prime_SO: \lnot (prime (S O)).
342 unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
345 (* smallest factor *)
346 definition smallest_factor : nat \to nat \def
352 [ O \Rightarrow (S O)
353 | (S q) \Rightarrow min_aux q (S(S q)) (\lambda m.(eqb ((S(S q)) \mod m) O))]].
356 theorem example1 : smallest_prime_factor (S(S(S O))) = (S(S(S O))).
357 normalize.reflexivity.
360 theorem example2: smallest_prime_factor (S(S(S(S O)))) = (S(S O)).
361 normalize.reflexivity.
364 theorem example3 : smallest_prime_factor (S(S(S(S(S(S(S O))))))) = (S(S(S(S(S(S(S O))))))).
365 simplify.reflexivity.
368 theorem lt_SO_smallest_factor:
369 \forall n:nat. (S O) < n \to (S O) < (smallest_factor n).
371 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
372 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
375 (S O < min_aux m1 (S(S m1)) (\lambda m.(eqb ((S(S m1)) \mod m) O))).
376 apply (lt_to_le_to_lt ? (S (S O))).
377 apply (le_n (S(S O))).
378 cut ((S(S O)) = (S(S m1)) - m1).
381 apply sym_eq.apply plus_to_minus.
382 rewrite < sym_plus.simplify.reflexivity.
385 theorem lt_O_smallest_factor: \forall n:nat. O < n \to O < (smallest_factor n).
387 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_n O H).
388 intro.apply (nat_case m).intro.
389 simplify.unfold lt.apply le_n.
390 intros.apply (trans_lt ? (S O)).
391 unfold lt.apply le_n.
392 apply lt_SO_smallest_factor.unfold lt. apply le_S_S.
393 apply le_S_S.apply le_O_n.
396 theorem divides_smallest_factor_n :
397 \forall n:nat. O < n \to smallest_factor n \divides n.
399 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O O H).
400 intro.apply (nat_case m).intro. simplify.
401 apply (witness ? ? (S O)). simplify.reflexivity.
403 apply divides_b_true_to_divides.
404 apply (lt_O_smallest_factor ? H).
406 (eqb ((S(S m1)) \mod (min_aux m1 (S(S m1))
407 (\lambda m.(eqb ((S(S m1)) \mod m) O)))) O = true).
408 apply f_min_aux_true.
409 apply (ex_intro nat ? (S(S m1))).
411 apply le_minus_m.apply le_n.
412 rewrite > mod_n_n.reflexivity.
413 apply (trans_lt ? (S O)).apply (le_n (S O)).unfold lt.
414 apply le_S_S.apply le_S_S.apply le_O_n.
417 theorem le_smallest_factor_n :
418 \forall n:nat. smallest_factor n \le n.
419 intro.apply (nat_case n).simplify.apply le_n.
420 intro.apply (nat_case m).simplify.apply le_n.
421 intro.apply divides_to_le.
422 unfold lt.apply le_S_S.apply le_O_n.
423 apply divides_smallest_factor_n.
424 unfold lt.apply le_S_S.apply le_O_n.
427 theorem lt_smallest_factor_to_not_divides: \forall n,i:nat.
428 (S O) < n \to (S O) < i \to i < (smallest_factor n) \to i \ndivides n.
430 apply (nat_case n).intro.apply False_ind.apply (not_le_Sn_O (S O) H).
431 intro.apply (nat_case m).intro. apply False_ind.apply (not_le_Sn_n (S O) H).
433 apply divides_b_false_to_not_divides.
434 apply (trans_lt O (S O)).apply (le_n (S O)).assumption.unfold divides_b.
435 apply (lt_min_aux_to_false
436 (\lambda i:nat.eqb ((S(S m1)) \mod i) O) (S(S m1)) m1 i).
437 cut ((S(S O)) = (S(S m1)-m1)).
438 rewrite < Hcut.exact H1.
439 apply sym_eq. apply plus_to_minus.
440 rewrite < sym_plus.simplify.reflexivity.
444 theorem prime_smallest_factor_n :
445 \forall n:nat. (S O) < n \to prime (smallest_factor n).
446 intro.change with ((S(S O)) \le n \to (S O) < (smallest_factor n) \land
447 (\forall m:nat. m \divides smallest_factor n \to (S O) < m \to m = (smallest_factor n))).
449 apply lt_SO_smallest_factor.assumption.
451 cut (le m (smallest_factor n)).
452 elim (le_to_or_lt_eq m (smallest_factor n) Hcut).
453 absurd (m \divides n).
454 apply (transitive_divides m (smallest_factor n)).
456 apply divides_smallest_factor_n.
457 apply (trans_lt ? (S O)). unfold lt. apply le_n. exact H.
458 apply lt_smallest_factor_to_not_divides.
459 exact H.assumption.assumption.assumption.
461 apply (trans_lt O (S O)).
463 apply lt_SO_smallest_factor.
468 theorem prime_to_smallest_factor: \forall n. prime n \to
469 smallest_factor n = n.
470 intro.apply (nat_case n).intro.apply False_ind.apply (not_prime_O H).
471 intro.apply (nat_case m).intro.apply False_ind.apply (not_prime_SO H).
474 ((S O) < (S(S m1)) \land
475 (\forall m:nat. m \divides S(S m1) \to (S O) < m \to m = (S(S m1))) \to
476 smallest_factor (S(S m1)) = (S(S m1))).
477 intro.elim H.apply H2.
478 apply divides_smallest_factor_n.
479 apply (trans_lt ? (S O)).unfold lt. apply le_n.assumption.
480 apply lt_SO_smallest_factor.
484 (* a number n > O is prime iff its smallest factor is n *)
485 definition primeb \def \lambda n:nat.
487 [ O \Rightarrow false
490 [ O \Rightarrow false
491 | (S q) \Rightarrow eqb (smallest_factor (S(S q))) (S(S q))]].
494 theorem example4 : primeb (S(S(S O))) = true.
495 normalize.reflexivity.
498 theorem example5 : primeb (S(S(S(S(S(S O)))))) = false.
499 normalize.reflexivity.
502 theorem example6 : primeb (S(S(S(S((S(S(S(S(S(S(S O)))))))))))) = true.
503 normalize.reflexivity.
506 theorem example7 : primeb (S(S(S(S(S(S((S(S(S(S((S(S(S(S(S(S(S O))))))))))))))))))) = true.
507 normalize.reflexivity.
510 theorem primeb_to_Prop: \forall n.
512 [ true \Rightarrow prime n
513 | false \Rightarrow \lnot (prime n)].
515 apply (nat_case n).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_O (S O) H1).
516 intro.apply (nat_case m).simplify.unfold Not.unfold prime.intro.elim H.apply (not_le_Sn_n (S O) H1).
519 match eqb (smallest_factor (S(S m1))) (S(S m1)) with
520 [ true \Rightarrow prime (S(S m1))
521 | false \Rightarrow \lnot (prime (S(S m1)))].
522 apply (eqb_elim (smallest_factor (S(S m1))) (S(S m1))).
525 apply prime_smallest_factor_n.
526 unfold lt.apply le_S_S.apply le_S_S.apply le_O_n.
528 change with (prime (S(S m1)) \to False).
530 apply prime_to_smallest_factor.
534 theorem primeb_true_to_prime : \forall n:nat.
535 primeb n = true \to prime n.
538 [ true \Rightarrow prime n
539 | false \Rightarrow \lnot (prime n)].
541 apply primeb_to_Prop.
544 theorem primeb_false_to_not_prime : \forall n:nat.
545 primeb n = false \to \lnot (prime n).
548 [ true \Rightarrow prime n
549 | false \Rightarrow \lnot (prime n)].
551 apply primeb_to_Prop.
554 theorem decidable_prime : \forall n:nat.decidable (prime n).
555 intro.unfold decidable.
558 [ true \Rightarrow prime n
559 | false \Rightarrow \lnot (prime n)] \to (prime n) \lor \lnot (prime n)).
560 apply Hcut.apply primeb_to_Prop.
561 elim (primeb n).left.apply H.right.apply H.
564 theorem prime_to_primeb_true: \forall n:nat.
565 prime n \to primeb n = true.
567 cut (match (primeb n) with
568 [ true \Rightarrow prime n
569 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = true)).
570 apply Hcut.apply primeb_to_Prop.
571 elim (primeb n).reflexivity.
572 absurd (prime n).assumption.assumption.
575 theorem not_prime_to_primeb_false: \forall n:nat.
576 \lnot(prime n) \to primeb n = false.
578 cut (match (primeb n) with
579 [ true \Rightarrow prime n
580 | false \Rightarrow \lnot (prime n)] \to ((primeb n) = false)).
581 apply Hcut.apply primeb_to_Prop.
583 absurd (prime n).assumption.assumption.