src/HOL/Number_Theory/Primes.thy
author haftmann
Wed Jun 02 16:24:14 2010 +0200 (2010-06-02)
changeset 37294 a2a8216999a2
parent 35644 d20cf282342e
child 37607 ebb8b1a79c4c
permissions -rw-r--r--
absolute import -- must work with Main.thy / HOL-Proofs
haftmann@32479
     1
(*  Authors:    Christophe Tabacznyj, Lawrence C. Paulson, Amine Chaieb,
nipkow@31798
     2
                Thomas M. Rasmussen, Jeremy Avigad, Tobias Nipkow
huffman@31706
     3
huffman@31706
     4
haftmann@32479
     5
This file deals with properties of primes. Definitions and lemmas are
haftmann@32479
     6
proved uniformly for the natural numbers and integers.
huffman@31706
     7
huffman@31706
     8
This file combines and revises a number of prior developments.
huffman@31706
     9
huffman@31706
    10
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
huffman@31706
    11
and Lawrence C. Paulson, based on \cite{davenport92}. They introduced
huffman@31706
    12
gcd, lcm, and prime for the natural numbers.
huffman@31706
    13
huffman@31706
    14
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
huffman@31706
    15
extended gcd, lcm, primes to the integers. Amine Chaieb provided
huffman@31706
    16
another extension of the notions to the integers, and added a number
huffman@31706
    17
of results to "Primes" and "GCD". IntPrimes also defined and developed
huffman@31706
    18
the congruence relations on the integers. The notion was extended to
webertj@33718
    19
the natural numbers by Chaieb.
huffman@31706
    20
avigad@32036
    21
Jeremy Avigad combined all of these, made everything uniform for the
avigad@32036
    22
natural numbers and the integers, and added a number of new theorems.
avigad@32036
    23
nipkow@31798
    24
Tobias Nipkow cleaned up a lot.
wenzelm@21256
    25
*)
wenzelm@21256
    26
huffman@31706
    27
haftmann@32479
    28
header {* Primes *}
wenzelm@21256
    29
haftmann@32479
    30
theory Primes
haftmann@37294
    31
imports "~~/src/HOL/GCD"
huffman@31706
    32
begin
huffman@31706
    33
huffman@31706
    34
declare One_nat_def [simp del]
huffman@31706
    35
huffman@31706
    36
class prime = one +
huffman@31706
    37
huffman@31706
    38
fixes
huffman@31706
    39
  prime :: "'a \<Rightarrow> bool"
huffman@31706
    40
huffman@31706
    41
instantiation nat :: prime
huffman@31706
    42
huffman@31706
    43
begin
wenzelm@21256
    44
wenzelm@21263
    45
definition
huffman@31706
    46
  prime_nat :: "nat \<Rightarrow> bool"
huffman@31706
    47
where
huffman@31709
    48
  [code del]: "prime_nat p = (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
huffman@31706
    49
huffman@31706
    50
instance proof qed
haftmann@23687
    51
huffman@31706
    52
end
huffman@31706
    53
huffman@31706
    54
instantiation int :: prime
huffman@31706
    55
huffman@31706
    56
begin
huffman@31706
    57
huffman@31706
    58
definition
huffman@31706
    59
  prime_int :: "int \<Rightarrow> bool"
huffman@31706
    60
where
huffman@31709
    61
  [code del]: "prime_int p = prime (nat p)"
huffman@31706
    62
huffman@31706
    63
instance proof qed
huffman@31706
    64
huffman@31706
    65
end
huffman@31706
    66
huffman@31706
    67
huffman@31706
    68
subsection {* Set up Transfer *}
huffman@31706
    69
huffman@31706
    70
haftmann@32479
    71
lemma transfer_nat_int_prime:
huffman@31706
    72
  "(x::int) >= 0 \<Longrightarrow> prime (nat x) = prime x"
huffman@31706
    73
  unfolding gcd_int_def lcm_int_def prime_int_def
