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/nth_prime".
17 include "nat/primes.ma".
18 include "nat/lt_arith.ma".
20 (* upper bound by Bertrand's conjecture. *)
21 (* Too difficult to prove.
22 let rec nth_prime n \def
24 [ O \Rightarrow (S(S O))
26 let previous_prime \def S (nth_prime p) in
27 min_aux previous_prime ((S(S O))*previous_prime) primeb].
29 theorem example8 : nth_prime (S(S O)) = (S(S(S(S(S O))))).
30 normalize.reflexivity.
33 theorem example9 : nth_prime (S(S(S O))) = (S(S(S(S(S(S(S O))))))).
34 normalize.reflexivity.
37 theorem example10 : nth_prime (S(S(S(S O)))) = (S(S(S(S(S(S(S(S(S(S(S O))))))))))).
38 normalize.reflexivity.
41 theorem smallest_factor_fact: \forall n:nat.
42 n < smallest_factor (S n!).
44 apply not_le_to_lt.unfold Not.
46 apply (not_divides_S_fact n (smallest_factor(S n!))).
47 apply lt_SO_smallest_factor.
48 unfold lt.apply le_S_S.apply le_SO_fact.
50 apply divides_smallest_factor_n.
51 unfold lt.apply le_S_S.apply le_O_n.
54 theorem ex_prime: \forall n. (S O) \le n \to \exists m.
55 n < m \land m \le S n! \land (prime m).
58 apply (ex_intro nat ? (S(S O))).
59 split.split.apply (le_n (S(S O))).
60 apply (le_n (S(S O))).apply (primeb_to_Prop (S(S O))).
61 apply (ex_intro nat ? (smallest_factor (S (S n1)!))).
63 apply smallest_factor_fact.
64 apply le_smallest_factor_n.
65 (* Andrea: ancora hint non lo trova *)
66 apply prime_smallest_factor_n.unfold lt.
67 apply le_S.apply le_SSO_fact.
68 unfold lt.apply le_S_S.assumption.
71 let rec nth_prime n \def
73 [ O \Rightarrow (S(S O))
75 let previous_prime \def (nth_prime p) in
76 let upper_bound \def S previous_prime! in
77 min_aux (upper_bound - (S previous_prime)) upper_bound primeb].
79 (* it works, but nth_prime 4 takes already a few minutes -
80 it must compute factorial of 7 ...
82 theorem example11 : nth_prime (S(S O)) = (S(S(S(S(S O))))).
83 normalize.reflexivity.
86 theorem example12: nth_prime (S(S(S O))) = (S(S(S(S(S(S(S O))))))).
87 normalize.reflexivity.
90 theorem example13 : nth_prime (S(S(S(S O)))) = (S(S(S(S(S(S(S(S(S(S(S O))))))))))).
91 normalize.reflexivity.
94 theorem prime_nth_prime : \forall n:nat.prime (nth_prime n).
96 apply (nat_case n).simplify.
97 apply (primeb_to_Prop (S(S O))).
100 (let previous_prime \def (nth_prime m) in
101 let upper_bound \def S previous_prime! in
102 prime (min_aux (upper_bound - (S previous_prime)) upper_bound primeb)).
103 apply primeb_true_to_prime.
104 apply f_min_aux_true.
105 apply (ex_intro nat ? (smallest_factor (S (nth_prime m)!))).
107 cut (S (nth_prime m)!-(S (nth_prime m)! - (S (nth_prime m))) = (S (nth_prime m))).
108 rewrite > Hcut.exact (smallest_factor_fact (nth_prime m)).
109 (* maybe we could factorize this proof *)
111 apply plus_minus_m_m.
114 apply le_smallest_factor_n.
115 apply prime_to_primeb_true.
116 apply prime_smallest_factor_n.unfold lt.
117 apply le_S_S.apply le_SO_fact.
120 (* properties of nth_prime *)
121 theorem increasing_nth_prime: increasing nth_prime.
125 (let previous_prime \def (nth_prime n) in
126 let upper_bound \def S previous_prime! in
127 (S previous_prime) \le min_aux (upper_bound - (S previous_prime)) upper_bound primeb).
129 cut (upper_bound - (upper_bound -(S previous_prime)) = (S previous_prime)).
130 rewrite < Hcut in \vdash (? % ?).
133 apply plus_minus_m_m.
138 variant lt_nth_prime_n_nth_prime_Sn :\forall n:nat.
139 (nth_prime n) < (nth_prime (S n)) \def increasing_nth_prime.
141 theorem injective_nth_prime: injective nat nat nth_prime.
142 apply increasing_to_injective.
143 apply increasing_nth_prime.
146 theorem lt_SO_nth_prime_n : \forall n:nat. (S O) \lt nth_prime n.
147 intros. elim n.unfold lt.apply le_n.
148 apply (trans_lt ? (nth_prime n1)).
149 assumption.apply lt_nth_prime_n_nth_prime_Sn.
152 theorem lt_O_nth_prime_n : \forall n:nat. O \lt nth_prime n.
153 intros.apply (trans_lt O (S O)).
154 unfold lt. apply le_n.apply lt_SO_nth_prime_n.
157 theorem ex_m_le_n_nth_prime_m:
158 \forall n: nat. nth_prime O \le n \to
159 \exists m. nth_prime m \le n \land n < nth_prime (S m).
161 apply increasing_to_le2.
162 exact lt_nth_prime_n_nth_prime_Sn.assumption.
165 theorem lt_nth_prime_to_not_prime: \forall n,m. nth_prime n < m \to m < nth_prime (S n)
168 apply primeb_false_to_not_prime.
169 letin previous_prime \def (nth_prime n).
170 letin upper_bound \def (S previous_prime!).
171 apply (lt_min_aux_to_false primeb upper_bound (upper_bound - (S previous_prime)) m).
172 cut (S (nth_prime n)!-(S (nth_prime n)! - (S (nth_prime n))) = (S (nth_prime n))).
173 rewrite > Hcut.assumption.
175 apply plus_minus_m_m.
181 (* nth_prime enumerates all primes *)
182 theorem prime_to_nth_prime : \forall p:nat. prime p \to
183 \exists i. nth_prime i = p.
185 cut (\exists m. nth_prime m \le p \land p < nth_prime (S m)).
187 cut (nth_prime a < p \lor nth_prime a = p).
191 apply (lt_nth_prime_to_not_prime a).assumption.assumption.
192 apply (ex_intro nat ? a).assumption.
193 apply le_to_or_lt_eq.assumption.
194 apply ex_m_le_n_nth_prime_m.
195 simplify.unfold prime in H.elim H.assumption.