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/factorization".
19 include "nat/nth_prime.ma".
21 (* the following factorization algorithm looks for the largest prime
23 definition max_prime_factor \def \lambda n:nat.
24 (max n (\lambda p:nat.eqb (n \mod (nth_prime p)) O)).
26 (* max_prime_factor is indeed a factor *)
27 theorem divides_max_prime_factor_n:
28 \forall n:nat. (S O) < n
29 \to nth_prime (max_prime_factor n) \divides n.
31 apply divides_b_true_to_divides.
32 apply (f_max_true (\lambda p:nat.eqb (n \mod (nth_prime p)) O) n);
33 cut (\exists i. nth_prime i = smallest_factor n);
35 apply (ex_intro nat ? a);
37 [ apply (trans_le a (nth_prime a));
39 exact lt_nth_prime_n_nth_prime_Sn;
41 apply le_smallest_factor_n; ]
43 (*CSC: simplify here does something nasty! *)
44 change with (divides_b (smallest_factor n) n = true);
45 apply divides_to_divides_b_true;
46 [ apply (trans_lt ? (S O));
47 [ unfold lt; apply le_n;
48 | apply lt_SO_smallest_factor; assumption; ]
49 | letin x \def le.auto new.
51 apply divides_smallest_factor_n;
52 apply (trans_lt ? (S O));
53 [ unfold lt; apply le_n;
54 | assumption; ] *) ] ]
57 apply prime_to_nth_prime;
58 apply prime_smallest_factor_n;
62 theorem divides_to_max_prime_factor : \forall n,m. (S O) < n \to O < m \to n \divides m \to
63 max_prime_factor n \le max_prime_factor m.
64 intros.unfold max_prime_factor.
67 apply le_max_n.apply divides_to_le.assumption.assumption.
68 change with (divides_b (nth_prime (max_prime_factor n)) m = true).
69 apply divides_to_divides_b_true.
70 cut (prime (nth_prime (max_prime_factor n))).
71 apply lt_O_nth_prime_n.apply prime_nth_prime.
72 cut (nth_prime (max_prime_factor n) \divides n).
76 [ apply (transitive_divides ? n);
77 [ apply divides_max_prime_factor_n.
81 | apply divides_b_true_to_divides;
82 [ apply lt_O_nth_prime_n.
83 | apply divides_to_divides_b_true;
84 [ apply lt_O_nth_prime_n.
85 | apply divides_max_prime_factor_n.
93 theorem divides_to_max_prime_factor1 : \forall n,m. O < n \to O < m \to n \divides m \to
94 max_prime_factor n \le max_prime_factor m.
96 elim (le_to_or_lt_eq ? ? H)
97 [apply divides_to_max_prime_factor
98 [assumption|assumption|assumption]
100 simplify.apply le_O_n.
104 theorem p_ord_to_lt_max_prime_factor: \forall n,p,q,r. O < n \to
105 p = max_prime_factor n \to
106 (pair nat nat q r) = p_ord n (nth_prime p) \to
107 (S O) < r \to max_prime_factor r < p.
110 cut (max_prime_factor r \lt max_prime_factor n \lor
111 max_prime_factor r = max_prime_factor n).
112 elim Hcut.assumption.
113 absurd (nth_prime (max_prime_factor n) \divides r).
115 apply divides_max_prime_factor_n.
116 assumption.unfold Not.
118 cut (r \mod (nth_prime (max_prime_factor n)) \neq O);
119 [unfold Not in Hcut1.auto new.
121 apply Hcut1.apply divides_to_mod_O;
122 [ apply lt_O_nth_prime_n.
127 cut(pair nat nat q r=p_ord_aux n n (nth_prime (max_prime_factor n)));
128 [2: rewrite < H1.assumption.|letin x \def le.auto width = 4 new]
129 (* CERCA COME MAI le_n non lo applica se lo trova come Const e non Rel *)
132 apply (p_ord_aux_to_not_mod_O n n ? q r);
133 [ apply lt_SO_nth_prime_n.
136 | rewrite < H1.assumption.
140 apply (le_to_or_lt_eq (max_prime_factor r) (max_prime_factor n)).
141 apply divides_to_max_prime_factor.
142 assumption.assumption.
143 apply (witness r n ((nth_prime p) \sup q)).
145 apply (p_ord_aux_to_exp n n ? q r).
146 apply lt_O_nth_prime_n.assumption.
149 theorem p_ord_to_lt_max_prime_factor1: \forall n,p,q,r. O < n \to
150 max_prime_factor n \le p \to
151 (pair nat nat q r) = p_ord n (nth_prime p) \to
152 (S O) < r \to max_prime_factor r < p.
154 cut (max_prime_factor n < p \lor max_prime_factor n = p).
155 elim Hcut.apply (le_to_lt_to_lt ? (max_prime_factor n)).
156 apply divides_to_max_prime_factor.assumption.assumption.
157 apply (witness r n ((nth_prime p) \sup q)).
159 apply (p_ord_aux_to_exp n n).
160 apply lt_O_nth_prime_n.
161 assumption.assumption.
162 apply (p_ord_to_lt_max_prime_factor n ? q).
163 assumption.apply sym_eq.assumption.assumption.assumption.
164 apply (le_to_or_lt_eq ? p H1).
167 (* datatypes and functions *)
169 inductive nat_fact : Set \def
170 nf_last : nat \to nat_fact
171 | nf_cons : nat \to nat_fact \to nat_fact.
173 inductive nat_fact_all : Set \def
174 nfa_zero : nat_fact_all
175 | nfa_one : nat_fact_all
176 | nfa_proper : nat_fact \to nat_fact_all.
178 let rec factorize_aux p n acc \def
182 match p_ord n (nth_prime p1) with
183 [ (pair q r) \Rightarrow
184 factorize_aux p1 r (nf_cons q acc)]].
186 definition factorize : nat \to nat_fact_all \def \lambda n:nat.
188 [ O \Rightarrow nfa_zero
191 [ O \Rightarrow nfa_one
193 let p \def (max (S(S n2)) (\lambda p:nat.eqb ((S(S n2)) \mod (nth_prime p)) O)) in
194 match p_ord (S(S n2)) (nth_prime p) with
195 [ (pair q r) \Rightarrow
196 nfa_proper (factorize_aux p r (nf_last (pred q)))]]].
198 let rec defactorize_aux f i \def
200 [ (nf_last n) \Rightarrow (nth_prime i) \sup (S n)
201 | (nf_cons n g) \Rightarrow
202 (nth_prime i) \sup n *(defactorize_aux g (S i))].
204 definition defactorize : nat_fact_all \to nat \def
205 \lambda f : nat_fact_all.
207 [ nfa_zero \Rightarrow O
208 | nfa_one \Rightarrow (S O)
209 | (nfa_proper g) \Rightarrow defactorize_aux g O].
211 theorem lt_O_defactorize_aux:
214 O < defactorize_aux f i.
218 rewrite > times_n_SO;
220 [ change with (O < nth_prime i);
221 apply lt_O_nth_prime_n;
223 change with (O < exp (nth_prime i) n);
225 apply lt_O_nth_prime_n;
226 | change with (O < defactorize_aux n1 (S i));
230 theorem lt_SO_defactorize_aux: \forall f:nat_fact.\forall i:nat.
231 S O < defactorize_aux f i.
232 intro.elim f.simplify.unfold lt.
233 rewrite > times_n_SO.
235 change with (S O < nth_prime i).
236 apply lt_SO_nth_prime_n.
237 change with (O < exp (nth_prime i) n).
239 apply lt_O_nth_prime_n.
241 rewrite > times_n_SO.
244 change with (O < exp (nth_prime i) n).
246 apply lt_O_nth_prime_n.
247 change with (S O < defactorize_aux n1 (S i)).
251 theorem defactorize_aux_factorize_aux :
252 \forall p,n:nat.\forall acc:nat_fact.O < n \to
253 ((n=(S O) \land p=O) \lor max_prime_factor n < p) \to
254 defactorize_aux (factorize_aux p n acc) O = n*(defactorize_aux acc p).
255 intro.elim p.simplify.
256 elim H1.elim H2.rewrite > H3.
257 rewrite > sym_times. apply times_n_SO.
258 apply False_ind.apply (not_le_Sn_O (max_prime_factor n) H2).
260 (* generalizing the goal: I guess there exists a better way *)
261 cut (\forall q,r.(pair nat nat q r) = (p_ord_aux n1 n1 (nth_prime n)) \to
262 defactorize_aux match (p_ord_aux n1 n1 (nth_prime n)) with
263 [(pair q r) \Rightarrow (factorize_aux n r (nf_cons q acc))] O =
264 n1*defactorize_aux acc (S n)).
265 apply (Hcut (fst ? ? (p_ord_aux n1 n1 (nth_prime n)))
266 (snd ? ? (p_ord_aux n1 n1 (nth_prime n)))).
267 apply sym_eq.apply eq_pair_fst_snd.
271 cut (n1 = r * (nth_prime n) \sup q).
273 simplify.rewrite < assoc_times.
274 rewrite < Hcut.reflexivity.
275 cut (O < r \lor O = r).
276 elim Hcut1.assumption.absurd (n1 = O).
277 rewrite > Hcut.rewrite < H4.reflexivity.
278 unfold Not. intro.apply (not_le_Sn_O O).
279 rewrite < H5 in \vdash (? ? %).assumption.
280 apply le_to_or_lt_eq.apply le_O_n.
281 cut ((S O) < r \lor (S O) \nlt r).
284 apply (p_ord_to_lt_max_prime_factor1 n1 ? q r).
288 apply (not_eq_O_S n).apply sym_eq.assumption.
291 assumption.assumption.
294 left.split.assumption.reflexivity.
295 intro.right.rewrite > Hcut2.
296 simplify.unfold lt.apply le_S_S.apply le_O_n.
297 cut (r < (S O) ∨ r=(S O)).
298 elim Hcut2.absurd (O=r).
299 apply le_n_O_to_eq.apply le_S_S_to_le.exact H5.
302 apply (not_le_Sn_O O).
303 rewrite > Hcut3 in ⊢ (? ? %).
304 assumption.rewrite > Hcut.
305 rewrite < H6.reflexivity.
307 apply (le_to_or_lt_eq r (S O)).
308 apply not_lt_to_le.assumption.
309 apply (decidable_lt (S O) r).
311 apply (p_ord_aux_to_exp n1 n1).
312 apply lt_O_nth_prime_n.assumption.
315 theorem defactorize_factorize: \forall n:nat.defactorize (factorize n) = n.
317 apply (nat_case n).reflexivity.
318 intro.apply (nat_case m).reflexivity.
319 intro.(*CSC: simplify here does something really nasty *)
321 (let p \def (max (S(S m1)) (\lambda p:nat.eqb ((S(S m1)) \mod (nth_prime p)) O)) in
322 defactorize (match p_ord (S(S m1)) (nth_prime p) with
323 [ (pair q r) \Rightarrow
324 nfa_proper (factorize_aux p r (nf_last (pred q)))])=(S(S m1))).
326 (* generalizing the goal; find a better way *)
327 cut (\forall q,r.(pair nat nat q r) = (p_ord (S(S m1)) (nth_prime p)) \to
328 defactorize (match p_ord (S(S m1)) (nth_prime p) with
329 [ (pair q r) \Rightarrow
330 nfa_proper (factorize_aux p r (nf_last (pred q)))])=(S(S m1))).
331 apply (Hcut (fst ? ? (p_ord (S(S m1)) (nth_prime p)))
332 (snd ? ? (p_ord (S(S m1)) (nth_prime p)))).
333 apply sym_eq.apply eq_pair_fst_snd.
337 cut ((S(S m1)) = (nth_prime p) \sup q *r).
339 rewrite > defactorize_aux_factorize_aux.
340 (*CSC: simplify here does something really nasty *)
341 change with (r*(nth_prime p) \sup (S (pred q)) = (S(S m1))).
342 cut ((S (pred q)) = q).
346 apply (p_ord_aux_to_exp (S(S m1))).
347 apply lt_O_nth_prime_n.
350 apply sym_eq. apply S_pred.
351 cut (O < q \lor O = q).
352 elim Hcut2.assumption.
353 absurd (nth_prime p \divides S (S m1)).
354 apply (divides_max_prime_factor_n (S (S m1))).
355 unfold lt.apply le_S_S.apply le_S_S. apply le_O_n.
357 rewrite > Hcut3 in \vdash (? (? ? %)).
358 (*CSC: simplify here does something really nasty *)
359 change with (nth_prime p \divides r \to False).
361 apply (p_ord_aux_to_not_mod_O (S(S m1)) (S(S m1)) (nth_prime p) q r).
362 apply lt_SO_nth_prime_n.
363 unfold lt.apply le_S_S.apply le_O_n.apply le_n.
365 apply divides_to_mod_O.apply lt_O_nth_prime_n.assumption.
366 rewrite > times_n_SO in \vdash (? ? ? %).
368 rewrite > (exp_n_O (nth_prime p)).
369 rewrite > H1 in \vdash (? ? ? (? (? ? %) ?)).
371 apply le_to_or_lt_eq.apply le_O_n.assumption.
372 (* e adesso l'ultimo goal. TASSI: che ora non e' piu' l'ultimo :P *)
373 cut ((S O) < r \lor S O \nlt r).
376 apply (p_ord_to_lt_max_prime_factor1 (S(S m1)) ? q r).
377 unfold lt.apply le_S_S. apply le_O_n.
379 assumption.assumption.
382 left.split.assumption.reflexivity.
383 intro.right.rewrite > Hcut3.
384 simplify.unfold lt.apply le_S_S.apply le_O_n.
385 cut (r \lt (S O) \or r=(S O)).
386 elim Hcut3.absurd (O=r).
387 apply le_n_O_to_eq.apply le_S_S_to_le.exact H2.
389 apply (not_le_Sn_O O).
390 rewrite > H3 in \vdash (? ? %).assumption.assumption.
391 apply (le_to_or_lt_eq r (S O)).
392 apply not_lt_to_le.assumption.
393 apply (decidable_lt (S O) r).
395 cut (O < r \lor O = r).
396 elim Hcut1.assumption.
398 apply (not_eq_O_S (S m1)).
399 rewrite > Hcut.rewrite < H1.rewrite < times_n_O.reflexivity.
400 apply le_to_or_lt_eq.apply le_O_n.
403 apply (p_ord_aux_to_exp (S(S m1))).
404 apply lt_O_nth_prime_n.
411 [ (nf_last n) \Rightarrow O
412 | (nf_cons n g) \Rightarrow S (max_p g)].
414 let rec max_p_exponent f \def
416 [ (nf_last n) \Rightarrow n
417 | (nf_cons n g) \Rightarrow max_p_exponent g].
419 theorem divides_max_p_defactorize: \forall f:nat_fact.\forall i:nat.
420 nth_prime ((max_p f)+i) \divides defactorize_aux f i.
422 elim f.simplify.apply (witness ? ? ((nth_prime i) \sup n)).
425 (nth_prime (S(max_p n1)+i) \divides
426 (nth_prime i) \sup n *(defactorize_aux n1 (S i))).
430 rewrite > assoc_times.
432 apply (witness ? ? (n2* (nth_prime i) \sup n)).
436 theorem divides_exp_to_divides:
437 \forall p,n,m:nat. prime p \to
438 p \divides n \sup m \to p \divides n.
439 intros 3.elim m.simplify in H1.
440 apply (transitive_divides p (S O)).assumption.
442 cut (p \divides n \lor p \divides n \sup n1).
443 elim Hcut.assumption.
444 apply H.assumption.assumption.
445 apply divides_times_to_divides.assumption.
449 theorem divides_exp_to_eq:
450 \forall p,q,m:nat. prime p \to prime q \to
451 p \divides q \sup m \to p = q.
455 apply (divides_exp_to_divides p q m).
456 assumption.assumption.
457 unfold prime in H.elim H.assumption.
460 theorem not_divides_defactorize_aux: \forall f:nat_fact. \forall i,j:nat.
461 i < j \to nth_prime i \ndivides defactorize_aux f j.
464 (nth_prime i \divides (nth_prime j) \sup (S n) \to False).
465 intro.absurd ((nth_prime i) = (nth_prime j)).
466 apply (divides_exp_to_eq ? ? (S n)).
467 apply prime_nth_prime.apply prime_nth_prime.
468 assumption.unfold Not.
470 apply (not_le_Sn_n i).rewrite > Hcut in \vdash (? ? %).assumption.
471 apply (injective_nth_prime ? ? H2).
474 cut (nth_prime i \divides (nth_prime j) \sup n
475 \lor nth_prime i \divides defactorize_aux n1 (S j)).
477 absurd ((nth_prime i) = (nth_prime j)).
478 apply (divides_exp_to_eq ? ? n).
479 apply prime_nth_prime.apply prime_nth_prime.
480 assumption.unfold Not.
483 apply (not_le_Sn_n i).rewrite > Hcut1 in \vdash (? ? %).assumption.
484 apply (injective_nth_prime ? ? H4).
486 apply (trans_lt ? j).assumption.unfold lt.apply le_n.
488 apply divides_times_to_divides.
489 apply prime_nth_prime.assumption.
492 lemma not_eq_nf_last_nf_cons: \forall g:nat_fact.\forall n,m,i:nat.
493 \lnot (defactorize_aux (nf_last n) i= defactorize_aux (nf_cons m g) i).
496 (exp (nth_prime i) (S n) = defactorize_aux (nf_cons m g) i \to False).
498 cut (S(max_p g)+i= i).
499 apply (not_le_Sn_n i).
500 rewrite < Hcut in \vdash (? ? %).
501 simplify.apply le_S_S.
503 apply injective_nth_prime.
504 apply (divides_exp_to_eq ? ? (S n)).
505 apply prime_nth_prime.apply prime_nth_prime.
507 change with (divides (nth_prime ((max_p (nf_cons m g))+i))
508 (defactorize_aux (nf_cons m g) i)).
509 apply divides_max_p_defactorize.
512 lemma not_eq_nf_cons_O_nf_cons: \forall f,g:nat_fact.\forall n,i:nat.
513 \lnot (defactorize_aux (nf_cons O f) i= defactorize_aux (nf_cons (S n) g) i).
515 simplify.unfold Not.rewrite < plus_n_O.
517 apply (not_divides_defactorize_aux f i (S i) ?).
518 unfold lt.apply le_n.
520 rewrite > assoc_times.
521 apply (witness ? ? ((exp (nth_prime i) n)*(defactorize_aux g (S i)))).
525 theorem eq_defactorize_aux_to_eq: \forall f,g:nat_fact.\forall i:nat.
526 defactorize_aux f i = defactorize_aux g i \to f = g.
529 generalize in match H.
532 apply inj_S. apply (inj_exp_r (nth_prime i)).
533 apply lt_SO_nth_prime_n.
536 apply (not_eq_nf_last_nf_cons n2 n n1 i H2).
537 generalize in match H1.
540 apply (not_eq_nf_last_nf_cons n1 n2 n i).
541 apply sym_eq. assumption.
543 generalize in match H3.
544 apply (nat_elim2 (\lambda n,n2.
545 ((nth_prime i) \sup n)*(defactorize_aux n1 (S i)) =
546 ((nth_prime i) \sup n2)*(defactorize_aux n3 (S i)) \to
547 nf_cons n n1 = nf_cons n2 n3)).
553 rewrite > (plus_n_O (defactorize_aux n3 (S i))).assumption.
555 apply (not_eq_nf_cons_O_nf_cons n1 n3 n5 i).assumption.
558 apply (not_eq_nf_cons_O_nf_cons n3 n1 n4 i).
559 apply sym_eq.assumption.
561 cut (nf_cons n4 n1 = nf_cons m n3).
564 rewrite > Hcut1.rewrite > Hcut2.reflexivity.
566 (match nf_cons n4 n1 with
567 [ (nf_last m) \Rightarrow n1
568 | (nf_cons m g) \Rightarrow g ] = n3).
569 rewrite > Hcut.simplify.reflexivity.
571 (match nf_cons n4 n1 with
572 [ (nf_last m) \Rightarrow m
573 | (nf_cons m g) \Rightarrow m ] = m).
574 rewrite > Hcut.simplify.reflexivity.
575 apply H4.simplify in H5.
576 apply (inj_times_r1 (nth_prime i)).
577 apply lt_O_nth_prime_n.
578 rewrite < assoc_times.rewrite < assoc_times.assumption.
581 theorem injective_defactorize_aux: \forall i:nat.
582 injective nat_fact nat (\lambda f.defactorize_aux f i).
585 apply (eq_defactorize_aux_to_eq x y i H).
588 theorem injective_defactorize:
589 injective nat_fact_all nat defactorize.
591 change with (\forall f,g.defactorize f = defactorize g \to f=g).
593 generalize in match H.elim g.
599 apply (not_eq_O_S O H1).
603 apply (not_le_Sn_n O).
604 rewrite > H1 in \vdash (? ? %).
605 change with (O < defactorize_aux n O).
606 apply lt_O_defactorize_aux.
607 generalize in match H.
612 apply (not_eq_O_S O).apply sym_eq. assumption.
618 apply (not_le_Sn_n (S O)).
619 rewrite > H1 in \vdash (? ? %).
620 change with ((S O) < defactorize_aux n O).
621 apply lt_SO_defactorize_aux.
622 generalize in match H.elim g.
626 apply (not_le_Sn_n O).
627 rewrite < H1 in \vdash (? ? %).
628 change with (O < defactorize_aux n O).
629 apply lt_O_defactorize_aux.
633 apply (not_le_Sn_n (S O)).
634 rewrite < H1 in \vdash (? ? %).
635 change with ((S O) < defactorize_aux n O).
636 apply lt_SO_defactorize_aux.
637 (* proper - proper *)
639 apply (injective_defactorize_aux O).
643 theorem factorize_defactorize:
644 \forall f,g: nat_fact_all. factorize (defactorize f) = f.
646 apply injective_defactorize.
647 apply defactorize_factorize.