huffman@31706
    74
  by auto
haftmann@23687
    75
haftmann@35644
    76
declare transfer_morphism_nat_int[transfer add return:
haftmann@32479
    77
    transfer_nat_int_prime]
huffman@31706
    78
haftmann@32479
    79
lemma transfer_int_nat_prime:
huffman@31706
    80
  "prime (int x) = prime x"
huffman@31706
    81
  by (unfold gcd_int_def lcm_int_def prime_int_def, auto)
huffman@31706
    82
haftmann@35644
    83
declare transfer_morphism_int_nat[transfer add return:
haftmann@32479
    84
    transfer_int_nat_prime]
nipkow@31798
    85
wenzelm@21256
    86
haftmann@32479
    87
subsection {* Primes *}
huffman@31706
    88
nipkow@31952
    89
lemma prime_odd_nat: "prime (p::nat) \<Longrightarrow> p > 2 \<Longrightarrow> odd p"
huffman@31706
    90
  unfolding prime_nat_def
huffman@31706
    91
  apply (subst even_mult_two_ex)
huffman@31706
    92
  apply clarify
huffman@31706
    93
  apply (drule_tac x = 2 in spec)
huffman@31706
    94
  apply auto
huffman@31706
    95
done
huffman@31706
    96
nipkow@31952
    97
lemma prime_odd_int: "prime (p::int) \<Longrightarrow> p > 2 \<Longrightarrow> odd p"
huffman@31706
    98
  unfolding prime_int_def
nipkow@31952
    99
  apply (frule prime_odd_nat)
huffman@31706
   100
  apply (auto simp add: even_nat_def)
huffman@31706
   101
done
huffman@31706
   102
nipkow@31992
   103
(* FIXME Is there a better way to handle these, rather than making them elim rules? *)
chaieb@22027
   104
nipkow@31952
   105
lemma prime_ge_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 0"
huffman@31706
   106
  by (unfold prime_nat_def, auto)
chaieb@22027
   107
nipkow@31952
   108
lemma prime_gt_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p > 0"
huffman@31706
   109
  by (unfold prime_nat_def, auto)
wenzelm@22367
   110
nipkow@31952
   111
lemma prime_ge_1_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 1"
huffman@31706
   112
  by (unfold prime_nat_def, auto)
chaieb@22027
   113
nipkow@31952
   114
lemma prime_gt_1_nat [elim]: "prime (p::nat) \<Longrightarrow> p > 1"
huffman@31706
   115
  by (unfold prime_nat_def, auto)
wenzelm@22367
   116
nipkow@31952
   117
lemma prime_ge_Suc_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= Suc 0"
huffman@31706
   118
  by (unfold prime_nat_def, auto)
wenzelm@22367
   119
nipkow@31952
   120
lemma prime_gt_Suc_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p > Suc 0"
huffman@31706
   121
  by (unfold prime_nat_def, auto)
huffman@31706
   122
nipkow@31952
   123
lemma prime_ge_2_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 2"
huffman@31706
   124
  by (unfold prime_nat_def, auto)
huffman@31706
   125
nipkow@31952
   126
lemma prime_ge_0_int [elim]: "prime (p::int) \<Longrightarrow> p >= 0"
nipkow@31992
   127
  by (unfold prime_int_def prime_nat_def) auto
wenzelm@22367
   128
nipkow@31952
   129
lemma prime_gt_0_int [elim]: "prime (p::int) \<Longrightarrow> p > 0"
huffman@31706
   130
  by (unfold prime_int_def prime_nat_def, auto)
huffman@31706
   131
nipkow@31952
   132
lemma prime_ge_1_int [elim]: "prime (p::int) \<Longrightarrow> p >= 1"
huffman@31706
   133
  by (unfold prime_int_def prime_nat_def, auto)
chaieb@22027
   134
nipkow@31952
   135
