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