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