lemma prime_gt_1_int [elim]: "prime (p::int) \<Longrightarrow> p > 1"
huffman@31706
   136
  by (unfold prime_int_def prime_nat_def, auto)
huffman@31706
   137
nipkow@31952
   138
lemma prime_ge_2_int [elim]: "prime (p::int) \<Longrightarrow> p >= 2"
huffman@31706
   139
  by (unfold prime_int_def prime_nat_def, auto)
wenzelm@22367
   140
huffman@31706
   141
huffman@31706
   142
lemma prime_int_altdef: "prime (p::int) = (1 < p \<and> (\<forall>m \<ge> 0. m dvd p \<longrightarrow>
huffman@31706
   143
    m = 1 \<or> m = p))"
huffman@31706
   144
  using prime_nat_def [transferred]
huffman@31706
   145
    apply (case_tac "p >= 0")
nipkow@31952
   146
    by (blast, auto simp add: prime_ge_0_int)
huffman@31706
   147
nipkow@31952
   148
lemma prime_imp_coprime_nat: "prime (p::nat) \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
huffman@31706
   149
  apply (unfold prime_nat_def)
nipkow@31952
   150
  apply (metis gcd_dvd1_nat gcd_dvd2_nat)
huffman@31706
   151
  done
huffman@31706
   152
nipkow@31952
   153
lemma prime_imp_coprime_int: "prime (p::int) \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
huffman@31706
   154
  apply (unfold prime_int_altdef)
nipkow@31952
   155
  apply (metis gcd_dvd1_int gcd_dvd2_int gcd_ge_0_int)
chaieb@27568
   156
  done
chaieb@27568
   157
nipkow@31952
   158
lemma prime_dvd_mult_nat: "prime (p::nat) \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
nipkow@31952
   159
  by (blast intro: coprime_dvd_mult_nat prime_imp_coprime_nat)
huffman@31706
   160
nipkow@31952
   161
lemma prime_dvd_mult_int: "prime (p::int) \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
nipkow@31952
   162
  by (blast intro: coprime_dvd_mult_int prime_imp_coprime_int)
huffman@31706
   163
nipkow@31952
   164
lemma prime_dvd_mult_eq_nat [simp]: "prime (p::nat) \<Longrightarrow>
huffman@31706
   165
    p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
   166
  by (rule iffI, rule prime_dvd_mult_nat, auto)
chaieb@27568
   167
nipkow@31952
   168
lemma prime_dvd_mult_eq_int [simp]: "prime (p::int) \<Longrightarrow>
huffman@31706
   169
    p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
   170
  by (rule iffI, rule prime_dvd_mult_int, auto)
chaieb@27568
   171
nipkow@31952
   172
lemma not_prime_eq_prod_nat: "(n::nat) > 1 \<Longrightarrow> ~ prime n \<Longrightarrow>
huffman@31706
   173
    EX m k. n = m * k & 1 < m & m < n & 1 < k & k < n"
huffman@31706
   174
  unfolding prime_nat_def dvd_def apply auto
nipkow@31992
   175
  by(metis mult_commute linorder_neq_iff linorder_not_le mult_1 n_less_n_mult_m one_le_mult_iff less_imp_le_nat)
chaieb@27568
   176
nipkow@31952
   177
lemma not_prime_eq_prod_int: "(n::int) > 1 \<Longrightarrow> ~ prime n \<Longrightarrow>
huffman@31706
   178
    EX m k. n = m * k & 1 < m & m < n & 1 < k & k < n"
huffman@31706
   179
  unfolding prime_int_altdef dvd_def
huffman@31706
   180
  apply auto
nipkow@31992
   181
  by(metis div_mult_self1_is_id div_mult_self2_is_id int_div_less_self int_one_le_iff_zero_less zero_less_mult_pos zless_le)
chaieb@27568
   182
nipkow@31952
   183
lemma prime_dvd_power_nat [rule_format]: "prime (p::nat) -->
huffman@31706
   184
    n > 0 --> (p dvd x^n --> p dvd x)"
