src/HOL/Number_Theory/Primes.thy
author Manuel Eberl <eberlm@in.tum.de>
Fri Feb 26 22:15:09 2016 +0100 (2016-02-26)
changeset 62429 25271ff79171
parent 62410 2fc7a8d9c529
child 62481 b5d8e57826df
permissions -rw-r--r--
Tuned Euclidean Rings/GCD rings
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
wenzelm@58623
    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
wenzelm@60526
    28
section \<open>Primes\<close>
wenzelm@21256
    29
haftmann@32479
    30
theory Primes
lp15@59669
    31
imports "~~/src/HOL/GCD" "~~/src/HOL/Binomial"
huffman@31706
    32
begin
huffman@31706
    33
lp15@55242
    34
declare [[coercion int]]
lp15@55242
    35
declare [[coercion_enabled]]
huffman@31706
    36
lp15@55242
    37
definition prime :: "nat \<Rightarrow> bool"
haftmann@60804
    38
  where "prime p = (1 < p \<and> (\<forall>m. m dvd p \<longrightarrow> m = 1 \<or> m = p))"
huffman@31706
    39
wenzelm@60526
    40
subsection \<open>Primes\<close>
huffman@31706
    41
lp15@55242
    42
lemma prime_odd_nat: "prime p \<Longrightarrow> p > 2 \<Longrightarrow> odd p"
lp15@62410
    43
  using nat_dvd_1_iff_1 odd_one prime_def by blast
huffman@31706
    44
lp15@61762
    45
lemma prime_gt_0_nat: "prime p \<Longrightarrow> p > 0"
lp15@61762
    46
  unfolding prime_def by auto
chaieb@22027
    47
lp15@61762
    48
lemma prime_ge_1_nat: "prime p \<Longrightarrow> p >= 1"
lp15@61762
    49
  unfolding prime_def by auto
chaieb@22027
    50
lp15@61762
    51
lemma prime_gt_1_nat: "prime p \<Longrightarrow> p > 1"
lp15@61762
    52
  unfolding prime_def by auto
wenzelm@22367
    53
lp15@61762
    54
lemma prime_ge_Suc_0_nat: "prime p \<Longrightarrow> p >= Suc 0"
lp15@61762
    55
  unfolding prime_def by auto
huffman@31706
    56
lp15@61762
    57
lemma prime_gt_Suc_0_nat: "prime p \<Longrightarrow> p > Suc 0"
lp15@61762
    58
  unfolding prime_def by auto
huffman@31706
    59
lp15@61762
    60
lemma prime_ge_2_nat: "prime p \<Longrightarrow> p >= 2"
lp15@61762
    61
  unfolding prime_def by auto
huffman@31706
    62
lp15@55242
    63
lemma prime_imp_coprime_nat: "prime p \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
lp15@61762
    64
  apply (unfold prime_def)
haftmann@62348
    65
  apply (metis gcd_dvd1 gcd_dvd2)
huffman@31706
    66
  done
huffman@31706
    67
lp15@59669
    68
lemma prime_int_altdef:
lp15@55242
    69
  "prime p = (1 < p \<and> (\<forall>m::int. m \<ge> 0 \<longrightarrow> m dvd p \<longrightarrow>
lp15@55242
    70
    m = 1 \<or> m = p))"
lp15@55242
    71
  apply (simp add: prime_def)
lp15@55242
    72
  apply (auto simp add: )
haftmann@62348
    73
  apply (metis One_nat_def of_nat_1 nat_0_le nat_dvd_iff)
lp15@55242
    74
  apply (metis zdvd_int One_nat_def le0 of_nat_0 of_nat_1 of_nat_eq_iff of_nat_le_iff)
lp15@55242
    75
  done
lp15@55242
    76
lp15@55242
    77
lemma prime_imp_coprime_int:
lp15@55242
    78
  fixes n::int shows "prime p \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
huffman@31706
    79
  apply (unfold prime_int_altdef)
