src/HOL/Library/Primes.thy
author nipkow
Wed, 28 Jan 2009 16:29:16 +0100
changeset 29667 53103fc8ffa3
parent 28952 15a4b2cf8c34
child 30042 31039ee583fa
child 30240 5b25fee0362c
permissions -rw-r--r--
Replaced group_ and ring_simps by algebra_simps; removed compare_rls - use algebra_simps now
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
     1
(*  Title:      HOL/Library/Primes.thy
27106
ff27dc6e7d05 removed some dubious code lemmas
haftmann
parents: 26757
diff changeset
     2
    Author:     Amine Chaieb, Christophe Tabacznyj and Lawrence C Paulson
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     3
    Copyright   1996  University of Cambridge
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     4
*)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     5
16762
aafd23b47a5d moved gcd to new GCD.thy
nipkow
parents: 16663
diff changeset
     6
header {* Primality on nat *}
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     7
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
     8
theory Primes
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28562
diff changeset
     9
imports Plain "~~/src/HOL/ATP_Linkup" "~~/src/HOL/GCD" "~~/src/HOL/Parity"
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
    10
begin
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    11
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 16762
diff changeset
    12
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21256
diff changeset
    13
  coprime :: "nat => nat => bool" where
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
    14
  "coprime m n \<longleftrightarrow> gcd m n = 1"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    15
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21256
diff changeset
    16
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21256
diff changeset
    17
  prime :: "nat \<Rightarrow> bool" where
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 27670
diff changeset
    18
  [code del]: "prime p \<longleftrightarrow> (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    19
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    20
16762
aafd23b47a5d moved gcd to new GCD.thy
nipkow
parents: 16663
diff changeset
    21
lemma two_is_prime: "prime 2"
aafd23b47a5d moved gcd to new GCD.thy
nipkow
parents: 16663
diff changeset
    22
  apply (auto simp add: prime_def)
aafd23b47a5d moved gcd to new GCD.thy
nipkow
parents: 16663
diff changeset
    23
  apply (case_tac m)
aafd23b47a5d moved gcd to new GCD.thy
nipkow
parents: 16663
diff changeset
    24
   apply (auto dest!: dvd_imp_le)
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    25
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    26
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
    27
lemma prime_imp_relprime: "prime p ==> \<not> p dvd n ==> gcd p n = 1"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    28
  apply (auto simp add: prime_def)
23839
d9fa0f457d9a tidying using metis
paulson
parents: 22665
diff changeset
    29
  apply (metis One_nat_def gcd_dvd1 gcd_dvd2)
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    30
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    31
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    32
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    33
  This theorem leads immediately to a proof of the uniqueness of
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    34
  factorization.  If @{term p} divides a product of primes then it is
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    35
  one of those primes.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    36
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    37
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 15628
diff changeset
    38
lemma prime_dvd_mult: "prime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
12739
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
    39
  by (blast intro: relprime_dvd_mult prime_imp_relprime)
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    40
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 15628
diff changeset
    41
lemma prime_dvd_square: "prime p ==> p dvd m^Suc (Suc 0) ==> p dvd m"
12739
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
    42
  by (auto dest: prime_dvd_mult)
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
    43
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 15628
diff changeset
    44
lemma prime_dvd_power_two: "prime p ==> p dvd m\<twosuperior> ==> p dvd m"
14353
79f9fbef9106 Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents: 13187
diff changeset
    45
  by (rule prime_dvd_square) (simp_all add: power2_eq_square)
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    46
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    47
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    48
lemma exp_eq_1:"(x::nat)^n = 1 \<longleftrightarrow> x = 1 \<or> n = 0" by (induct n, auto)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    49
lemma exp_mono_lt: "(x::nat) ^ (Suc n) < y ^ (Suc n) \<longleftrightarrow> x < y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    50
  using power_less_imp_less_base[of x "Suc n" y] power_strict_mono[of x y "Suc n"]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    51
    by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    52
lemma exp_mono_le: "(x::nat) ^ (Suc n) \<le> y ^ (Suc n) \<longleftrightarrow> x \<le> y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    53
  by (simp only: linorder_not_less[symmetric] exp_mono_lt)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    54
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    55
lemma exp_mono_eq: "(x::nat) ^ Suc n = y ^ Suc n \<longleftrightarrow> x = y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    56
using power_inject_base[of x n y] by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    57
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    58
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    59
lemma even_square: assumes e: "even (n::nat)" shows "\<exists>x. n ^ 2 = 4*x"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    60
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    61
  from e have "2 dvd n" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    62
  then obtain k where k: "n = 2*k" using dvd_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    63
  hence "n^2 = 4* (k^2)" by (simp add: power2_eq_square)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    64
  thus ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    65
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    66
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    67
lemma odd_square: assumes e: "odd (n::nat)" shows "\<exists>x. n ^ 2 = 4*x + 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    68
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    69
  from e have np: "n > 0" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    70
  from e have "2 dvd (n - 1)" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    71
  then obtain k where "n - 1 = 2*k" using dvd_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    72
  hence k: "n = 2*k + 1"  using e by presburger 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    73
  hence "n^2 = 4* (k^2 + k) + 1" by algebra   
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    74
  thus ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    75
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    76
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    77
lemma diff_square: "(x::nat)^2 - y^2 = (x+y)*(x - y)" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    78
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    79
  have "x \<le> y \<or> y \<le> x" by (rule nat_le_linear)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    80
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    81
  {assume le: "x \<le> y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    82
    hence "x ^2 \<le> y^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    83
    with le have ?thesis by simp }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    84
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    85
  {assume le: "y \<le> x"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    86
    hence le2: "y ^2 \<le> x^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    87
    from le have "\<exists>z. y + z = x" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    88
    then obtain z where z: "x = y + z" by blast 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    89
    from le2 have "\<exists>z. x^2 = y^2 + z" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    90
    then obtain z2 where z2: "x^2 = y^2 + z2"  by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    91
    from z z2 have ?thesis apply simp by algebra }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    92
  ultimately show ?thesis by blast  
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    93
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    94
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
    95