huffman@31706
   185
  by (induct n rule: nat_induct, auto)
chaieb@27568
   186
nipkow@31952
   187
lemma prime_dvd_power_int [rule_format]: "prime (p::int) -->
huffman@31706
   188
    n > 0 --> (p dvd x^n --> p dvd x)"
huffman@31706
   189
  apply (induct n rule: nat_induct, auto)
nipkow@31952
   190
  apply (frule prime_ge_0_int)
huffman@31706
   191
  apply auto
huffman@31706
   192
done
huffman@31706
   193
nipkow@32007
   194
subsubsection{* Make prime naively executable *}
nipkow@32007
   195
nipkow@32007
   196
lemma zero_not_prime_nat [simp]: "~prime (0::nat)"
nipkow@32007
   197
  by (simp add: prime_nat_def)
nipkow@32007
   198
nipkow@32007
   199
lemma zero_not_prime_int [simp]: "~prime (0::int)"
nipkow@32007
   200
  by (simp add: prime_int_def)
nipkow@32007
   201
nipkow@32007
   202
lemma one_not_prime_nat [simp]: "~prime (1::nat)"
nipkow@32007
   203
  by (simp add: prime_nat_def)
nipkow@32007
   204
nipkow@32007
   205
lemma Suc_0_not_prime_nat [simp]: "~prime (Suc 0)"
nipkow@32007
   206
  by (simp add: prime_nat_def One_nat_def)
nipkow@32007
   207
nipkow@32007
   208
lemma one_not_prime_int [simp]: "~prime (1::int)"
nipkow@32007
   209
  by (simp add: prime_int_def)
nipkow@32007
   210
nipkow@32007
   211
lemma prime_nat_code[code]:
nipkow@32007
   212
 "prime(p::nat) = (p > 1 & (ALL n : {1<..<p}. ~(n dvd p)))"
nipkow@32007
   213
apply(simp add: Ball_def)
nipkow@32007
   214
apply (metis less_not_refl prime_nat_def dvd_triv_right not_prime_eq_prod_nat)
nipkow@32007
   215
done
nipkow@32007
   216
nipkow@32007
   217
lemma prime_nat_simp:
nipkow@32007
   218
 "prime(p::nat) = (p > 1 & (list_all (%n. ~ n dvd p) [2..<p]))"
nipkow@32007
   219
apply(simp only:prime_nat_code list_ball_code greaterThanLessThan_upt)
nipkow@32007
   220
apply(simp add:nat_number One_nat_def)
nipkow@32007
   221
done
nipkow@32007
   222
nipkow@32007
   223
lemmas prime_nat_simp_number_of[simp] = prime_nat_simp[of "number_of m", standard]
nipkow@32007
   224
nipkow@32007
   225
lemma prime_int_code[code]:
nipkow@32007
   226
  "prime(p::int) = (p > 1 & (ALL n : {1<..<p}. ~(n dvd p)))" (is "?L = ?R")
nipkow@32007
   227
proof
nipkow@32007
   228
  assume "?L" thus "?R"
nipkow@32007
   229
    by (clarsimp simp: prime_gt_1_int) (metis int_one_le_iff_zero_less prime_int_altdef zless_le)
nipkow@32007
   230
next
nipkow@32007
   231
    assume "?R" thus "?L" by (clarsimp simp:Ball_def) (metis dvdI not_prime_eq_prod_int)
nipkow@32007
   232
qed
nipkow@32007
   233
nipkow@32007
   234
lemma prime_int_simp:
nipkow@32007
   235
  "prime(p::int) = (p > 1 & (list_all (%n. ~ n dvd p) [2..p - 1]))"
nipkow@32007
   236
apply(simp only:prime_int_code list_ball_code greaterThanLessThan_upto)
nipkow@32007
   237
apply simp
nipkow@32007
   238