haftmann@62348
    80
  apply (metis gcd_dvd1 gcd_dvd2 gcd_ge_0_int)
chaieb@27568
    81
  done
chaieb@27568
    82
lp15@55242
    83
lemma prime_dvd_mult_nat: "prime p \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
haftmann@62348
    84
  by (blast intro: coprime_dvd_mult prime_imp_coprime_nat)
huffman@31706
    85
lp15@59669
    86
lemma prime_dvd_mult_int:
lp15@55242
    87
  fixes n::int shows "prime p \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
haftmann@62348
    88
  by (blast intro: coprime_dvd_mult prime_imp_coprime_int)
huffman@31706
    89
lp15@55242
    90
lemma prime_dvd_mult_eq_nat [simp]: "prime p \<Longrightarrow>
huffman@31706
    91
    p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
    92
  by (rule iffI, rule prime_dvd_mult_nat, auto)
chaieb@27568
    93
lp15@55242
    94
lemma prime_dvd_mult_eq_int [simp]:
lp15@59669
    95
  fixes n::int
lp15@55242
    96
  shows "prime p \<Longrightarrow> p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
    97
  by (rule iffI, rule prime_dvd_mult_int, auto)
chaieb@27568
    98
nipkow@31952
    99
lemma not_prime_eq_prod_nat: "(n::nat) > 1 \<Longrightarrow> ~ prime n \<Longrightarrow>
huffman@31706
   100
    EX m k. n = m * k & 1 < m & m < n & 1 < k & k < n"
lp15@61762
   101
  unfolding prime_def dvd_def apply auto
haftmann@57512
   102
  by (metis mult.commute linorder_neq_iff linorder_not_le mult_1
wenzelm@44872
   103
      n_less_n_mult_m one_le_mult_iff less_imp_le_nat)
chaieb@27568
   104
lp15@55242
   105
lemma prime_dvd_power_nat: "prime p \<Longrightarrow> p dvd x^n \<Longrightarrow> p dvd x"
huffman@53598
   106
  by (induct n) auto
chaieb@27568
   107
lp15@55242
   108
lemma prime_dvd_power_int:
lp15@55242
   109
  fixes x::int shows "prime p \<Longrightarrow> p dvd x^n \<Longrightarrow> p dvd x"
lp15@55242
   110
  by (induct n) auto
huffman@31706
   111
lp15@55242
   112
lemma prime_dvd_power_nat_iff: "prime p \<Longrightarrow> n > 0 \<Longrightarrow>
huffman@53598
   113
    p dvd x^n \<longleftrightarrow> p dvd x"
huffman@53598
   114
  by (cases n) (auto elim: prime_dvd_power_nat)
huffman@53598
   115
lp15@55242
   116
lemma prime_dvd_power_int_iff:
lp15@59669
   117
  fixes x::int
lp15@55242
   118
  shows "prime p \<Longrightarrow> n > 0 \<Longrightarrow> p dvd x^n \<longleftrightarrow> p dvd x"
huffman@53598
   119
  by (cases n) (auto elim: prime_dvd_power_int)
huffman@53598
   120
huffman@53598
   121
wenzelm@60526
   122
subsubsection \<open>Make prime naively executable\<close>
nipkow@32007
   123
nipkow@32007
   124
lemma zero_not_prime_nat [simp]: "~prime (0::nat)"
lp15@61762
   125
  by (simp add: prime_def)
nipkow@32007
   126
nipkow@32007
   127
lemma one_not_prime_nat [simp]: "~prime (1::nat)"
lp15@61762
   128
  by (simp add: prime_def)
nipkow@32007
   129
nipkow@32007
   130
lemma Suc_0_not_prime_nat [simp]: "~prime (Suc 0)"
lp15@61762
   131
  by (simp add: prime_def)
nipkow@32007
   132
haftmann@37607
   133
lemma prime_nat_code [code]:
lp15@55242
   134
    "prime p \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {1<..<p}. ~ n dvd p)"