text {* Elementary theory of divisibility *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    96
lemma divides_ge: "(a::nat) dvd b \<Longrightarrow> b = 0 \<or> a \<le> b" unfolding dvd_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    97
lemma divides_antisym: "(x::nat) dvd y \<and> y dvd x \<longleftrightarrow> x = y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    98
  using dvd_anti_sym[of x y] by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
    99
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   100
lemma divides_add_revr: assumes da: "(d::nat) dvd a" and dab:"d dvd (a + b)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   101
  shows "d dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   102
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   103
  from da obtain k where k:"a = d*k" by (auto simp add: dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   104
  from dab obtain k' where k': "a + b = d*k'" by (auto simp add: dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   105
  from k k' have "b = d *(k' - k)" by (simp add : diff_mult_distrib2)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   106
  thus ?thesis unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   107
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   108
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   109
declare nat_mult_dvd_cancel_disj[presburger]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   110
lemma nat_mult_dvd_cancel_disj'[presburger]: 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   111
  "(m\<Colon>nat)*k dvd n*k \<longleftrightarrow> k = 0 \<or> m dvd n" unfolding mult_commute[of m k] mult_commute[of n k] by presburger 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   112
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   113
lemma divides_mul_l: "(a::nat) dvd b ==> (c * a) dvd (c * b)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   114
  by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   115
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   116
lemma divides_mul_r: "(a::nat) dvd b ==> (a * c) dvd (b * c)" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   117
lemma divides_cases: "(n::nat) dvd m ==> m = 0 \<or> m = n \<or> 2 * n <= m" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   118
  by (auto simp add: dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   119
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   120
lemma divides_div_not: "(x::nat) = (q * n) + r \<Longrightarrow> 0 < r \<Longrightarrow> r < n ==> ~(n dvd x)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   121
proof(auto simp add: dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   122
  fix k assume H: "0 < r" "r < n" "q * n + r = n * k"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   123
  from H(3) have r: "r = n* (k -q)" by(simp add: diff_mult_distrib2 mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   124
  {assume "k - q = 0" with r H(1) have False by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   125
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   126
  {assume "k - q \<noteq> 0" with r have "r \<ge> n" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   127
    with H(2) have False by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   128
  ultimately show False by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   129
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   130
lemma divides_exp: "(x::nat) dvd y ==> x ^ n dvd y ^ n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   131
  by (auto simp add: power_mult_distrib dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   132
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   133
lemma divides_exp2: "n \<noteq> 0 \<Longrightarrow> (x::nat) ^ n dvd y \<Longrightarrow> x dvd y" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   134
  by (induct n ,auto simp add: dvd_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   135
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   136
fun fact :: "nat \<Rightarrow> nat" where
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   137
  "fact 0 = 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   138
| "fact (Suc n) = Suc n * fact n"	
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   139
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   140
lemma fact_lt: "0 < fact n" by(induct n, simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   141
lemma fact_le: "fact n \<ge> 1" using fact_lt[of n] by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   142
lemma fact_mono: assumes le: "m \<le> n" shows "fact m \<le> fact n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   143
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   144
  from le have "\<exists>i. n = m+i" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   145
  then obtain i where i: "n = m+i" by blast 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   146
  have "fact m \<le> fact (m + i)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   147
  proof(induct m)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   148
    case 0 thus ?case using fact_le[of i] by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   149
  next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   150
    case (Suc m)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   151
    have "fact (Suc m) = Suc m * fact m" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   152
    have th1: "Suc m \<le> Suc (m + i)" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   153
    from mult_le_mono[of "Suc m" "Suc (m+i)" "fact m" "fact (m+i)", OF th1 Suc.hyps]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   154
    show ?case by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   155
  qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   156
  thus ?thesis using i by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   157
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   158
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   159
lemma divides_fact: "1 <= p \<Longrightarrow> p <= n ==> p dvd fact n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   160
proof(induct n arbitrary: p)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   161
  case 0 thus ?case by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   162
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   163
  case (Suc n p)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   164
  from Suc.prems have "p = Suc n \<or> p \<le> n" by presburger 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   165
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   166
  {assume "p = Suc n" hence ?case  by (simp only: fact.simps dvd_triv_left)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   167
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   168
  {assume "p \<le> n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   169
    with Suc.prems(1) Suc.hyps have th: "p dvd fact n" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   170
    from dvd_mult[OF th] have ?case by (simp only: fact.simps) }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   171
  ultimately show ?case by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   172
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   173
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   174
declare dvd_triv_left[presburger]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   175
declare dvd_triv_right[presburger]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   176
lemma divides_rexp: 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   177
  "x dvd y \<Longrightarrow> (x::nat) dvd (y^(Suc n))" by (simp add: dvd_mult2[of x y])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   178
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   179
text {* Coprimality *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   180
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   181
lemma coprime: "coprime a b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   182
using gcd_unique[of 1 a b, simplified] by (auto simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   183
lemma coprime_commute: "coprime a b \<longleftrightarrow> coprime b a" by (simp add: coprime_def gcd_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   184
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   185
lemma coprime_bezout: "coprime a b \<longleftrightarrow> (\<exists>x y. a * x - b * y = 1 \<or> b * x - a * y = 1)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   186
using coprime_def gcd_bezout by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   187
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   188
lemma coprime_divprod: "d dvd a * b  \<Longrightarrow> coprime d a \<Longrightarrow> d dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   189
  using relprime_dvd_mult_iff[of d a b] by (auto simp add: coprime_def mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   190
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   191
lemma coprime_1[simp]: "coprime a 1" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   192
lemma coprime_1'[simp]: "coprime 1 a" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   193
lemma coprime_Suc0[simp]: "coprime a (Suc 0)" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   194
lemma coprime_Suc0'[simp]: "coprime (Suc 0) a" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   195
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   196
lemma gcd_coprime: 
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   197
  assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   198
  shows    "coprime a' b'"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   199
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   200
  let ?g = "gcd a b"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   201
  {assume bz: "a = 0" from b bz z a have ?thesis by (simp add: gcd_zero coprime_def)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   202
  moreover 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   203
  {assume az: "a\<noteq> 0" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   204
    from z have z': "?g > 0" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   205
    from bezout_gcd_strong[OF az, of b] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   206
    obtain x y where xy: "a*x = b*y + ?g" by blast
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 28952
diff changeset
   207
    from xy a b have "?g * a'*x = ?g * (b'*y + 1)" by (simp add: algebra_simps)
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   208
    hence "?g * (a'*x) = ?g * (b'*y + 1)" by (simp add: mult_assoc)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   209
    hence "a'*x = (b'*y + 1)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   210
      by (simp only: nat_mult_eq_cancel1[OF z']) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   211
    hence "a'*x - b'*y = 1" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   212
    with coprime_bezout[of a' b'] have ?thesis by auto}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   213
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   214
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   215
lemma coprime_0: "coprime d 0 \<longleftrightarrow> d = 1" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   216
lemma coprime_mul: assumes da: "coprime d a" and db: "coprime d b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   217
  shows "coprime d (a * b)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   218
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   219
  from da have th: "gcd a d = 1" by (simp add: coprime_def gcd_commute)
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   220
  from gcd_mult_cancel[of a d b, OF th] db[unfolded coprime_def] have "gcd d (a*b) = 1"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   221
    by (simp add: gcd_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   222
  thus ?thesis unfolding coprime_def .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   223
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   224
lemma coprime_lmul2: assumes dab: "coprime d (a * b)" shows "coprime d b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   225
using prems unfolding coprime_bezout
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   226
apply clarsimp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   227
apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   228
apply (rule_tac x="x" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   229
apply (rule_tac x="a*y" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   230
apply (simp add: mult_ac)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   231
apply (rule_tac x="a*x" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   232
apply (rule_tac x="y" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   233
apply (simp add: mult_ac)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   234
done
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   235
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   236
lemma coprime_rmul2: "coprime d (a * b) \<Longrightarrow> coprime d a"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   237
unfolding coprime_bezout
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   238
apply clarsimp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   239
apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   240
apply (rule_tac x="x" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   241
apply (rule_tac x="b*y" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   242
apply (simp add: mult_ac)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   243
apply (rule_tac x="b*x" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   244
apply (rule_tac x="y" in exI)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   245
apply (simp add: mult_ac)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   246
done
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   247
lemma coprime_mul_eq: "coprime d (a * b) \<longleftrightarrow> coprime d a \<and>  coprime d b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   248
  using coprime_rmul2[of d a b] coprime_lmul2[of d a b] coprime_mul[of d a b] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   249
  by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   250
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   251
lemma gcd_coprime_exists:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   252
  assumes nz: "gcd a b \<noteq> 0" 
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   253
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   254
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   255
  let ?g = "gcd a b"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   256
  from gcd_dvd1[of a b] gcd_dvd2[of a b] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   257
  obtain a' b' where "a = ?g*a'"  "b = ?g*b'" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   258
  hence ab': "a = a'*?g" "b = b'*?g" by algebra+
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   259
  from ab' gcd_coprime[OF nz ab'] show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   260
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   261
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   262
lemma coprime_exp: "coprime d a ==> coprime d (a^n)" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   263
  by(induct n, simp_all add: coprime_mul)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   264
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   265
lemma coprime_exp_imp: "coprime a b ==> coprime (a ^n) (b ^n)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   266
  by (induct n, simp_all add: coprime_mul_eq coprime_commute coprime_exp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   267
lemma coprime_refl[simp]: "coprime n n \<longleftrightarrow> n = 1" by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   268
lemma coprime_plus1[simp]: "coprime (n + 1) n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   269
  apply (simp add: coprime_bezout)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   270
  apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   271
  apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   272
  apply simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   273
  done
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   274
lemma coprime_minus1: "n \<noteq> 0 ==> coprime (n - 1) n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   275
  using coprime_plus1[of "n - 1"] coprime_commute[of "n - 1" n] by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   276
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   277
lemma bezout_gcd_pow: "\<exists>x y. a ^n * x - b ^ n * y = gcd a b ^ n \<or> b ^ n * x - a ^ n * y = gcd a b ^ n"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   278
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   279
  let ?g = "gcd a b"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   280
  {assume z: "?g = 0" hence ?thesis 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   281
      apply (cases n, simp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   282
      apply arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   283
      apply (simp only: z power_0_Suc)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   284
      apply (rule exI[where x=0])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   285
      apply (rule exI[where x=0])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   286
      by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   287
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   288
  {assume z: "?g \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   289
    from gcd_dvd1[of a b] gcd_dvd2[of a b] obtain a' b' where
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   290
      ab': "a = a'*?g" "b = b'*?g" unfolding dvd_def by (auto simp add: mult_ac)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   291
    hence ab'': "?g*a' = a" "?g * b' = b" by algebra+
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   292
    from coprime_exp_imp[OF gcd_coprime[OF z ab'], unfolded coprime_bezout, of n]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   293
    obtain x y where "a'^n * x - b'^n * y = 1 \<or> b'^n * x - a'^n * y = 1"  by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   294
    hence "?g^n * (a'^n * x - b'^n * y) = ?g^n \<or> ?g^n*(b'^n * x - a'^n * y) = ?g^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   295
      using z by auto 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   296
    then have "a^n * x - b^n * y = ?g^n \<or> b^n * x - a^n * y = ?g^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   297
      using z ab'' by (simp only: power_mult_distrib[symmetric] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   298
	diff_mult_distrib2 mult_assoc[symmetric])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   299
    hence  ?thesis by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   300
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   301
qed
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   302
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   303
lemma gcd_exp: "gcd (a^n) (b^n) = gcd a b^n"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   304
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   305
  let ?g = "gcd (a^n) (b^n)"
27567
e3fe9a327c63 Fixed proofs.
chaieb
parents: 27556
diff changeset
   306
  let ?gn = "gcd a b^n"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   307
  {fix e assume H: "e dvd a^n" "e dvd b^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   308
    from bezout_gcd_pow[of a n b] obtain x y 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   309
      where xy: "a ^ n * x - b ^ n * y = ?gn \<or> b ^ n * x - a ^ n * y = ?gn" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   310
    from dvd_diff [OF dvd_mult2[OF H(1), of x] dvd_mult2[OF H(2), of y]]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   311
      dvd_diff [OF dvd_mult2[OF H(2), of x] dvd_mult2[OF H(1), of y]] xy
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   312
    have "e dvd ?gn" by (cases "a ^ n * x - b ^ n * y = gcd a b ^ n", simp_all)}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   313
  hence th:  "\<forall>e. e dvd a^n \<and> e dvd b^n \<longrightarrow> e dvd ?gn" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   314
  from divides_exp[OF gcd_dvd1[of a b], of n] divides_exp[OF gcd_dvd2[of a b], of n] th
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   315
    gcd_unique have "?gn = ?g" by blast thus ?thesis by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   316
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   317
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   318
lemma coprime_exp2:  "coprime (a ^ Suc n) (b^ Suc n) \<longleftrightarrow> coprime a b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   319
by (simp only: coprime_def gcd_exp exp_eq_1) simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   320
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   321
lemma division_decomp: assumes dc: "(a::nat) dvd b * c"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   322
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   323
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   324
  let ?g = "gcd a b"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   325
  {assume "?g = 0" with dc have ?thesis apply (simp add: gcd_zero)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   326
      apply (rule exI[where x="0"])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   327
      by (rule exI[where x="c"], simp)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   328
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   329
  {assume z: "?g \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   330
    from gcd_coprime_exists[OF z]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   331
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   332
    from gcd_dvd2[of a b] have thb: "?g dvd b" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   333
    from ab'(1) have "a' dvd a"  unfolding dvd_def by blast  
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   334
    with dc have th0: "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   335
    from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   336
    hence "?g*a' dvd ?g * (b' * c)" by (simp add: mult_assoc)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   337
    with z have th_1: "a' dvd b'*c" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   338
    from coprime_divprod[OF th_1 ab'(3)] have thc: "a' dvd c" . 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   339
    from ab' have "a = ?g*a'" by algebra
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   340
    with thb thc have ?thesis by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   341
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   342
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   343
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   344
lemma nat_power_eq_0_iff: "(m::nat) ^ n = 0 \<longleftrightarrow> n \<noteq> 0 \<and> m = 0" by (induct n, auto)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   345
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   346
lemma divides_rev: assumes ab: "(a::nat) ^ n dvd b ^n" and n:"n \<noteq> 0" shows "a dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   347
proof-
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27487
diff changeset
   348
  let ?g = "gcd a b"
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   349
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   350
  {assume "?g = 0" with ab n have ?thesis by (simp add: gcd_zero)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   351
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   352
  {assume z: "?g \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   353
    hence zn: "?g ^ n \<noteq> 0" using n by (simp add: neq0_conv)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   354
    from gcd_coprime_exists[OF z] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   355
    obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   356
    from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n" by (simp add: ab'(1,2)[symmetric])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   357
    hence "?g^n*a'^n dvd ?g^n *b'^n" by (simp only: power_mult_distrib mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   358
    with zn z n have th0:"a'^n dvd b'^n" by (auto simp add: nat_power_eq_0_iff)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   359
    have "a' dvd a'^n" by (simp add: m)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   360
    with th0 have "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   361
    hence th1: "a' dvd b'^m * b'" by (simp add: m mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   362
    from coprime_divprod[OF th1 coprime_exp[OF ab'(3), of m]]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   363
    have "a' dvd b'" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   364
    hence "a'*?g dvd b'*?g" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   365
    with ab'(1,2)  have ?thesis by simp }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   366
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   367
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   368
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   369
lemma divides_mul: assumes mr: "m dvd r" and nr: "n dvd r" and mn:"coprime m n" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   370
  shows "m * n dvd r"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   371
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   372
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   373
    unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   374
  from mr n' have "m dvd n'*n" by (simp add: mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   375
  hence "m dvd n'" using relprime_dvd_mult_iff[OF mn[unfolded coprime_def]] by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   376
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   377
  from n' k show ?thesis unfolding dvd_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   378
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   379
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   380
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   381
text {* A binary form of the Chinese Remainder Theorem. *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   382
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   383
lemma chinese_remainder: assumes ab: "coprime a b" and a:"a \<noteq> 0" and b:"b \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   384
  shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   385
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   386
  from bezout_add_strong[OF a, of b] bezout_add_strong[OF b, of a]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   387
  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   388
    and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   389
  from gcd_unique[of 1 a b, simplified ab[unfolded coprime_def], simplified] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   390
    dxy1(1,2) dxy2(1,2) have d12: "d1 = 1" "d2 =1" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   391
  let ?x = "v * a * x1 + u * b * x2"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   392
  let ?q1 = "v * x1 + u * y2"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   393
  let ?q2 = "v * y1 + u * x2"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   394
  from dxy2(3)[simplified d12] dxy1(3)[simplified d12] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   395
  have "?x = u + ?q1 * a" "?x = v + ?q2 * b" by algebra+ 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   396
  thus ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   397
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   398
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   399
text {* Primality *}
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   400
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   401
text {* A few useful theorems about primes *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   402
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   403
lemma prime_0[simp]: "~prime 0" by (simp add: prime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   404
lemma prime_1[simp]: "~ prime 1"  by (simp add: prime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   405
lemma prime_Suc0[simp]: "~ prime (Suc 0)"  by (simp add: prime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   406
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   407
lemma prime_ge_2: "prime p ==> p \<ge> 2" by (simp add: prime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   408
lemma prime_factor: assumes n: "n \<noteq> 1" shows "\<exists> p. prime p \<and> p dvd n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   409
using n
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   410
proof(induct n rule: nat_less_induct)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   411
  fix n
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   412
  assume H: "\<forall>m<n. m \<noteq> 1 \<longrightarrow> (\<exists>p. prime p \<and> p dvd m)" "n \<noteq> 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   413
  let ?ths = "\<exists>p. prime p \<and> p dvd n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   414
  {assume "n=0" hence ?ths using two_is_prime by auto}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   415
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   416
  {assume nz: "n\<noteq>0" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   417
    {assume "prime n" hence ?ths by - (rule exI[where x="n"], simp)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   418
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   419
    {assume n: "\<not> prime n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   420
      with nz H(2) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   421
      obtain k where k:"k dvd n" "k \<noteq> 1" "k \<noteq> n" by (auto simp add: prime_def) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   422
      from dvd_imp_le[OF k(1)] nz k(3) have kn: "k < n" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   423
      from H(1)[rule_format, OF kn k(2)] obtain p where p: "prime p" "p dvd k" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   424
      from dvd_trans[OF p(2) k(1)] p(1) have ?ths by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   425
    ultimately have ?ths by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   426
  ultimately show ?ths by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   427
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   428
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   429
lemma prime_factor_lt: assumes p: "prime p" and n: "n \<noteq> 0" and npm:"n = p * m"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   430
  shows "m < n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   431
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   432
  {assume "m=0" with n have ?thesis by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   433
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   434
  {assume m: "m \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   435
    from npm have mn: "m dvd n" unfolding dvd_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   436
    from npm m have "n \<noteq> m" using p by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   437
    with dvd_imp_le[OF mn] n have ?thesis by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   438
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   439
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   440
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   441
lemma euclid_bound: "\<exists>p. prime p \<and> n < p \<and>  p <= Suc (fact n)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   442
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   443
  have f1: "fact n + 1 \<noteq> 1" using fact_le[of n] by arith 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   444
  from prime_factor[OF f1] obtain p where p: "prime p" "p dvd fact n + 1" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   445
  from dvd_imp_le[OF p(2)] have pfn: "p \<le> fact n + 1" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   446
  {assume np: "p \<le> n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   447
    from p(1) have p1: "p \<ge> 1" by (cases p, simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   448
    from divides_fact[OF p1 np] have pfn': "p dvd fact n" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   449
    from divides_add_revr[OF pfn' p(2)] p(1) have False by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   450
  hence "n < p" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   451
  with p(1) pfn show ?thesis by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   452
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   453
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   454
lemma euclid: "\<exists>p. prime p \<and> p > n" using euclid_bound by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   455
lemma primes_infinite: "\<not> (finite {p. prime p})"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   456
proof (auto simp add: finite_conv_nat_seg_image)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   457
  fix n f 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   458
  assume H: "Collect prime = f ` {i. i < (n::nat)}"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   459
  let ?P = "Collect prime"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   460
  let ?m = "Max ?P"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   461
  have P0: "?P \<noteq> {}" using two_is_prime by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   462
  from H have fP: "finite ?P" using finite_conv_nat_seg_image by blast 
26757
e775accff967 thms Max_ge, Min_le: dropped superfluous premise
haftmann
parents: 26159
diff changeset
   463
  from Max_in [OF fP P0] have "?m \<in> ?P" . 
e775accff967 thms Max_ge, Min_le: dropped superfluous premise
haftmann
parents: 26159
diff changeset
   464
  from Max_ge [OF fP] have contr: "\<forall> p. prime p \<longrightarrow> p \<le> ?m" by blast
e775accff967 thms Max_ge, Min_le: dropped superfluous premise
haftmann
parents: 26159
diff changeset
   465
  from euclid [of ?m] obtain q where q: "prime q" "q > ?m" by blast
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   466
  with contr show False by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   467
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   468
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   469
lemma coprime_prime: assumes ab: "coprime a b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   470
  shows "~(prime p \<and> p dvd a \<and> p dvd b)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   471
proof
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   472
  assume "prime p \<and> p dvd a \<and> p dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   473
  thus False using ab gcd_greatest[of p a b] by (simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   474
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   475
lemma coprime_prime_eq: "coprime a b \<longleftrightarrow> (\<forall>p. ~(prime p \<and> p dvd a \<and> p dvd b))" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   476
  (is "?lhs = ?rhs")
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   477
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   478
  {assume "?lhs" with coprime_prime  have ?rhs by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   479
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   480
  {assume r: "?rhs" and c: "\<not> ?lhs"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   481
    then obtain g where g: "g\<noteq>1" "g dvd a" "g dvd b" unfolding coprime_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   482
    from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   483
    from dvd_trans [OF p(2) g(2)] dvd_trans [OF p(2) g(3)] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   484
    have "p dvd a" "p dvd b" . with p(1) r have False by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   485
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   486
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   487
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   488
lemma prime_coprime: assumes p: "prime p" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   489
  shows "n = 1 \<or> p dvd n \<or> coprime p n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   490
using p prime_imp_relprime[of p n] by (auto simp add: coprime_def)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   491
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   492
lemma prime_coprime_strong: "prime p \<Longrightarrow> p dvd n \<or> coprime p n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   493
  using prime_coprime[of p n] by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   494
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   495
declare  coprime_0[simp]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   496
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   497
lemma coprime_0'[simp]: "coprime 0 d \<longleftrightarrow> d = 1" by (simp add: coprime_commute[of 0 d])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   498
lemma coprime_bezout_strong: assumes ab: "coprime a b" and b: "b \<noteq> 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   499
  shows "\<exists>x y. a * x = b * y + 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   500
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   501
  from ab b have az: "a \<noteq> 0" by - (rule ccontr, auto)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   502
  from bezout_gcd_strong[OF az, of b] ab[unfolded coprime_def]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   503
  show ?thesis by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   504
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   505
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   506
lemma bezout_prime: assumes p: "prime p"  and pa: "\<not> p dvd a"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   507
  shows "\<exists>x y. a*x = p*y + 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   508
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   509
  from p have p1: "p \<noteq> 1" using prime_1 by blast 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   510
  from prime_coprime[OF p, of a] p1 pa have ap: "coprime a p" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   511
    by (auto simp add: coprime_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   512
  from coprime_bezout_strong[OF ap p1] show ?thesis . 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   513
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   514
lemma prime_divprod: assumes p: "prime p" and pab: "p dvd a*b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   515
  shows "p dvd a \<or> p dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   516
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   517
  {assume "a=1" hence ?thesis using pab by simp }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   518
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   519
  {assume "p dvd a" hence ?thesis by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   520
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   521
  {assume pa: "coprime p a" from coprime_divprod[OF pab pa]  have ?thesis .. }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   522
  ultimately show ?thesis using prime_coprime[OF p, of a] by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   523
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   524
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   525
lemma prime_divprod_eq: assumes p: "prime p"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   526
  shows "p dvd a*b \<longleftrightarrow> p dvd a \<or> p dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   527
using p prime_divprod dvd_mult dvd_mult2 by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   528
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   529
lemma prime_divexp: assumes p:"prime p" and px: "p dvd x^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   530
  shows "p dvd x"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   531
using px
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   532
proof(induct n)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   533
  case 0 thus ?case by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   534
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   535
  case (Suc n) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   536
  hence th: "p dvd x*x^n" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   537
  {assume H: "p dvd x^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   538
    from Suc.hyps[OF H] have ?case .}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   539
  with prime_divprod[OF p th] show ?case by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   540
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   541
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   542
lemma prime_divexp_n: "prime p \<Longrightarrow> p dvd x^n \<Longrightarrow> p^n dvd x^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   543
  using prime_divexp[of p x n] divides_exp[of p x n] by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   544
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   545
lemma coprime_prime_dvd_ex: assumes xy: "\<not>coprime x y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   546
  shows "\<exists>p. prime p \<and> p dvd x \<and> p dvd y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   547
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   548
  from xy[unfolded coprime_def] obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd y" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   549
    by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   550
  from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   551
  from g(2,3) dvd_trans[OF p(2)] p(1) show ?thesis by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   552
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   553
lemma coprime_sos: assumes xy: "coprime x y" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   554
  shows "coprime (x * y) (x^2 + y^2)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   555
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   556
  {assume c: "\<not> coprime (x * y) (x^2 + y^2)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   557
    from coprime_prime_dvd_ex[OF c] obtain p 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   558
      where p: "prime p" "p dvd x*y" "p dvd x^2 + y^2" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   559
    {assume px: "p dvd x"
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   560
      from dvd_mult[OF px, of x] p(3) 
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   561
        obtain r s where "x * x = p * r" and "x^2 + y^2 = p * s"
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   562
          by (auto elim!: dvdE)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   563
        then have "y^2 = p * (s - r)" 
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   564
          by (auto simp add: power2_eq_square diff_mult_distrib2)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   565
        then have "p dvd y^2" ..
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   566
      with prime_divexp[OF p(1), of y 2] have py: "p dvd y" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   567
      from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   568
      have False by simp }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   569
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   570
    {assume py: "p dvd y"
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   571
      from dvd_mult[OF py, of y] p(3)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   572
        obtain r s where "y * y = p * r" and "x^2 + y^2 = p * s"
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   573
          by (auto elim!: dvdE)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   574
        then have "x^2 = p * (s - r)" 
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   575
          by (auto simp add: power2_eq_square diff_mult_distrib2)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   576
        then have "p dvd x^2" ..
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   577
      with prime_divexp[OF p(1), of x 2] have px: "p dvd x" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   578
      from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   579
      have False by simp }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   580
    ultimately have False using prime_divprod[OF p(1,2)] by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   581
  thus ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   582
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   583
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   584
lemma distinct_prime_coprime: "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   585
  unfolding prime_def coprime_prime_eq by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   586
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   587
lemma prime_coprime_lt: assumes p: "prime p" and x: "0 < x" and xp: "x < p"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   588
  shows "coprime x p"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   589
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   590
  {assume c: "\<not> coprime x p"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   591
    then obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd p" unfolding coprime_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   592
  from dvd_imp_le[OF g(2)] x xp have gp: "g < p" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   593
  from g(2) x have "g \<noteq> 0" by - (rule ccontr, simp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   594
  with g gp p[unfolded prime_def] have False by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   595
thus ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   596
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   597
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   598
lemma even_dvd[simp]: "even (n::nat) \<longleftrightarrow> 2 dvd n" by presburger
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   599
lemma prime_odd: "prime p \<Longrightarrow> p = 2 \<or> odd p" unfolding prime_def by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   600
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   601
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   602
text {* One property of coprimality is easier to prove via prime factors. *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   603
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   604
lemma prime_divprod_pow: 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   605
  assumes p: "prime p" and ab: "coprime a b" and pab: "p^n dvd a * b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   606
  shows "p^n dvd a \<or> p^n dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   607
proof-
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   608
  {assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   609
      apply (cases "n=0", simp_all)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   610
      apply (cases "a=1", simp_all) done}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   611
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   612
  {assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   613
    then obtain m where m: "n = Suc m" by (cases n, auto)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   614
    from divides_exp2[OF n pab] have pab': "p dvd a*b" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   615
    from prime_divprod[OF p pab'] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   616
    have "p dvd a \<or> p dvd b" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   617
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   618
    {assume pa: "p dvd a"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   619
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   620
      from coprime_prime[OF ab, of p] p pa have "\<not> p dvd b" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   621
      with prime_coprime[OF p, of b] b 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   622
      have cpb: "coprime b p" using coprime_commute by blast 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   623
      from coprime_exp[OF cpb] have pnb: "coprime (p^n) b" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   624
	by (simp add: coprime_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   625
      from coprime_divprod[OF pnba pnb] have ?thesis by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   626
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   627
    {assume pb: "p dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   628
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   629
      from coprime_prime[OF ab, of p] p pb have "\<not> p dvd a" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   630
      with prime_coprime[OF p, of a] a
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   631
      have cpb: "coprime a p" using coprime_commute by blast 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   632
      from coprime_exp[OF cpb] have pnb: "coprime (p^n) a" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   633
	by (simp add: coprime_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   634
      from coprime_divprod[OF pab pnb] have ?thesis by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   635
    ultimately have ?thesis by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   636
  ultimately show ?thesis by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   637
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   638
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   639
lemma nat_mult_eq_one: "(n::nat) * m = 1 \<longleftrightarrow> n = 1 \<and> m = 1" (is "?lhs \<longleftrightarrow> ?rhs")
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   640
proof
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   641
  assume H: "?lhs"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   642
  hence "n dvd 1" "m dvd 1" unfolding dvd_def by (auto simp add: mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   643
  thus ?rhs by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   644
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   645
  assume ?rhs then show ?lhs by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   646
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   647
  
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   648
lemma power_Suc0[simp]: "Suc 0 ^ n = Suc 0" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   649
  unfolding One_nat_def[symmetric] power_one ..
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   650
lemma coprime_pow: assumes ab: "coprime a b" and abcn: "a * b = c ^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   651
  shows "\<exists>r s. a = r^n  \<and> b = s ^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   652
  using ab abcn
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   653
proof(induct c arbitrary: a b rule: nat_less_induct)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   654
  fix c a b
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   655
  assume H: "\<forall>m<c. \<forall>a b. coprime a b \<longrightarrow> a * b = m ^ n \<longrightarrow> (\<exists>r s. a = r ^ n \<and> b = s ^ n)" "coprime a b" "a * b = c ^ n" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   656
  let ?ths = "\<exists>r s. a = r^n  \<and> b = s ^n"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   657
  {assume n: "n = 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   658
    with H(3) power_one have "a*b = 1" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   659
    hence "a = 1 \<and> b = 1" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   660
    hence ?ths 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   661
      apply -
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   662
      apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   663
      apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   664
      using power_one[of  n]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   665
      by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   666
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   667
  {assume n: "n \<noteq> 0" then obtain m where m: "n = Suc m" by (cases n, auto)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   668
    {assume c: "c = 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   669
      with H(3) m H(2) have ?ths apply simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   670
	apply (cases "a=0", simp_all) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   671
	apply (rule exI[where x="0"], simp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   672
	apply (rule exI[where x="0"], simp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   673
	done}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   674
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   675
    {assume "c=1" with H(3) power_one have "a*b = 1" by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   676
	hence "a = 1 \<and> b = 1" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   677
	hence ?ths 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   678
      apply -
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   679
      apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   680
      apply (rule exI[where x=1])
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   681
      using power_one[of  n]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   682
      by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   683
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   684
  {assume c: "c\<noteq>1" "c \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   685
    from prime_factor[OF c(1)] obtain p where p: "prime p" "p dvd c" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   686
    from prime_divprod_pow[OF p(1) H(2), unfolded H(3), OF divides_exp[OF p(2), of n]] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   687
    have pnab: "p ^ n dvd a \<or> p^n dvd b" . 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   688
    from p(2) obtain l where l: "c = p*l" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   689
    have pn0: "p^n \<noteq> 0" using n prime_ge_2 [OF p(1)] by (simp add: neq0_conv)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   690
    {assume pa: "p^n dvd a"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   691
      then obtain k where k: "a = p^n * k" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   692
      from l have "l dvd c" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   693
      with dvd_imp_le[of l c] c have "l \<le> c" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   694
      moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   695
      ultimately have lc: "l < c" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   696
      from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" b]]]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   697
      have kb: "coprime k b" by (simp add: coprime_commute) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   698
      from H(3) l k pn0 have kbln: "k * b = l ^ n" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   699
	by (auto simp add: power_mult_distrib)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   700
      from H(1)[rule_format, OF lc kb kbln]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   701
      obtain r s where rs: "k = r ^n" "b = s^n" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   702
      from k rs(1) have "a = (p*r)^n" by (simp add: power_mult_distrib)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   703
      with rs(2) have ?ths by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   704
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   705
    {assume pb: "p^n dvd b"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   706
      then obtain k where k: "b = p^n * k" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   707
      from l have "l dvd c" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   708
      with dvd_imp_le[of l c] c have "l \<le> c" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   709
      moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   710
      ultimately have lc: "l < c" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   711
      from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" a]]]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   712
      have kb: "coprime k a" by (simp add: coprime_commute) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   713
      from H(3) l k pn0 n have kbln: "k * a = l ^ n" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   714
	by (simp add: power_mult_distrib mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   715
      from H(1)[rule_format, OF lc kb kbln]
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   716
      obtain r s where rs: "k = r ^n" "a = s^n" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   717
      from k rs(1) have "b = (p*r)^n" by (simp add: power_mult_distrib)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   718
      with rs(2) have ?ths by blast }
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   719
    ultimately have ?ths using pnab by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   720
  ultimately have ?ths by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   721
ultimately show ?ths by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   722
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   723
26144
98d23fc02585 tuned document;
wenzelm
parents: 26125
diff changeset
   724
text {* More useful lemmas. *}
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   725
lemma prime_product: 
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   726
  assumes "prime (p * q)"
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   727
  shows "p = 1 \<or> q = 1"
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   728
proof -
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   729
  from assms have 
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   730
    "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   731
    unfolding prime_def by auto
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   732
  from `1 < p * q` have "p \<noteq> 0" by (cases p) auto
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   733
  then have Q: "p = p * q \<longleftrightarrow> q = 1" by auto
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   734
  have "p dvd p * q" by simp
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   735
  then have "p = 1 \<or> p = p * q" by (rule P)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   736
  then show ?thesis by (simp add: Q)
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27567
diff changeset
   737
qed
26125
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   738
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   739
lemma prime_exp: "prime (p^n) \<longleftrightarrow> prime p \<and> n = 1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   740
proof(induct n)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   741
  case 0 thus ?case by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   742
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   743
  case (Suc n)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   744
  {assume "p = 0" hence ?case by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   745
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   746
  {assume "p=1" hence ?case by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   747
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   748
  {assume p: "p \<noteq> 0" "p\<noteq>1"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   749
    {assume pp: "prime (p^Suc n)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   750
      hence "p = 1 \<or> p^n = 1" using prime_product[of p "p^n"] by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   751
      with p have n: "n = 0" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   752
	by (simp only: exp_eq_1 ) simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   753
      with pp have "prime p \<and> Suc n = 1" by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   754
    moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   755
    {assume n: "prime p \<and> Suc n = 1" hence "prime (p^Suc n)" by simp}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   756
    ultimately have ?case by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   757
  ultimately show ?case by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   758
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   759
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   760
lemma prime_power_mult: 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   761
  assumes p: "prime p" and xy: "x * y = p ^ k"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   762
  shows "\<exists>i j. x = p ^i \<and> y = p^ j"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   763
  using xy
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   764
proof(induct k arbitrary: x y)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   765
  case 0 thus ?case apply simp by (rule exI[where x="0"], simp)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   766
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   767
  case (Suc k x y)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   768
  from Suc.prems have pxy: "p dvd x*y" by auto
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   769
  from prime_divprod[OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   770
  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp) 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   771
  {assume px: "p dvd x"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   772
    then obtain d where d: "x = p*d" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   773
    from Suc.prems d  have "p*d*y = p^Suc k" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   774
    hence th: "d*y = p^k" using p0 by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   775
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   776
    with d have "x = p^Suc i" by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   777
    with ij(2) have ?case by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   778
  moreover 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   779
  {assume px: "p dvd y"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   780
    then obtain d where d: "y = p*d" unfolding dvd_def by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   781
    from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult_commute)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   782
    hence th: "d*x = p^k" using p0 by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   783
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   784
    with d have "y = p^Suc i" by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   785
    with ij(2) have ?case by blast}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   786
  ultimately show ?case  using pxyc by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   787
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   788
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   789
lemma prime_power_exp: assumes p: "prime p" and n:"n \<noteq> 0" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   790
  and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   791
  using n xn
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   792
proof(induct n arbitrary: k)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   793
  case 0 thus ?case by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   794
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   795
  case (Suc n k) hence th: "x*x^n = p^k" by simp
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   796
  {assume "n = 0" with prems have ?case apply simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   797
      by (rule exI[where x="k"],simp)}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   798
  moreover
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   799
  {assume n: "n \<noteq> 0"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   800
    from prime_power_mult[OF p th] 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   801
    obtain i j where ij: "x = p^i" "x^n = p^j"by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   802
    from Suc.hyps[OF n ij(2)] have ?case .}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   803
  ultimately show ?case by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   804
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   805
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   806
lemma divides_primepow: assumes p: "prime p" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   807
  shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   808
proof
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   809
  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k" 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   810
    unfolding dvd_def  apply (auto simp add: mult_commute) by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   811
  from prime_power_mult[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   812
  from prime_ge_2[OF p] have p1: "p > 1" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   813
  from e ij have "p^(i + j) = p^k" by (simp add: power_add)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   814
  hence "i + j = k" using power_inject_exp[of p "i+j" k, OF p1] by simp 
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   815
  hence "i \<le> k" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   816
  with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   817
next
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   818
  {fix i assume H: "i \<le> k" "d = p^i"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   819
    hence "\<exists>j. k = i + j" by arith
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   820
    then obtain j where j: "k = i + j" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   821
    hence "p^k = p^j*d" using H(2) by (simp add: power_add)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   822
    hence "d dvd p^k" unfolding dvd_def by auto}
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   823
  thus "\<exists>i\<le>k. d = p ^ i \<Longrightarrow> d dvd p ^ k" by blast
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   824
qed
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   825
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   826
lemma coprime_divisors: "d dvd a \<Longrightarrow> e dvd b \<Longrightarrow> coprime a b \<Longrightarrow> coprime d e"
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   827
  by (auto simp add: dvd_def coprime)
345465cc9e79 More primality theorems
chaieb
parents: 25593
diff changeset
   828
26159
ff372ff5cc34 Removed theorems from default simpset
chaieb
parents: 26144
diff changeset
   829
declare power_Suc0[simp del]
ff372ff5cc34 Removed theorems from default simpset
chaieb
parents: 26144
diff changeset
   830
declare even_dvd[simp del]
26757
e775accff967 thms Max_ge, Min_le: dropped superfluous premise
haftmann
parents: 26159
diff changeset
   831
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   832
end