done
nipkow@32007
   239
nipkow@32007
   240
lemmas prime_int_simp_number_of[simp] = prime_int_simp[of "number_of m", standard]
nipkow@32007
   241
nipkow@32007
   242
lemma two_is_prime_nat [simp]: "prime (2::nat)"
nipkow@32007
   243
by simp
nipkow@32007
   244
nipkow@32007
   245
lemma two_is_prime_int [simp]: "prime (2::int)"
nipkow@32007
   246
by simp
nipkow@32007
   247
nipkow@32111
   248
text{* A bit of regression testing: *}
nipkow@32111
   249
nipkow@32111
   250
lemma "prime(97::nat)"
nipkow@32111
   251
by simp
nipkow@32111
   252
nipkow@32111
   253
lemma "prime(97::int)"
nipkow@32111
   254
by simp
nipkow@32111
   255
nipkow@32111
   256
lemma "prime(997::nat)"
nipkow@32111
   257
by eval
nipkow@32111
   258
nipkow@32111
   259
lemma "prime(997::int)"
nipkow@32111
   260
by eval
nipkow@32111
   261
nipkow@32007
   262
nipkow@32007
   263
lemma prime_imp_power_coprime_nat: "prime (p::nat) \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
nipkow@31952
   264
  apply (rule coprime_exp_nat)
nipkow@31952
   265
  apply (subst gcd_commute_nat)
nipkow@31952
   266
  apply (erule (1) prime_imp_coprime_nat)
huffman@31706
   267
done
chaieb@27568
   268
nipkow@32007
   269
lemma prime_imp_power_coprime_int: "prime (p::int) \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
nipkow@31952
   270
  apply (rule coprime_exp_int)
nipkow@31952
   271
  apply (subst gcd_commute_int)
nipkow@31952
   272
  apply (erule (1) prime_imp_coprime_int)
huffman@31706
   273
done
chaieb@27568
   274
nipkow@31952
   275
lemma primes_coprime_nat: "prime (p::nat) \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
nipkow@31952
   276
  apply (rule prime_imp_coprime_nat, assumption)
huffman@31706
   277
  apply (unfold prime_nat_def, auto)
huffman@31706
   278
done
chaieb@27568
   279
nipkow@31952
   280
lemma primes_coprime_int: "prime (p::int) \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
nipkow@31952
   281
  apply (rule prime_imp_coprime_int, assumption)
huffman@31706
   282
  apply (unfold prime_int_altdef, clarify)
huffman@31706
   283
  apply (drule_tac x = q in spec)
huffman@31706
   284
  apply (drule_tac x = p in spec)
huffman@31706
   285
  apply auto
huffman@31706
   286
done
chaieb@27568
   287
nipkow@32007
   288
lemma primes_imp_powers_coprime_nat: "prime (p::nat) \<Longrightarrow> prime q \<Longrightarrow> p ~= q \<Longrightarrow> coprime (p^m) (q^n)"
nipkow@31952
   289
  by (rule coprime_exp2_nat, rule primes_coprime_nat)
chaieb@27568
   290
nipkow@32007
   291
lemma primes_imp_powers_coprime_int: "prime (p::int) \<Longrightarrow> prime q \<Longrightarrow> p ~= q \<Longrightarrow> coprime (p^m) (q^n)"
nipkow@31952
   292
  by (rule coprime_exp2_int, rule primes_coprime_int)
chaieb@27568
   293
nipkow@31952
   294
lemma prime_factor_nat: "n \<noteq> (1::nat) \<Longrightarrow> \<exists> p. prime p \<and> p dvd n"
huffman@31706
   295
  apply (induct n rule: nat_less_induct)
huffman@31706
   296
  apply (case_tac "n = 0")
nipkow@31952
   297
  using two_is_prime_nat apply blast
huffman@31706
   298
  apply (case_tac "prime n")
huffman@31706
   299
  apply blast