wenzelm@44872
   135
  apply (simp add: Ball_def)
lp15@61762
   136
  apply (metis One_nat_def less_not_refl prime_def dvd_triv_right not_prime_eq_prod_nat)
wenzelm@44872
   137
  done
nipkow@32007
   138
nipkow@32007
   139
lemma prime_nat_simp:
lp15@55242
   140
    "prime p \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> set [2..<p]. \<not> n dvd p)"
wenzelm@44872
   141
  by (auto simp add: prime_nat_code)
nipkow@32007
   142
huffman@47108
   143
lemmas prime_nat_simp_numeral [simp] = prime_nat_simp [of "numeral m"] for m
nipkow@32007
   144
nipkow@32007
   145
lemma two_is_prime_nat [simp]: "prime (2::nat)"
wenzelm@44872
   146
  by simp
nipkow@32007
   147
wenzelm@60526
   148
text\<open>A bit of regression testing:\<close>
nipkow@32111
   149
haftmann@58954
   150
lemma "prime(97::nat)" by simp
wenzelm@44872
   151
lemma "prime(997::nat)" by eval
nipkow@32111
   152
nipkow@32007
   153
lp15@55242
   154
lemma prime_imp_power_coprime_nat: "prime p \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
eberlm@62429
   155
  by (metis coprime_exp gcd.commute prime_imp_coprime_nat)
chaieb@27568
   156
lp15@55242
   157
lemma prime_imp_power_coprime_int:
lp15@55242
   158
  fixes a::int shows "prime p \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
eberlm@62429
   159
  by (metis coprime_exp gcd.commute prime_imp_coprime_int)
chaieb@27568
   160
lp15@55242
   161
lemma primes_coprime_nat: "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
lp15@55130
   162
  by (metis gcd_nat.absorb1 gcd_nat.absorb2 prime_imp_coprime_nat)
chaieb@27568
   163
wenzelm@44872
   164
lemma primes_imp_powers_coprime_nat:
lp15@55242
   165
    "prime p \<Longrightarrow> prime q \<Longrightarrow> p ~= q \<Longrightarrow> coprime (p^m) (q^n)"
nipkow@31952
   166
  by (rule coprime_exp2_nat, rule primes_coprime_nat)
chaieb@27568
   167
haftmann@62349
   168
lemma prime_factor_nat:
haftmann@62349
   169
  "n \<noteq> (1::nat) \<Longrightarrow> \<exists>p. prime p \<and> p dvd n"
haftmann@62349
   170
proof (induct n rule: nat_less_induct)
haftmann@62349
   171
  case (1 n) show ?case
haftmann@62349
   172
  proof (cases "n = 0")
haftmann@62349
   173
    case True then show ?thesis
haftmann@62349
   174
    by (auto intro: two_is_prime_nat)
haftmann@62349
   175
  next
haftmann@62349
   176
    case False with "1.prems" have "n > 1" by simp
haftmann@62349
   177
    with "1.hyps" show ?thesis
haftmann@62349
   178
    by (metis One_nat_def dvd_mult dvd_refl not_prime_eq_prod_nat order_less_irrefl)
haftmann@62349
   179
  qed
haftmann@62349
   180
qed
chaieb@23244
   181
wenzelm@60526
   182
text \<open>One property of coprimality is easier to prove via prime factors.\<close>
huffman@31706
   183
nipkow@31952
   184
lemma prime_divprod_pow_nat:
lp15@55242
   185
  assumes p: "prime p" and ab: "coprime a b" and pab: "p^n dvd a * b"
huffman@31706
   186
  shows "p^n dvd a \<or> p^n dvd b"
huffman@31706
   187
proof-
wenzelm@44872
   188
  { assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis
huffman@31706
   189
      apply (cases "n=0", simp_all)
wenzelm@44872
   190
      apply (cases "a=1", simp_all)
wenzelm@44872
   191
      done }
huffman@31706
   192
  moreover
