-
Notifications
You must be signed in to change notification settings - Fork 1
/
Primes.v
2519 lines (2392 loc) · 70.8 KB
/
Primes.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* We copied and modified this file from https://github.com/roglo/coq_euler_prod_form/blob/master/Primes.v *)
(* Prime numbers *)
Set Nested Proofs Allowed.
Require Import Utf8 Arith SetoidList Permutation.
Require Import Misc.
Import List ListNotations.
Fixpoint prime_test cnt n d :=
match cnt with
| 0 => true
| S c =>
match n mod d with
| 0 => n <=? d
| S _ => prime_test c n (d + 1)
end
end.
Definition is_prime n :=
match n with
| 0 | 1 => false
| S (S c) => prime_test c n 2
end.
Definition prime p := is_prime p = true.
Theorem prime_test_false_exists_div_iff : ∀ n k,
2 ≤ k
→ (∀ d, 2 ≤ d < k → n mod d ≠ 0)
→ prime_test (n - k) n k = false
↔ ∃ a b : nat, 2 ≤ a ∧ 2 ≤ b ∧ n = a * b.
Proof.
intros * Hk Hd.
split.
-intros Hp.
remember (n - k) as cnt eqn:Hcnt; symmetry in Hcnt.
revert n k Hk Hd Hcnt Hp.
induction cnt; intros; [ easy | ].
cbn in Hp.
remember (n mod k) as m eqn:Hm; symmetry in Hm.
destruct m. {
destruct k; [ easy | ].
apply Nat.mod_divides in Hm; [ | easy ].
destruct Hm as (m, Hm).
destruct m; [ now rewrite Hm, Nat.mul_0_r in Hcnt | ].
destruct k; [ flia Hk | ].
destruct m. {
now rewrite Hm, Nat.mul_1_r, Nat.sub_diag in Hcnt.
}
exists (S (S k)), (S (S m)).
rewrite Hm.
replace (S (S k) * S (S m)) with (S (S k) + k * m + k + 2 * m + 2) by flia.
split; [ flia | ].
split; [ flia | easy ].
}
destruct n; [ flia Hcnt | ].
apply (IHcnt (S n) (k + 1)); [ flia Hk | | flia Hcnt | easy ].
intros d Hdk.
destruct (Nat.eq_dec d k) as [Hdk1| Hdk1]. {
now intros H; rewrite <- Hdk1, H in Hm.
}
apply Hd; flia Hdk Hdk1.
-intros (a & b & Han & Hbn & Hnab).
remember (n - k) as cnt eqn:Hcnt; symmetry in Hcnt.
revert n a b k Hk Hd Hcnt Han Hbn Hnab.
induction cnt; intros. {
specialize (Hd a) as H1.
assert (H : 2 ≤ a < k). {
split. {
destruct a; [ flia Hnab Han | ].
destruct a; [ flia Hnab Han Hbn | flia ].
}
rewrite Hnab in Hcnt.
apply Nat.sub_0_le in Hcnt.
apply (Nat.lt_le_trans _ (a * b)); [ | easy ].
destruct a; [ flia Han | ].
destruct b; [ flia Hbn | ].
destruct b; [ flia Hbn | flia ].
}
specialize (H1 H).
exfalso; apply H1; rewrite Hnab, Nat.mul_comm.
apply Nat.mod_mul; flia H.
}
cbn.
remember (n mod k) as m eqn:Hm; symmetry in Hm.
destruct m; [ apply Nat.leb_gt; flia Hcnt | ].
apply (IHcnt _ a b); [ flia Hk | | flia Hcnt | easy | easy | easy ].
intros d (H2d, Hdk).
destruct (Nat.eq_dec d k) as [Hdk1| Hdk1]. {
now intros H; rewrite <- Hdk1, H in Hm.
}
apply Hd.
flia H2d Hdk Hdk1.
Qed.
Theorem not_prime_decomp : ∀ n, 2 ≤ n →
¬ prime n
→ ∃ a b, 2 ≤ a ∧ 2 ≤ b ∧ n = a * b.
Proof.
intros n Hn Hp.
apply Bool.not_true_iff_false in Hp.
unfold is_prime in Hp.
destruct n; [ flia Hn | ].
destruct n; [ flia Hn | ].
replace n with (S (S n) - 2) in Hp at 1 by flia.
apply (prime_test_false_exists_div_iff _ 2); [ easy | | easy ].
intros * H; flia H.
Qed.
Theorem not_prime_exists_div : ∀ n, 2 ≤ n →
¬ prime n
→ ∃ a, 2 ≤ a < n ∧ Nat.divide a n.
Proof.
intros n Hn Hp.
specialize (not_prime_decomp n Hn Hp) as (a & b & Ha & Hb & Hab).
exists a.
split; [ | now rewrite Hab; apply Nat.divide_mul_l ].
split; [ easy | ].
rewrite Hab, Nat.mul_comm.
destruct a; [ flia Ha | ].
destruct b; [ flia Hb | ].
destruct b; [ flia Hb | flia ].
Qed.
Theorem exist_prime_divisor : ∀ n, 2 ≤ n →
∃ d, prime d ∧ Nat.divide d n.
Proof.
intros * Hn.
induction n as (n, IHn) using (well_founded_ind lt_wf).
remember (is_prime n) as b eqn:Hb; symmetry in Hb.
destruct b; [ now exists n | ].
apply Bool.not_true_iff_false in Hb.
specialize (not_prime_exists_div n Hn Hb) as (a & Han & Hd).
specialize (IHn a (proj2 Han) (proj1 Han)) as H1.
destruct H1 as (d & Hpd & Hda).
exists d.
split; [ easy | ].
now transitivity a.
Qed.
Theorem Nat_le_divides_fact : ∀ n d, d ≤ n → Nat.divide (fact d) (fact n).
Proof.
intros * Hdn.
replace d with (n - (n - d)) by flia Hdn.
apply Nat_divide_fact_fact.
Qed.
Theorem Nat_fact_divides_small : ∀ n d,
1 ≤ d ≤ n
→ fact n = fact n / d * d.
Proof.
intros * (Hd, Hdn).
specialize (Nat_le_divides_fact n d Hdn) as H1.
destruct H1 as (c, Hc).
rewrite Hc at 2.
destruct d; [ easy | ].
rewrite Nat_fact_succ.
rewrite (Nat.mul_comm (S d)).
rewrite Nat.mul_assoc.
rewrite Nat.div_mul; [ | easy ].
rewrite Hc, Nat_fact_succ.
now rewrite Nat.mul_assoc, Nat.mul_shuffle0.
Qed.
Lemma next_prime_bounded : ∀ n, ∃ m, n < m ≤ fact n + 1 ∧ prime m.
Proof.
intros.
specialize (exist_prime_divisor (fact n + 1)) as H1.
assert (H : 2 ≤ fact n + 1). {
clear.
induction n; [ easy | ].
rewrite Nat_fact_succ.
apply (Nat.le_trans _ (fact n + 1)); [ easy | ].
apply Nat.add_le_mono_r.
cbn; flia.
}
specialize (H1 H); clear H.
destruct H1 as (d & Hd & Hdn).
exists d.
split; [ | easy ].
split.
-destruct (lt_dec n d) as [Hnd| Hnd]; [ easy | ].
apply Nat.nlt_ge in Hnd; exfalso.
assert (Ht : Nat.divide d (fact n)). {
exists (fact n / d).
apply Nat_fact_divides_small.
split; [ | easy ].
destruct d; [ easy | flia ].
}
destruct Hdn as (z, Hz).
destruct Ht as (t, Ht).
rewrite Ht in Hz.
apply Nat.add_sub_eq_l in Hz.
rewrite <- Nat.mul_sub_distr_r in Hz.
apply Nat.eq_mul_1 in Hz.
now destruct Hz as (Hz, H); subst d.
-apply Nat.divide_pos_le; [ flia | easy ].
Qed.
Theorem infinitely_many_primes : ∀ n, ∃ m, m > n ∧ prime m.
Proof.
intros.
specialize (next_prime_bounded n) as (m & (Hnm & _) & Hp).
now exists m.
Qed.
Lemma prime_test_mod_ne_0 : ∀ n k,
2 ≤ n
→ prime_test (n - k) n k = true
→ ∀ d, k ≤ d < n → n mod d ≠ 0.
Proof.
intros * Hn Hp d Hd.
remember (n - k) as cnt eqn:Hcnt; symmetry in Hcnt.
revert n k d Hn Hcnt Hp Hd.
induction cnt; intros; [ flia Hcnt Hd | ].
cbn in Hp.
remember (n mod k) as m eqn:Hm; symmetry in Hm.
destruct m; [ apply Nat.leb_le in Hp; flia Hp Hd | ].
destruct n; [ flia Hcnt | ].
destruct (Nat.eq_dec k d) as [Hkd| Hkd]. {
now intros H; rewrite Hkd, H in Hm.
}
apply (IHcnt (S n) (k + 1)); [ easy | flia Hcnt | easy | flia Hd Hkd ].
Qed.
Theorem prime_only_divisors : ∀ p,
prime p → ∀ a, Nat.divide a p → a = 1 ∨ a = p.
Proof.
intros * Hp a * Hap.
destruct (lt_dec p 2) as [Hp2| Hp2]. {
destruct p; [ easy | ].
destruct p; [ easy | flia Hp2 ].
}
apply Nat.nlt_ge in Hp2.
destruct (zerop a) as [Ha| Ha]. {
subst a.
apply Nat.divide_0_l in Hap; flia Hap Hp2.
}
apply Nat.neq_0_lt_0 in Ha.
apply Nat.mod_divide in Hap; [ | easy ].
apply Nat.mod_divides in Hap; [ | easy ].
destruct Hap as (k, Hk).
symmetry in Hk.
destruct p; [ easy | ].
destruct p; [ easy | ].
specialize (prime_test_mod_ne_0 (S (S p)) 2 Hp2) as H1.
replace (S (S p) - 2) with p in H1 by flia.
specialize (H1 Hp).
destruct k; [ now rewrite Nat.mul_0_r in Hk | ].
destruct k; [ now rewrite Nat.mul_1_r in Hk; right | left ].
destruct a; [ easy | ].
destruct a; [ easy | exfalso ].
specialize (H1 (S (S k))) as H2.
assert (H : 2 ≤ S (S k) < S (S p)). {
split; [ flia Hp2 | flia Hk ].
}
specialize (H2 H); clear H.
apply H2; rewrite <- Hk.
now rewrite Nat.mod_mul.
Qed.
Theorem prime_prop : ∀ p, prime p → ∀ i, 2 ≤ i ≤ p - 1 → ¬ Nat.divide i p.
Proof.
intros * Hp i Hi Hdiv.
specialize (prime_only_divisors p Hp i Hdiv) as H1.
flia Hi H1.
Qed.
Theorem eq_primes_gcd_1 : ∀ a b,
prime a → prime b → a ≠ b → Nat.gcd a b = 1.
Proof.
intros p q Hp Hq Hpq.
specialize (prime_only_divisors _ Hp) as Hpp.
specialize (prime_only_divisors _ Hq) as Hqp.
specialize (Hpp (Nat.gcd p q) (Nat.gcd_divide_l _ _)) as H1.
specialize (Hqp (Nat.gcd p q) (Nat.gcd_divide_r _ _)) as H2.
destruct H1 as [H1| H1]; [ easy | ].
destruct H2 as [H2| H2]; [ easy | ].
now rewrite H1 in H2.
Qed.
Fixpoint prime_decomp_aux cnt n d :=
match cnt with
| 0 => []
| S c =>
match n mod d with
| 0 => d :: prime_decomp_aux c (n / d) d
| _ => prime_decomp_aux c n (S d)
end
end.
Definition prime_decomp n :=
match n with
| 0 | 1 => []
| _ => prime_decomp_aux n n 2
end.
Lemma prime_decomp_aux_of_prime_test : ∀ n k,
2 ≤ n
→ prime_test (n - 2) (k + n) (k + 2) = true
→ prime_decomp_aux n (k + n) (k + 2) = [k + n].
Proof.
intros * Hn Hpn.
destruct n; [ easy | ].
destruct n; [ flia Hn | clear Hn ].
replace (S (S n) - 2) with n in Hpn by flia.
revert k Hpn.
induction n; intros. {
cbn - [ "/" "mod" ].
rewrite Nat.mod_same; [ | flia ].
rewrite Nat.div_same; [ | flia ].
rewrite Nat.mod_1_l; [ easy | flia ].
}
remember (S (S n)) as sn.
cbn - [ "/" "mod" ].
cbn - [ "/" "mod" ] in Hpn.
remember ((k + S sn) mod (k + 2)) as b eqn:Hb; symmetry in Hb.
destruct b; [ apply Nat.leb_le in Hpn; flia Heqsn Hpn | ].
replace (k + S sn) with (S k + sn) in Hpn |-* by flia.
replace (S (k + 2)) with (S k + 2) by flia.
replace (k + 2 + 1) with (S k + 2) in Hpn by flia.
now apply IHn.
Qed.
Theorem prime_neq_0 : ∀ p, prime p → p ≠ 0.
Proof. now intros * Hp H; subst p. Qed.
Theorem prime_ge_2 : ∀ n, prime n → 2 ≤ n.
Proof.
intros * Hp.
destruct n; [ easy | ].
destruct n; [ easy | flia ].
Qed.
Theorem prime_decomp_of_prime : ∀ n, prime n → prime_decomp n = [n].
Proof.
intros * Hpn.
specialize (prime_ge_2 _ Hpn) as Hn.
unfold prime, is_prime in Hpn.
unfold prime_decomp.
replace n with (S (S (n - 2))) in Hpn at 1 by flia Hn.
replace n with (S (S (n - 2))) at 1 by flia Hn.
replace n with (0 + n) in Hpn at 2 by flia.
replace 2 with (0 + 2) in Hpn at 2 by flia.
now apply prime_decomp_aux_of_prime_test in Hpn; [ | easy ].
Qed.
Lemma prime_decomp_aux_le : ∀ cnt n d d',
d ≤ d' → HdRel le d (prime_decomp_aux cnt n d').
Proof.
intros * Hdd.
revert n d d' Hdd.
induction cnt; intros; [ constructor | cbn ].
destruct (n mod d') as [| Hnd]; [ now constructor | ].
apply IHcnt, (le_trans _ d'); [ easy | ].
apply Nat.le_succ_diag_r.
Qed.
Lemma Sorted_prime_decomp_aux : ∀ cnt n d,
Sorted le (prime_decomp_aux cnt n d).
Proof.
intros.
revert n d.
induction cnt; intros; [ constructor | cbn ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b. {
constructor; [ apply IHcnt | ].
now apply prime_decomp_aux_le.
}
apply IHcnt.
Qed.
Theorem Sorted_prime_decomp : ∀ n, Sorted le (prime_decomp n).
Proof.
intros.
destruct n; [ constructor | ].
cbn - [ "/" "mod" ].
destruct n; [ constructor | ].
destruct (S (S n) mod 2) as [| b]. {
constructor; [ apply Sorted_prime_decomp_aux | ].
now apply prime_decomp_aux_le.
}
apply Sorted_prime_decomp_aux.
Qed.
Lemma in_prime_decomp_aux_le : ∀ cnt n d d',
d' ∈ prime_decomp_aux cnt n d
→ d ≤ d'.
Proof.
intros * Hd'.
revert n d d' Hd'.
induction cnt; intros; [ easy | ].
cbn in Hd'.
destruct (n mod d) as [| b]. {
destruct Hd' as [Hd'| Hd']; [ now subst d' | ].
now apply (IHcnt (n / d)).
}
transitivity (S d); [ apply Nat.le_succ_diag_r | now apply (IHcnt n) ].
Qed.
Theorem in_prime_decomp_ge_2 : ∀ n d,
d ∈ prime_decomp n
→ 2 ≤ d.
Proof.
intros * Hd.
destruct n; [ easy | ].
destruct n; [ easy | ].
unfold prime_decomp in Hd.
eapply in_prime_decomp_aux_le.
apply Hd.
Qed.
Theorem prime_decomp_param_ge_2 : ∀ n d,
d ∈ prime_decomp n
→ 2 ≤ n.
Proof.
intros * Hd.
destruct n; [ easy | ].
destruct n; [ easy | flia ].
Qed.
Lemma in_prime_decomp_aux_divide : ∀ cnt n d p,
d ≠ 0
→ p ∈ prime_decomp_aux cnt n d
→ Nat.divide p n.
Proof.
intros * Hdz Hp.
specialize (in_prime_decomp_aux_le cnt n d _ Hp) as Hdp.
assert (Hpz : p ≠ 0) by flia Hdz Hdp.
move Hpz before Hdz.
revert n d p Hdz Hp Hpz Hdp.
induction cnt; intros; [ easy | ].
cbn in Hp.
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b. {
destruct Hp as [Hp| Hp]; [ now subst d; apply Nat.mod_divide | ].
apply (Nat.divide_trans _ (n / d)). 2: {
apply Nat.mod_divides in Hb; [ | easy ].
destruct Hb as (c, Hc).
rewrite Hc, Nat.mul_comm, Nat.div_mul; [ | easy ].
apply Nat.divide_factor_l.
}
now apply (IHcnt _ d).
}
specialize (in_prime_decomp_aux_le _ _ _ _ Hp) as H1.
now apply (IHcnt _ (S d)).
Qed.
Theorem in_prime_decomp_divide : ∀ n d,
d ∈ prime_decomp n → Nat.divide d n.
Proof.
intros * Hd.
assert (H2n : 2 ≤ n). {
destruct n; [ easy | ].
destruct n; [ easy | flia ].
}
specialize (in_prime_decomp_ge_2 n d Hd) as H2d.
move Hd at bottom.
unfold prime_decomp in Hd.
replace n with (S (S (n - 2))) in Hd by flia H2n.
replace (S (S (n - 2))) with n in Hd by flia H2n.
now apply in_prime_decomp_aux_divide in Hd.
Qed.
Theorem in_prime_decomp_le : ∀ n d : nat, d ∈ prime_decomp n → d ≤ n.
Proof.
intros * Hd.
apply Nat.divide_pos_le; [ | now apply in_prime_decomp_divide ].
destruct n; [ easy | flia ].
Qed.
Lemma prime_decomp_aux_at_1 : ∀ cnt d, 2 ≤ d → prime_decomp_aux cnt 1 d = [].
Proof.
intros * H2d.
destruct d; [ flia H2d | ].
destruct d; [ flia H2d | clear H2d ].
revert d.
induction cnt; intros; [ easy | cbn ].
destruct d; [ apply IHcnt | ].
replace (S d - d) with 1 by flia.
apply IHcnt.
Qed.
Lemma prime_decomp_aux_more_iter : ∀ k cnt n d,
2 ≤ n
→ 2 ≤ d
→ n + 2 ≤ cnt + d
→ prime_decomp_aux cnt n d = prime_decomp_aux (cnt + k) n d.
Proof.
intros * H2n H2d Hnc.
revert n k d H2n H2d Hnc.
induction cnt; intros. {
cbn in Hnc; cbn.
revert d H2d Hnc.
induction k; intros; [ easy | cbn ].
rewrite Nat.mod_small; [ | flia Hnc ].
destruct n; [ flia H2n | ].
apply IHk; flia Hnc.
}
cbn - [ "/" "mod" ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b. {
f_equal.
apply Nat.mod_divides in Hb; [ | flia H2d ].
destruct Hb as (b, Hb); rewrite Nat.mul_comm in Hb.
rewrite Hb, Nat.div_mul; [ | flia H2d ].
destruct (le_dec 2 b) as [H2b| H2b]. {
apply IHcnt; [ easy | easy | ].
transitivity (n + 1); [ | flia H2n Hnc ].
rewrite Hb.
destruct b; [ flia H2n Hb | ].
destruct d; [ easy | ].
destruct d; [ flia H2d | ].
cbn; rewrite Nat.mul_comm; cbn.
flia.
}
apply Nat.nle_gt in H2b.
destruct b; [ cbn in Hb; subst n; flia H2n | ].
destruct b; [ | flia H2b ].
rewrite prime_decomp_aux_at_1; [ | easy ].
now rewrite prime_decomp_aux_at_1.
}
apply IHcnt; [ easy | | flia Hnc ].
flia H2d Hnc.
Qed.
Lemma prime_test_more_iter : ∀ k cnt n d,
2 ≤ n
→ n ≤ cnt + d
→ prime_test cnt n d = prime_test (cnt + k) n d.
Proof.
intros * H2n Hnc.
revert n k d H2n Hnc.
induction cnt; intros. {
cbn in Hnc; cbn.
revert d Hnc.
induction k; intros; [ easy | cbn ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b; [ now symmetry; apply Nat.leb_le | ].
destruct n; [ flia H2n | ].
apply IHk; flia Hnc.
}
cbn - [ "/" "mod" ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b; [ easy | ].
apply IHcnt; [ easy | flia Hnc ].
Qed.
Lemma hd_prime_decomp_aux_ge : ∀ cnt n d,
2 ≤ d
→ 2 ≤ hd 2 (prime_decomp_aux cnt n d).
Proof.
intros * H2d.
revert d H2d.
induction cnt; intros; [ easy | cbn ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b; [ easy | ].
apply IHcnt; flia H2d.
Qed.
Lemma prev_not_div_prime_test_true : ∀ cnt n d,
2 ≤ n
→ 2 ≤ d
→ n ≤ cnt + d
→ (∀ e, 2 ≤ e < n → n mod e ≠ 0)
→ prime_test cnt n d = true.
Proof.
intros * H2n H2d Hcnt Hn.
revert n d H2n H2d Hcnt Hn.
induction cnt; intros; [ easy | cbn ].
remember (n mod d) as b1 eqn:Hb1; symmetry in Hb1.
destruct b1. {
apply Nat.leb_le.
apply Nat.mod_divides in Hb1; [ | flia H2d ].
destruct Hb1 as (b1, Hb1).
destruct b1; [ flia H2d Hb1 | ].
destruct b1; [ flia Hb1 | ].
specialize (Hn (S (S b1))) as H1.
assert (H : 2 ≤ S (S b1) < n). {
split; [ flia | ].
rewrite Hb1; remember (S (S b1)) as b.
destruct d; [ flia H2d | cbn ].
destruct d; [ flia H2d | flia Heqb ].
}
specialize (H1 H).
exfalso; apply H1; clear H1.
rewrite Hb1.
now apply Nat.mod_mul.
}
apply IHcnt; [ easy | flia H2d | flia Hcnt | easy ].
Qed.
Lemma hd_prime_decomp_aux_prime_test_true : ∀ cnt n b d,
2 ≤ n
→ 2 ≤ d
→ n + 2 ≤ cnt + d
→ (∀ e : nat, 2 ≤ e < d → n mod e ≠ 0)
→ b = hd 2 (prime_decomp_aux cnt n d)
→ prime_test (b - 2) b 2 = true.
Proof.
intros * H2n H2d Hcnt Hnd Hb.
revert d H2d Hcnt Hnd Hb.
induction cnt; intros; [ now subst b | ].
cbn - [ "/" "mod" ] in Hb.
remember (n mod d) as b1 eqn:Hb1; symmetry in Hb1.
destruct b1. {
cbn in Hb; subst b.
apply Nat.mod_divides in Hb1; [ | flia H2d ].
destruct Hb1 as (b1, Hb1).
destruct b1; [ flia H2n Hb1 | ].
destruct b1. {
rewrite Nat.mul_1_r in Hb1; subst n.
apply prev_not_div_prime_test_true; [ easy | easy | flia H2d | easy ].
}
apply prev_not_div_prime_test_true; [ easy | easy | flia H2n | ].
intros e He.
specialize (Hnd e He) as H1.
intros H2; apply H1; clear H1.
apply Nat.mod_divides in H2; [ | flia He ].
destruct H2 as (b2, Hb2); rewrite Nat.mul_comm in Hb2.
rewrite Hb1, Hb2, Nat.mul_shuffle0.
apply Nat.mod_mul; flia He.
}
assert (H : ∀ e, 2 ≤ e < 1 + d → n mod e ≠ 0). {
intros e He.
destruct (Nat.eq_dec e d) as [Hed| Hed]. {
now subst e; intros H; rewrite H in Hb1.
}
apply Hnd; flia He Hed.
}
move H before Hnd; clear Hnd; rename H into Hnd.
clear b1 Hb1.
replace (S cnt + d) with (cnt + S d) in Hcnt by flia.
apply (IHcnt (S d)); [ flia H2d | easy | easy | easy ].
Qed.
Theorem first_in_decomp_is_prime : ∀ n, prime (List.hd 2 (prime_decomp n)).
Proof.
intros.
unfold is_prime, prime_decomp.
destruct n; [ easy | ].
destruct n; [ easy | ].
assert (H2n : 2 ≤ S (S n)) by flia.
remember (S (S n)) as n'.
clear n Heqn'; rename n' into n.
specialize (hd_prime_decomp_aux_ge n n 2 (le_refl _)) as H2b.
unfold prime, is_prime.
remember (hd 2 (prime_decomp_aux n n 2)) as b eqn:Hb.
move b before n; move H2b before H2n.
replace b with (S (S (b - 2))) by flia H2b.
replace (S (S (b - 2))) with b by flia H2b.
apply (hd_prime_decomp_aux_prime_test_true n n b 2);
[ easy | easy | easy | | easy ].
intros e He; flia He.
Qed.
Lemma prime_decomp_aux_not_nil : ∀ cnt n d,
2 ≤ n
→ 2 ≤ d
→ n + 2 ≤ cnt + d
→ (∀ e : nat, 2 ≤ e < d → n mod e ≠ 0)
→ prime_decomp_aux cnt n d ≠ [].
Proof.
intros * H2n H2d Hcnt Hnd.
revert d H2d Hcnt Hnd.
induction cnt; intros. {
assert (H : 2 ≤ n < d) by flia H2n Hcnt.
specialize (Hnd n H); clear H.
rewrite Nat.mod_same in Hnd; [ easy | flia H2n ].
}
cbn - [ "/" "mod" ].
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b; [ easy | ].
rewrite Nat.add_succ_comm in Hcnt.
apply IHcnt; [ flia H2d | easy | ].
intros e He.
destruct (Nat.eq_dec e d) as [Hed| Hed]. {
now subst e; intros H; rewrite H in Hb.
}
apply Hnd; flia He Hed.
Qed.
Theorem prime_decomp_nil_iff : ∀ n, prime_decomp n = [] ↔ n = 0 ∨ n = 1.
Proof.
intros.
split.
-intros Hn.
unfold prime_decomp in Hn.
destruct n; [ now left | ].
destruct n; [ now right | exfalso ].
revert Hn.
apply prime_decomp_aux_not_nil; [ flia | easy | easy | ].
intros e He; flia He.
-now intros [Hn| Hn]; subst n.
Qed.
Lemma prime_decomp_aux_cons_nil : ∀ cnt n d l,
2 ≤ n
→ 2 ≤ d
→ n + 2 ≤ cnt + d
→ (∀ e, 2 ≤ e < d → n mod e ≠ 0)
→ prime_decomp_aux cnt n d = n :: l
→ l = [].
Proof.
intros * H2n H2d Hcnt Hdn Hnd.
revert d l H2d Hcnt Hdn Hnd.
induction cnt; intros; [ easy | ].
cbn in Hnd.
remember (n mod d) as b eqn:Hb; symmetry in Hb.
destruct b. {
injection Hnd; clear Hnd; intros Hl Hd; subst d.
rewrite Nat.div_same in Hl; [ | flia H2n ].
now rewrite prime_decomp_aux_at_1 in Hl.
}
rewrite Nat.add_succ_comm in Hcnt.
apply (IHcnt (S d)); [ flia H2d | easy | | easy ].
intros e He.
destruct (Nat.eq_dec e d) as [Hed| Hed]. {
now subst e; intros H; rewrite H in Hb.
}
apply Hdn; flia He Hed.
Qed.
Lemma prime_decomp_aux_cons : ∀ p b l n d cb cn,
2 ≤ n
→ 2 ≤ b
→ 2 ≤ d
→ 2 ≤ p
→ b + 2 ≤ cb + d
→ n + 2 ≤ cn + d
→ n * p = b
→ prime_decomp_aux cb b d = p :: l
→ prime_decomp_aux cn n d = l.
Proof.
intros * H2n H2b H2d H2p Hcb Hcn Hb Hbp.
revert p b n d cn H2n H2b H2d H2p Hcb Hcn Hb Hbp.
induction cb; intros; [ easy | ].
cbn in Hbp.
remember (b mod d) as b1 eqn:Hb1; symmetry in Hb1.
destruct b1. {
injection Hbp; clear Hbp; intros Hl Hp; subst d.
rewrite <- Hb, Nat.div_mul in Hl; [ | flia H2b Hb ].
rewrite (prime_decomp_aux_more_iter cn) in Hl; [ | easy | easy | ]. 2: {
apply Nat.succ_le_mono.
replace (S (cb + p)) with (S cb + p) by flia.
transitivity (b + 2); [ | easy ].
replace (S (n + 2)) with (S n + 2) by flia.
apply Nat.add_le_mono_r.
rewrite <- Hb.
destruct p; [ flia H2p | ].
destruct p; [ flia H2p | ].
rewrite Nat.mul_comm; cbn.
destruct n; [ easy | flia ].
}
rewrite Nat.add_comm in Hl.
now rewrite (prime_decomp_aux_more_iter cb).
}
rewrite (prime_decomp_aux_more_iter 1); try easy.
rewrite Nat.add_1_r; cbn.
remember (n mod d) as b2 eqn:Hb2; symmetry in Hb2.
destruct b2. {
apply Nat.mod_divides in Hb2; [ | flia H2d ].
destruct Hb2 as (b2, Hb2).
rewrite <- Hb, Hb2 in Hb1.
rewrite <- Nat.mul_assoc, Nat.mul_comm in Hb1.
rewrite Nat.mod_mul in Hb1; [ easy | flia H2d ].
}
rewrite Nat.add_succ_comm in Hcb.
apply (IHcb p b); try easy; [ flia H2d | flia Hcn ].
Qed.
Theorem prime_decomp_mul : ∀ n d l,
2 ≤ d
→ prime_decomp (n * d) = d :: l
→ prime_decomp n = l.
Proof.
intros * H2d Hnd.
unfold prime_decomp in Hnd.
unfold prime_decomp.
destruct n; [ easy | ].
destruct n. {
rewrite Nat.mul_1_l in Hnd.
destruct d; [ easy | ].
cbn - [ "/" "mod" ] in Hnd.
destruct d; [ easy | ].
remember (S (S d)) as d'.
replace (S d) with (d' - 1) in Hnd by flia Heqd'.
clear d Heqd'; rename d' into d; move H2d after Hnd.
remember (d mod 2) as b eqn:Hb; symmetry in Hb.
destruct b. {
remember (prime_decomp_aux _ _ _) as x.
injection Hnd; clear Hnd; intros Hl Hd; subst x.
now subst d.
}
symmetry.
apply prime_decomp_aux_cons_nil in Hnd; [ easy | easy | flia | flia | ].
intros e He H.
replace e with 2 in H by flia He.
now rewrite H in Hb.
}
assert (H2n : 2 ≤ S (S n)) by flia.
remember (S (S n)) as n'; clear n Heqn'; rename n' into n.
remember (n * d) as b eqn:Hb; symmetry in Hb.
destruct b; [ easy | ].
destruct b; [ easy | ].
assert (H2b : 2 ≤ S (S b)) by flia.
remember (S (S b)) as b'; clear b Heqb'; rename b' into b.
move H2n after Hb; move H2b after Hb.
now apply (prime_decomp_aux_cons) with (n := n) (cn := n) in Hnd.
Qed.
Theorem prime_decomp_cons : ∀ n a l,
prime_decomp n = a :: l
→ prime_decomp (n / a) = l.
Proof.
intros * Hl.
assert (Hap : prime a). {
specialize (first_in_decomp_is_prime n) as H1.
now rewrite Hl in H1.
}
assert (H2a : 2 ≤ a) by now apply prime_ge_2.
apply (prime_decomp_mul (n / a) a); [ easy | ].
specialize (in_prime_decomp_divide n a) as H1.
rewrite Hl in H1.
specialize (H1 (or_introl eq_refl)).
destruct H1 as (b, Hb).
rewrite Hb, Nat.div_mul; [ | flia H2a ].
now rewrite <- Hb.
Qed.
Theorem prime_decomp_inj : ∀ a b,
a ≠ 0 → b ≠ 0 → prime_decomp a = prime_decomp b → a = b.
Proof.
intros * Ha0 Hb0 Hab.
remember (prime_decomp b) as l eqn:Hb; symmetry in Hb.
rename Hab into Ha; move Ha after Hb.
revert a b Ha0 Hb0 Ha Hb.
induction l as [| d]; intros. {
apply prime_decomp_nil_iff in Ha.
apply prime_decomp_nil_iff in Hb.
destruct Ha as [Ha| Ha]; [ easy | ].
destruct Hb as [Hb| Hb]; [ easy | ].
now subst a b.
}
specialize (in_prime_decomp_divide a d) as Hda.
rewrite Ha in Hda; cbn in Hda.
specialize (Hda (or_introl (eq_refl _))) as (da, Hda).
specialize (in_prime_decomp_divide b d) as Hdb.
rewrite Hb in Hdb; cbn in Hdb.
specialize (Hdb (or_introl (eq_refl _))) as (db, Hdb).
move db before da.
rewrite Hda, Hdb; f_equal.
apply IHl.
-now intros H; rewrite H in Hda.
-now intros H; rewrite H in Hdb.
-rewrite Hda in Ha.
apply (prime_decomp_mul _ d); [ | easy ].
destruct d; [ flia Ha0 Hda | ].
destruct d; [ | flia ].
apply (in_prime_decomp_ge_2 b 1).
now rewrite Hb; left.
-rewrite Hdb in Hb.
apply (prime_decomp_mul _ d); [ | easy ].
destruct d; [ flia Ha0 Hda | ].
destruct d; [ | flia ].
apply (in_prime_decomp_ge_2 a 1).
now rewrite Ha; left.
Qed.
Theorem in_prime_decomp_is_prime : ∀ n d, d ∈ prime_decomp n → prime d.
Proof.
intros * Hdn.
specialize (In_nth (prime_decomp n) d 2 Hdn) as (i & Hilen & Hid).
clear Hdn; subst d.
revert n Hilen.
induction i; intros. {
rewrite <- List_hd_nth_0.
apply first_in_decomp_is_prime.
}
remember (prime_decomp n) as l eqn:Hl; symmetry in Hl.
destruct l as [| a l]; [ easy | ].
cbn in Hilen; cbn.
apply Nat.succ_lt_mono in Hilen.
specialize (prime_decomp_cons n a l Hl) as H1.
rewrite <- H1.
apply IHi.
now rewrite H1.
Qed.
Theorem prime_decomp_prod : ∀ n, n ≠ 0 →
fold_left Nat.mul (prime_decomp n) 1 = n.
Proof.
intros * Hnz.
remember (prime_decomp n) as l eqn:Hl; symmetry in Hl.
revert n Hnz Hl.
induction l as [| a l]; intros. {
now apply prime_decomp_nil_iff in Hl; destruct Hl.
}
remember 1 as one; cbn; subst one.
specialize (in_prime_decomp_divide n a) as H1.
rewrite Hl in H1; specialize (H1 (or_introl eq_refl)).
destruct H1 as (k, Hk).
rewrite Hk in Hl.
assert (H2a : 2 ≤ a). {
apply (in_prime_decomp_ge_2 (k * a)).
now rewrite Hl; left.
}
apply prime_decomp_mul in Hl; [ | easy ].
apply IHl in Hl; [ | now intros H; subst k ].
apply (Nat.mul_cancel_r _ _ a) in Hl; [ | flia H2a ].
rewrite Hk, <- Hl.
symmetry; apply List_fold_left_mul_assoc.
Qed.
Theorem eq_gcd_prime_small_1 : ∀ p n,
prime p
→ 0 < n < p
→ Nat.gcd p n = 1.
Proof.
intros * Hp Hnp.
remember (Nat.gcd p n) as g eqn:Hg; symmetry in Hg.
destruct g; [ now apply Nat.gcd_eq_0 in Hg; rewrite (proj1 Hg) in Hp | ].
destruct g; [ easy | exfalso ].
specialize (Nat.gcd_divide_l p n) as H1.
rewrite Hg in H1.
destruct H1 as (d, Hd).
specialize (prime_only_divisors p Hp (S (S g))) as H1.
assert (H : Nat.divide (S (S g)) p). {
rewrite Hd; apply Nat.divide_factor_r.
}
specialize (H1 H); clear H.
destruct H1 as [H1| H1]; [ easy | ].
destruct d; [ now rewrite Hd in Hp | ].
rewrite Hd in H1.
destruct d. {
rewrite Nat.mul_1_l in Hd.
rewrite <- Hd in Hg.
specialize (Nat.gcd_divide_r p n) as H2.
rewrite Hg in H2.
destruct H2 as (d2, Hd2).
destruct d2; [ rewrite Hd2 in Hnp; flia Hnp | ].
rewrite Hd2 in Hnp; flia Hnp.
}
replace (S (S d)) with (1 + S d) in H1 by flia.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l in H1.
rewrite <- (Nat.add_0_r (S (S g))) in H1 at 1.
now apply Nat.add_cancel_l in H1.
Qed.
Theorem prime_divisor_in_decomp : ∀ n d,
2 ≤ n
→ prime d
→ Nat.divide d n
→ d ∈ prime_decomp n.
Proof.
intros * H2n Hd Hdn.
unfold prime_decomp.
replace n with (S (S (n - 2))) at 1 by flia H2n.
assert (prime_divisor_in_decomp_aux : ∀ cnt n d p,
2 ≤ n
→ 2 ≤ d
→ d ≤ p
→ n + 2 ≤ cnt + d
→ prime p
→ Nat.divide p n
→ p ∈ prime_decomp_aux cnt n d). {
clear.
intros * H2n H2d Hdp Hcnt Hp Hpn.
revert n d p H2n H2d Hdp Hcnt Hp Hpn.
induction cnt; intros. {
cbn in Hcnt; cbn.
destruct Hpn as (k, Hk); subst n.
apply Nat.nlt_ge in Hcnt; apply Hcnt; clear Hcnt.
destruct k; [ flia H2n | flia Hdp ].
}
cbn.
remember (n mod d) as b eqn:Hb; symmetry in Hb.
assert (Hdz : d ≠ 0) by flia H2d.
destruct b. 2: {
apply IHcnt; [ easy | flia H2d | | flia Hcnt | easy | easy ].
destruct (Nat.eq_dec p d) as [Hpd| Hpd]; [ | flia Hdp Hpd ].
subst d; exfalso.
apply Nat.mod_divide in Hpn; [ | easy ].
now rewrite Hpn in Hb.
}
destruct (Nat.eq_dec p d) as [Hpd| Hpd]; [ now left | right ].
apply IHcnt; [ | easy | easy | | easy | ]. {
apply Nat.mod_divide in Hb; [ | easy ].
destruct Hb as (k, Hk).
rewrite Hk, Nat.div_mul; [ | easy ].
destruct k; [ flia H2n Hk | ].
destruct k; [ exfalso | flia ].
rewrite Nat.mul_1_l in Hk; subst n.
destruct Hpn as (k, Hk).
destruct k; [ easy | ].
destruct k; [ rewrite Nat.mul_1_l in Hk; flia Hk Hpd | ].
apply Nat.nlt_ge in Hdp; apply Hdp; clear Hdp.
rewrite Hk; cbn.
destruct p; [ now rewrite Nat.mul_0_r in Hk | flia ].