huffman@31706
   300
  apply (subgoal_tac "n > 1")
nipkow@31952
   301
  apply (frule (1) not_prime_eq_prod_nat)
huffman@31706
   302
  apply (auto intro: dvd_mult dvd_mult2)
huffman@31706
   303
done
chaieb@23244
   304
huffman@31706
   305
(* An Isar version:
huffman@31706
   306
nipkow@31952
   307
lemma prime_factor_b_nat:
huffman@31706
   308
  fixes n :: nat
huffman@31706
   309
  assumes "n \<noteq> 1"
huffman@31706
   310
  shows "\<exists>p. prime p \<and> p dvd n"
nipkow@23983
   311
huffman@31706
   312
using `n ~= 1`
nipkow@31952
   313
proof (induct n rule: less_induct_nat)
huffman@31706
   314
  fix n :: nat
huffman@31706
   315
  assume "n ~= 1" and
huffman@31706
   316
    ih: "\<forall>m<n. m \<noteq> 1 \<longrightarrow> (\<exists>p. prime p \<and> p dvd m)"
huffman@31706
   317
  thus "\<exists>p. prime p \<and> p dvd n"
huffman@31706
   318
  proof -
huffman@31706
   319
  {
huffman@31706
   320
    assume "n = 0"
nipkow@31952
   321
    moreover note two_is_prime_nat
huffman@31706
   322
    ultimately have ?thesis
nipkow@31952
   323
      by (auto simp del: two_is_prime_nat)
huffman@31706
   324
  }
huffman@31706
   325
  moreover
huffman@31706
   326
  {
huffman@31706
   327
    assume "prime n"
huffman@31706
   328
    hence ?thesis by auto
huffman@31706
   329
  }
huffman@31706
   330
  moreover
huffman@31706
   331
  {
huffman@31706
   332
    assume "n ~= 0" and "~ prime n"
huffman@31706
   333
    with `n ~= 1` have "n > 1" by auto
nipkow@31952
   334
    with `~ prime n` and not_prime_eq_prod_nat obtain m k where
huffman@31706
   335
      "n = m * k" and "1 < m" and "m < n" by blast
huffman@31706
   336
    with ih obtain p where "prime p" and "p dvd m" by blast
huffman@31706
   337
    with `n = m * k` have ?thesis by auto
huffman@31706
   338
  }
huffman@31706
   339
  ultimately show ?thesis by blast
huffman@31706
   340
  qed
nipkow@23983
   341
qed
nipkow@23983
   342
huffman@31706
   343
*)
huffman@31706
   344
huffman@31706
   345
text {* One property of coprimality is easier to prove via prime factors. *}
huffman@31706
   346
nipkow@31952
   347
lemma prime_divprod_pow_nat:
huffman@31706
   348
  assumes p: "prime (p::nat)" and ab: "coprime a b" and pab: "p^n dvd a * b"
huffman@31706
   349
  shows "p^n dvd a \<or> p^n dvd b"
huffman@31706
   350
proof-
huffman@31706
   351
  {assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis
huffman@31706
   352
      apply (cases "n=0", simp_all)
huffman@31706
   353
      apply (cases "a=1", simp_all) done}
huffman@31706
   354
  moreover
