]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/nth_prime.ma
.ma inclusions corrected/minimized
[helm.git] / helm / matita / library / nat / nth_prime.ma
1 (**************************************************************************)
2 (*       ___                                                                *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        Matita is distributed under the terms of the          *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/nth_prime".
16
17 include "nat/primes.ma".
18 include "nat/lt_arith.ma".
19
20 (* upper bound by Bertrand's conjecture. *)
21 (* Too difficult to prove.        
22 let rec nth_prime n \def
23 match n with
24   [ O \Rightarrow (S(S O))
25   | (S p) \Rightarrow
26     let previous_prime \def S (nth_prime p) in
27     min_aux previous_prime ((S(S O))*previous_prime) primeb].
28
29 theorem example8 : nth_prime (S(S O)) = (S(S(S(S(S O))))).
30 normalize.reflexivity.
31 qed.
32
33 theorem example9 : nth_prime (S(S(S O))) = (S(S(S(S(S(S(S O))))))).
34 normalize.reflexivity.
35 qed.
36
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.
39 qed. *)
40
41 theorem smallest_factor_fact: \forall n:nat.
42 n < smallest_factor (S (fact n)).
43 intros.
44 apply not_le_to_lt.
45 change with smallest_factor (S (fact n)) \le n \to False.intro.
46 apply not_divides_S_fact n (smallest_factor(S (fact n))) ? ?.
47 apply lt_SO_smallest_factor.
48 simplify.apply le_S_S.apply le_SO_fact.
49 assumption.
50 apply divides_smallest_factor_n.
51 simplify.apply le_S_S.apply le_O_n.
52 qed.
53
54 (* mi sembra che il problem sia ex *)
55 theorem ex_prime: \forall n. (S O) \le n \to ex nat (\lambda m.
56 n < m \land m \le (S (fact n)) \land (prime m)).
57 intros.
58 elim H.
59 apply ex_intro nat ? (S(S O)).
60 split.split.apply le_n (S(S O)).
61 apply le_n (S(S O)).apply primeb_to_Prop (S(S O)).
62 apply ex_intro nat ? (smallest_factor (S (fact (S n1)))).
63 split.split.
64 apply smallest_factor_fact.
65 apply le_smallest_factor_n.
66 (* ancora hint non lo trova *)
67 apply prime_smallest_factor_n.
68 change with (S(S O)) \le S (fact (S n1)).
69 apply le_S.apply le_SSO_fact.
70 simplify.apply le_S_S.assumption.
71 qed.
72
73 let rec nth_prime n \def
74 match n with
75   [ O \Rightarrow (S(S O))
76   | (S p) \Rightarrow
77     let previous_prime \def (nth_prime p) in
78     let upper_bound \def S (fact previous_prime) in
79     min_aux (upper_bound - (S previous_prime)) upper_bound primeb].
80     
81 (* it works, but nth_prime 4 takes already a few minutes -
82 it must compute factorial of 7 ...
83
84 theorem example11 : nth_prime (S(S O)) = (S(S(S(S(S O))))).
85 normalize.reflexivity.
86 qed.
87
88 theorem example12: nth_prime (S(S(S O))) = (S(S(S(S(S(S(S O))))))).
89 normalize.reflexivity.
90 qed.
91
92 theorem example13 : nth_prime (S(S(S(S O)))) = (S(S(S(S(S(S(S(S(S(S(S O))))))))))).
93 normalize.reflexivity.
94 *) 
95
96 theorem prime_nth_prime : \forall n:nat.prime (nth_prime n).
97 intro.
98 apply nat_case n.
99 change with prime (S(S O)).
100 apply primeb_to_Prop (S(S O)).
101 intro.
102 (* ammirare la resa del letin !! *)
103 change with
104 let previous_prime \def (nth_prime m) in
105 let upper_bound \def S (fact previous_prime) in
106 prime (min_aux (upper_bound - (S previous_prime)) upper_bound primeb).
107 apply primeb_true_to_prime.
108 apply f_min_aux_true.
109 apply ex_intro nat ? (smallest_factor (S (fact (nth_prime m)))).
110 split.split.
111 cut S (fact (nth_prime m))-(S (fact (nth_prime m)) - (S (nth_prime m))) = (S (nth_prime m)).
112 rewrite > Hcut.exact smallest_factor_fact (nth_prime m).
113 (* maybe we could factorize this proof *)
114 apply plus_to_minus.
115 apply le_minus_m.
116 apply plus_minus_m_m.
117 apply le_S_S.
118 apply le_n_fact_n.
119 apply le_smallest_factor_n.
120 apply prime_to_primeb_true.
121 apply prime_smallest_factor_n.
122 change with (S(S O)) \le S (fact (nth_prime m)).
123 apply le_S_S.apply le_SO_fact.
124 qed.
125
126 (* properties of nth_prime *)
127 theorem increasing_nth_prime: increasing nth_prime.
128 change with \forall n:nat. (nth_prime n) < (nth_prime (S n)).
129 intros.
130 change with
131 let previous_prime \def (nth_prime n) in
132 let upper_bound \def S (fact previous_prime) in
133 (S previous_prime) \le min_aux (upper_bound - (S previous_prime)) upper_bound primeb.
134 intros.
135 cut upper_bound - (upper_bound -(S previous_prime)) = (S previous_prime).
136 rewrite < Hcut in \vdash (? % ?).
137 apply le_min_aux.
138 apply plus_to_minus.
139 apply le_minus_m.
140 apply plus_minus_m_m.
141 apply le_S_S.
142 apply le_n_fact_n.
143 qed.
144
145 variant lt_nth_prime_n_nth_prime_Sn :\forall n:nat. 
146 (nth_prime n) < (nth_prime (S n)) \def increasing_nth_prime.
147
148 theorem injective_nth_prime: injective nat nat nth_prime.
149 apply increasing_to_injective.
150 apply increasing_nth_prime.
151 qed.
152
153 theorem lt_SO_nth_prime_n : \forall n:nat. (S O) \lt nth_prime n.
154 intros. elim n.simplify.apply le_n.
155 apply trans_lt ? (nth_prime n1).
156 assumption.apply lt_nth_prime_n_nth_prime_Sn.
157 qed.
158
159 theorem lt_O_nth_prime_n : \forall n:nat. O \lt nth_prime n.
160 intros.apply trans_lt O (S O).
161 simplify. apply le_n.apply lt_SO_nth_prime_n.
162 qed.
163
164 theorem ex_m_le_n_nth_prime_m: 
165 \forall n: nat. nth_prime O \le n \to 
166 ex nat (\lambda m. nth_prime m \le n \land n < nth_prime (S m)).
167 intros.
168 apply increasing_to_le2.
169 exact lt_nth_prime_n_nth_prime_Sn.assumption.
170 qed.
171
172 theorem lt_nth_prime_to_not_prime: \forall n,m. nth_prime n < m \to m < nth_prime (S n) 
173 \to \not (prime m).
174 intros.
175 apply primeb_false_to_not_prime.
176 letin previous_prime \def nth_prime n.
177 letin upper_bound \def S (fact previous_prime).
178 apply lt_min_aux_to_false primeb upper_bound (upper_bound - (S previous_prime)) m.
179 cut S (fact (nth_prime n))-(S (fact (nth_prime n)) - (S (nth_prime n))) = (S (nth_prime n)).
180 rewrite > Hcut.assumption.
181 apply plus_to_minus.
182 apply le_minus_m.
183 apply plus_minus_m_m.
184 apply le_S_S.
185 apply le_n_fact_n.
186 assumption.
187 qed.
188
189 (* nth_prime enumerates all primes *)
190 theorem prime_to_nth_prime : \forall p:nat. prime p \to
191 ex nat (\lambda i:nat. nth_prime i = p).
192 intros.
193 cut ex nat (\lambda m. nth_prime m \le p \land p < nth_prime (S m)).
194 elim Hcut.elim H1.
195 cut nth_prime a < p \lor nth_prime a = p.
196 elim Hcut1.
197 absurd (prime p).
198 assumption.
199 apply lt_nth_prime_to_not_prime a.assumption.assumption.
200 apply ex_intro nat ? a.assumption.
201 apply le_to_or_lt_eq.assumption.
202 apply ex_m_le_n_nth_prime_m.
203 simplify.simplify in H.elim H.assumption.
204 qed.
205