wenzelm@44872
   193
  { assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1"
wenzelm@44872
   194
    then obtain m where m: "n = Suc m" by (cases n) auto
wenzelm@44872
   195
    from n have "p dvd p^n" apply (intro dvd_power) apply auto done
huffman@31706
   196
    also note pab
huffman@31706
   197
    finally have pab': "p dvd a * b".
nipkow@31952
   198
    from prime_dvd_mult_nat[OF p pab']
huffman@31706
   199
    have "p dvd a \<or> p dvd b" .
huffman@31706
   200
    moreover
nipkow@33946
   201
    { assume pa: "p dvd a"
nipkow@31952
   202
      from coprime_common_divisor_nat [OF ab, OF pa] p have "\<not> p dvd b" by auto
huffman@31706
   203
      with p have "coprime b p"
haftmann@62348
   204
        by (subst gcd.commute, intro prime_imp_coprime_nat)
wenzelm@44872
   205
      then have pnb: "coprime (p^n) b"
eberlm@62429
   206
        by (subst gcd.commute, rule coprime_exp)
haftmann@62348
   207
      from coprime_dvd_mult[OF pnb pab] have ?thesis by blast }
huffman@31706
   208
    moreover
nipkow@33946
   209
    { assume pb: "p dvd b"
haftmann@57512
   210
      have pnba: "p^n dvd b*a" using pab by (simp add: mult.commute)
nipkow@31952
   211
      from coprime_common_divisor_nat [OF ab, of p] pb p have "\<not> p dvd a"
huffman@31706
   212
        by auto
huffman@31706
   213
      with p have "coprime a p"
haftmann@62348
   214
        by (subst gcd.commute, intro prime_imp_coprime_nat)
wenzelm@44872
   215
      then have pna: "coprime (p^n) a"
eberlm@62429
   216
        by (subst gcd.commute, rule coprime_exp)
haftmann@62348
   217
      from coprime_dvd_mult[OF pna pnba] have ?thesis by blast }
wenzelm@44872
   218
    ultimately have ?thesis by blast }
huffman@31706
   219
  ultimately show ?thesis by blast
nipkow@23983
   220
qed
nipkow@23983
   221
wenzelm@44872
   222
wenzelm@60526
   223
subsection \<open>Infinitely many primes\<close>
avigad@32036
   224
lp15@55242
   225
lemma next_prime_bound: "\<exists>p. prime p \<and> n < p \<and> p <= fact n + 1"
avigad@32036
   226
proof-
lp15@59730
   227
  have f1: "fact n + 1 \<noteq> (1::nat)" using fact_ge_1 [of n, where 'a=nat] by arith
avigad@32036
   228
  from prime_factor_nat [OF f1]
wenzelm@44872
   229
  obtain p where "prime p" and "p dvd fact n + 1" by auto
wenzelm@44872
   230
  then have "p \<le> fact n + 1" apply (intro dvd_imp_le) apply auto done
wenzelm@44872
   231
  { assume "p \<le> n"
wenzelm@60526
   232
    from \<open>prime p\<close> have "p \<ge> 1"
avigad@32036
   233
      by (cases p, simp_all)
wenzelm@60526
   234
    with \<open>p <= n\<close> have "p dvd fact n"
lp15@59730
   235
      by (intro dvd_fact)
wenzelm@60526
   236
    with \<open>p dvd fact n + 1\<close> have "p dvd fact n + 1 - fact n"
avigad@32036
   237
      by (rule dvd_diff_nat)
wenzelm@44872
   238
    then have "p dvd 1" by simp
wenzelm@44872
   239
    then have "p <= 1" by auto
lp15@61762
   240
    moreover from \<open>prime p\<close> have "p > 1"
lp15@61762
   241
      using prime_def by blast
avigad@32036
   242
    ultimately have False by auto}
wenzelm@44872
   243
  then have "n < p" by presburger
wenzelm@60526
   244
  with \<open>prime p\<close> and \<open>p <= fact n + 1\<close> show ?thesis by auto
avigad@32036
   245
qed
avigad@32036
   246
lp15@59669
   247
lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)"
wenzelm@44872
   248
  using next_prime_bound by auto
avigad@32036
   249
avigad@32036
   250
lemma primes_infinite: "\<not> (finite {(p::nat). prime p})"
avigad@32036
   251
proof
avigad@32036
   252
  assume "finite {(p::nat). prime p}"
avigad@32036
   253
  with Max_ge have "(EX b. (ALL x : {(p::nat). prime p}. x <= b))"
avigad@32036
   254
    by auto
avigad@32036
   255
  then obtain b where "ALL (x::nat). prime x \<longrightarrow> x <= b"
avigad@32036
   256
    by auto
wenzelm@44872
   257
  with bigger_prime [of b] show False
wenzelm@44872
   258
    by auto
avigad@32036
   259
qed
avigad@32036
   260
wenzelm@60526
   261
subsection\<open>Powers of Primes\<close>
lp15@55215
   262
wenzelm@60526
   263
text\<open>Versions for type nat only\<close>
lp15@55215
   264
lp15@59669
   265
lemma prime_product:
lp15@55215
   266
  fixes p::nat
lp15@55215
   267
  assumes "prime (p * q)"
lp15@55215
   268
  shows "p = 1 \<or> q = 1"
lp15@55215
   269
proof -
lp15@59669
   270
  from assms have
lp15@55215
   271
    "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
lp15@61762
   272
    unfolding prime_def by auto
wenzelm@60526
   273
  from \<open>1 < p * q\<close> have "p \<noteq> 0" by (cases p) auto
lp15@55215
   274
  then have Q: "p = p * q \<longleftrightarrow> q = 1" by auto
lp15@55215
   275
  have "p dvd p * q" by simp
lp15@55215
   276
  then have "p = 1 \<or> p = p * q" by (rule P)
lp15@55215
   277
  then show ?thesis by (simp add: Q)
lp15@55215
   278
qed
lp15@55215
   279
lp15@59669
   280
lemma prime_exp:
lp15@55215
   281
  fixes p::nat
lp15@55215
   282
  shows "prime (p^n) \<longleftrightarrow> prime p \<and> n = 1"
lp15@55215
   283
proof(induct n)
lp15@55215
   284
  case 0 thus ?case by simp
lp15@55215
   285
next
lp15@55215
   286
  case (Suc n)
lp15@55215
   287
  {assume "p = 0" hence ?case by simp}
lp15@55215
   288
  moreover
lp15@55215
   289
  {assume "p=1" hence ?case by simp}
lp15@55215
   290
  moreover
lp15@55215
   291
  {assume p: "p \<noteq> 0" "p\<noteq>1"
lp15@55215
   292
    {assume pp: "prime (p^Suc n)"
lp15@55215
   293
      hence "p = 1 \<or> p^n = 1" using prime_product[of p "p^n"] by simp
lp15@55215
   294
      with p have n: "n = 0"
lp15@55215
   295
        by (metis One_nat_def nat_power_eq_Suc_0_iff)
lp15@55215
   296
      with pp have "prime p \<and> Suc n = 1" by simp}
lp15@55215
   297
    moreover
lp15@55215
   298
    {assume n: "prime p \<and> Suc n = 1" hence "prime (p^Suc n)" by simp}
lp15@55215
   299
    ultimately have ?case by blast}
lp15@55215
   300
  ultimately show ?case by blast
lp15@55215
   301
qed
lp15@55215
   302
lp15@59669
   303
lemma prime_power_mult:
lp15@55215
   304
  fixes p::nat
lp15@55215
   305
  assumes p: "prime p" and xy: "x * y = p ^ k"
lp15@55215
   306
  shows "\<exists>i j. x = p ^i \<and> y = p^ j"
lp15@55215
   307
using xy
lp15@55215
   308
proof(induct k arbitrary: x y)
lp15@55215
   309
  case 0 thus ?case apply simp by (rule exI[where x="0"], simp)
lp15@55215
   310
next
lp15@55215
   311
  case (Suc k x y)
lp15@55215
   312
  from Suc.prems have pxy: "p dvd x*y" by auto
lp15@55215
   313
  from Primes.prime_dvd_mult_nat [OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
lp15@59669
   314
  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp)
lp15@55215
   315
  {assume px: "p dvd x"
lp15@55215
   316
    then obtain d where d: "x = p*d" unfolding dvd_def by blast
lp15@55215
   317
    from Suc.prems d  have "p*d*y = p^Suc k" by simp
lp15@55215
   318
    hence th: "d*y = p^k" using p0 by simp
lp15@55215
   319
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
lp15@59669
   320
    with d have "x = p^Suc i" by simp
lp15@55215
   321
    with ij(2) have ?case by blast}
lp15@59669
   322
  moreover
lp15@55215
   323
  {assume px: "p dvd y"
lp15@55215
   324
    then obtain d where d: "y = p*d" unfolding dvd_def by blast
haftmann@57512
   325
    from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult.commute)
lp15@55215
   326
    hence th: "d*x = p^k" using p0 by simp
lp15@55215
   327
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
lp15@59669
   328
    with d have "y = p^Suc i" by simp
lp15@55215
   329
    with ij(2) have ?case by blast}