huffman@31706
   355
  {assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1"
huffman@31706
   356
    then obtain m where m: "n = Suc m" by (cases n, auto)
huffman@31706
   357
    from n have "p dvd p^n" by (intro dvd_power, auto)
huffman@31706
   358
    also note pab
huffman@31706
   359
    finally have pab': "p dvd a * b".
nipkow@31952
   360
    from prime_dvd_mult_nat[OF p pab']
huffman@31706
   361
    have "p dvd a \<or> p dvd b" .
huffman@31706
   362
    moreover
nipkow@33946
   363
    { assume pa: "p dvd a"
nipkow@31952
   364
      from coprime_common_divisor_nat [OF ab, OF pa] p have "\<not> p dvd b" by auto
huffman@31706
   365
      with p have "coprime b p"
nipkow@31952
   366
        by (subst gcd_commute_nat, intro prime_imp_coprime_nat)
huffman@31706
   367
      hence pnb: "coprime (p^n) b"
nipkow@31952
   368
        by (subst gcd_commute_nat, rule coprime_exp_nat)
nipkow@33946
   369
      from coprime_dvd_mult_nat[OF pnb pab] have ?thesis by blast }
huffman@31706
   370
    moreover
nipkow@33946
   371
    { assume pb: "p dvd b"
huffman@31706
   372
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
nipkow@31952
   373
      from coprime_common_divisor_nat [OF ab, of p] pb p have "\<not> p dvd a"
huffman@31706
   374
        by auto
huffman@31706
   375
      with p have "coprime a p"
nipkow@31952
   376
        by (subst gcd_commute_nat, intro prime_imp_coprime_nat)
huffman@31706
   377
      hence pna: "coprime (p^n) a"
nipkow@31952
   378
        by (subst gcd_commute_nat, rule coprime_exp_nat)
nipkow@33946
   379
      from coprime_dvd_mult_nat[OF pna pnba] have ?thesis by blast }
huffman@31706
   380
    ultimately have ?thesis by blast}
huffman@31706
   381
  ultimately show ?thesis by blast
nipkow@23983
   382
qed
nipkow@23983
   383
avigad@32036
   384
subsection {* Infinitely many primes *}
avigad@32036
   385
avigad@32036
   386
lemma next_prime_bound: "\<exists>(p::nat). prime p \<and> n < p \<and> p <= fact n + 1"
avigad@32036
   387
proof-
avigad@32036
   388
  have f1: "fact n + 1 \<noteq> 1" using fact_ge_one_nat [of n] by arith 
avigad@32036
   389
  from prime_factor_nat [OF f1]
avigad@32036
   390
      obtain p where "prime p" and "p dvd fact n + 1" by auto
avigad@32036
   391
  hence "p \<le> fact n + 1" 
avigad@32036
   392
    by (intro dvd_imp_le, auto)
avigad@32036
   393
  {assume "p \<le> n"
avigad@32036
   394
    from `prime p` have "p \<ge> 1" 
avigad@32036
   395
      by (cases p, simp_all)
avigad@32036
   396
    with `p <= n` have "p dvd fact n" 
avigad@32036
   397
      by (intro dvd_fact_nat)
avigad@32036
   398
    with `p dvd fact n + 1` have "p dvd fact n + 1 - fact n"
avigad@32036
   399
      by (rule dvd_diff_nat)
avigad@32036
   400
    hence "p dvd 1" by simp
avigad@32036
   401
    hence "p <= 1" by auto
avigad@32036
   402
    moreover from `prime p` have "p > 1" by auto
avigad@32036
   403
    ultimately have False by auto}
avigad@32036
   404
  hence "n < p" by arith
avigad@32036
   405
  with `prime p` and `p <= fact n + 1` show ?thesis by auto
avigad@32036
   406
qed
avigad@32036
   407
avigad@32036
   408
lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)" 
avigad@32036
   409
using next_prime_bound by auto
avigad@32036
   410
avigad@32036
   411
lemma primes_infinite: "\<not> (finite {(p::nat). prime p})"
avigad@32036
   412
proof
avigad@32036
   413
  assume "finite {(p::nat). prime p}"
avigad@32036
   414
  with Max_ge have "(EX b. (ALL x : {(p::nat). prime p}. x <= b))"
avigad@32036
   415
    by auto
avigad@32036
   416
  then obtain b where "ALL (x::nat). prime x \<longrightarrow> x <= b"
avigad@32036
   417
    by auto
avigad@32036
   418
  with bigger_prime [of b] show False by auto
avigad@32036
   419
qed
avigad@32036
   420
avigad@32036
   421
wenzelm@21256
   422
end