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