lp15@55215
   330
  ultimately show ?case  using pxyc by blast
lp15@55215
   331
qed
lp15@55215
   332
lp15@59669
   333
lemma prime_power_exp:
lp15@55215
   334
  fixes p::nat
lp15@59669
   335
  assumes p: "prime p" and n: "n \<noteq> 0"
lp15@55215
   336
    and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
lp15@55215
   337
  using n xn
lp15@55215
   338
proof(induct n arbitrary: k)
lp15@55215
   339
  case 0 thus ?case by simp
lp15@55215
   340
next
lp15@55215
   341
  case (Suc n k) hence th: "x*x^n = p^k" by simp
lp15@55215
   342
  {assume "n = 0" with Suc have ?case by simp (rule exI[where x="k"], simp)}
lp15@55215
   343
  moreover
lp15@55215
   344
  {assume n: "n \<noteq> 0"
lp15@59669
   345
    from prime_power_mult[OF p th]
lp15@55215
   346
    obtain i j where ij: "x = p^i" "x^n = p^j"by blast
lp15@55215
   347
    from Suc.hyps[OF n ij(2)] have ?case .}
lp15@55215
   348
  ultimately show ?case by blast
lp15@55215
   349
qed
lp15@55215
   350
lp15@55215
   351
lemma divides_primepow:
lp15@55215
   352
  fixes p::nat
lp15@59669
   353
  assumes p: "prime p"
lp15@55215
   354
  shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
lp15@55215
   355
proof
lp15@59669
   356
  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k"
haftmann@57512
   357
    unfolding dvd_def  apply (auto simp add: mult.commute) by blast
lp15@55215
   358
  from prime_power_mult[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
lp15@55215
   359
  from e ij have "p^(i + j) = p^k" by (simp add: power_add)
lp15@59669
   360
  hence "i + j = k" using p prime_gt_1_nat power_inject_exp[of p "i+j" k] by simp
lp15@55215
   361
  hence "i \<le> k" by arith
lp15@55215
   362
  with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
lp15@55215
   363
next
lp15@55215
   364
  {fix i assume H: "i \<le> k" "d = p^i"
lp15@55215
   365
    then obtain j where j: "k = i + j"
lp15@55215
   366
      by (metis le_add_diff_inverse)
lp15@55215
   367
    hence "p^k = p^j*d" using H(2) by (simp add: power_add)
lp15@55215
   368
    hence "d dvd p^k" unfolding dvd_def by auto}
lp15@55215
   369
  thus "\<exists>i\<le>k. d = p ^ i \<Longrightarrow> d dvd p ^ k" by blast
lp15@55215
   370
qed
lp15@55215
   371
wenzelm@60526
   372
subsection \<open>Chinese Remainder Theorem Variants\<close>
lp15@55238
   373
lp15@55238
   374
lemma bezout_gcd_nat:
lp15@55238
   375
  fixes a::nat shows "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
lp15@55238
   376
  using bezout_nat[of a b]
eberlm@62429
   377
by (metis bezout_nat diff_add_inverse gcd_add_mult gcd.commute
lp15@59669
   378
  gcd_nat.right_neutral mult_0)
lp15@55238
   379
lp15@55238
   380
lemma gcd_bezout_sum_nat:
lp15@59669
   381
  fixes a::nat
lp15@59669
   382
  assumes "a * x + b * y = d"
lp15@55238
   383
  shows "gcd a b dvd d"
lp15@55238
   384
proof-
lp15@55238
   385
  let ?g = "gcd a b"
lp15@59669
   386
    have dv: "?g dvd a*x" "?g dvd b * y"
lp15@55238
   387
      by simp_all
lp15@55238
   388
    from dvd_add[OF dv] assms
lp15@55238
   389
    show ?thesis by auto
lp15@55238
   390
qed
lp15@55238
   391
lp15@55238
   392
wenzelm@60526
   393
text \<open>A binary form of the Chinese Remainder Theorem.\<close>
lp15@55238
   394
lp15@59669
   395
lemma chinese_remainder:
lp15@55238
   396
  fixes a::nat  assumes ab: "coprime a b" and a: "a \<noteq> 0" and b: "b \<noteq> 0"
lp15@55238
   397
  shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
lp15@55238
   398
proof-
lp15@55238
   399
  from bezout_add_strong_nat[OF a, of b] bezout_add_strong_nat[OF b, of a]
lp15@59669
   400
  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1"
lp15@55238
   401
    and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
lp15@55238
   402
  then have d12: "d1 = 1" "d2 =1"
lp15@55238
   403
    by (metis ab coprime_nat)+
lp15@55238
   404
  let ?x = "v * a * x1 + u * b * x2"
lp15@55238
   405
  let ?q1 = "v * x1 + u * y2"
lp15@55238
   406
  let ?q2 = "v * y1 + u * x2"
lp15@59669
   407
  from dxy2(3)[simplified d12] dxy1(3)[simplified d12]
lp15@55238
   408
  have "?x = u + ?q1 * a" "?x = v + ?q2 * b"
lp15@55337
   409
    by algebra+
lp15@55238
   410
  thus ?thesis by blast
lp15@55238
   411
qed
lp15@55238
   412
wenzelm@60526
   413
text \<open>Primality\<close>
lp15@55238
   414
lp15@55238
   415
lemma coprime_bezout_strong:
lp15@55238
   416
  fixes a::nat assumes "coprime a b"  "b \<noteq> 1"
lp15@55238
   417
  shows "\<exists>x y. a * x = b * y + 1"
lp15@55238
   418
by (metis assms bezout_nat gcd_nat.left_neutral)
lp15@55238
   419
lp15@59669
   420
lemma bezout_prime:
lp15@55238
   421
  assumes p: "prime p" and pa: "\<not> p dvd a"
lp15@55238
   422
  shows "\<exists>x y. a*x = Suc (p*y)"
haftmann@62349
   423
proof -
lp15@55238
   424
  have ap: "coprime a p"
haftmann@60688
   425
    by (metis gcd.commute p pa prime_imp_coprime_nat)
haftmann@62349
   426
  moreover from p have "p \<noteq> 1" by auto
haftmann@62349
   427
  ultimately have "\<exists>x y. a * x = p * y + 1"
haftmann@62349
   428
    by (rule coprime_bezout_strong)
haftmann@62349
   429
  then show ?thesis by simp    
lp15@55238
   430
qed
lp15@55238
   431
wenzelm@21256
   432
end