
wenzelm@38159: (*  Title:      HOL/Old_Number_Theory/Primes.thy
haftmann@27106:     Author:     Amine Chaieb, Christophe Tabacznyj and Lawrence C Paulson
paulson@11363:     Copyright   1996  University of Cambridge
paulson@11363: *)
paulson@11363: 
nipkow@16762: header {* Primality on nat *}
paulson@11363: 
nipkow@15131: theory Primes
huffman@31706: imports Complex_Main Legacy_GCD
nipkow@15131: begin
paulson@11363: 
wenzelm@38159: definition coprime :: "nat => nat => bool"
wenzelm@38159:   where "coprime m n \<longleftrightarrow> gcd m n = 1"
paulson@11363: 
wenzelm@38159: definition prime :: "nat \<Rightarrow> bool"
wenzelm@38159:   where "prime p \<longleftrightarrow> (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
paulson@11363: 
paulson@11363: 
nipkow@16762: lemma two_is_prime: "prime 2"
nipkow@16762:   apply (auto simp add: prime_def)
nipkow@16762:   apply (case_tac m)
nipkow@16762:    apply (auto dest!: dvd_imp_le)
paulson@11363:   done
paulson@11363: 
haftmann@27556: lemma prime_imp_relprime: "prime p ==> \<not> p dvd n ==> gcd p n = 1"
paulson@11363:   apply (auto simp add: prime_def)
paulson@23839:   apply (metis One_nat_def gcd_dvd1 gcd_dvd2)
paulson@11363:   done
paulson@11363: 
paulson@11363: text {*
paulson@11363:   This theorem leads immediately to a proof of the uniqueness of
paulson@11363:   factorization.  If @{term p} divides a product of primes then it is
paulson@11363:   one of those primes.
paulson@11363: *}
paulson@11363: 
nipkow@16663: lemma prime_dvd_mult: "prime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
wenzelm@12739:   by (blast intro: relprime_dvd_mult prime_imp_relprime)
paulson@11363: 
nipkow@16663: lemma prime_dvd_square: "prime p ==> p dvd m^Suc (Suc 0) ==> p dvd m"
wenzelm@12739:   by (auto dest: prime_dvd_mult)
wenzelm@12739: 
nipkow@16663: lemma prime_dvd_power_two: "prime p ==> p dvd m\<twosuperior> ==> p dvd m"
paulson@14353:   by (rule prime_dvd_square) (simp_all add: power2_eq_square)
wenzelm@11368: 
chaieb@26125: 
nipkow@30056: lemma exp_eq_1:"(x::nat)^n = 1 \<longleftrightarrow> x = 1 \<or> n = 0"
nipkow@30056: by (induct n, auto)
nipkow@30056: 
chaieb@26125: lemma exp_mono_lt: "(x::nat) ^ (Suc n) < y ^ (Suc n) \<longleftrightarrow> x < y"
nipkow@30056: by(metis linorder_not_less not_less0 power_le_imp_le_base power_less_imp_less_base)
nipkow@30056: 
chaieb@26125: lemma exp_mono_le: "(x::nat) ^ (Suc n) \<le> y ^ (Suc n) \<longleftrightarrow> x \<le> y"
nipkow@30056: by (simp only: linorder_not_less[symmetric] exp_mono_lt)
chaieb@26125: 
chaieb@26125: lemma exp_mono_eq: "(x::nat) ^ Suc n = y ^ Suc n \<longleftrightarrow> x = y"
chaieb@26125: using power_inject_base[of x n y] by auto
chaieb@26125: 
chaieb@26125: 
chaieb@26125: lemma even_square: assumes e: "even (n::nat)" shows "\<exists>x. n ^ 2 = 4*x"
chaieb@26125: proof-
chaieb@26125:   from e have "2 dvd n" by presburger
chaieb@26125:   then obtain k where k: "n = 2*k" using dvd_def by auto
chaieb@26125:   hence "n^2 = 4* (k^2)" by (simp add: power2_eq_square)
chaieb@26125:   thus ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma odd_square: assumes e: "odd (n::nat)" shows "\<exists>x. n ^ 2 = 4*x + 1"
chaieb@26125: proof-
chaieb@26125:   from e have np: "n > 0" by presburger
chaieb@26125:   from e have "2 dvd (n - 1)" by presburger
chaieb@26125:   then obtain k where "n - 1 = 2*k" using dvd_def by auto
chaieb@26125:   hence k: "n = 2*k + 1"  using e by presburger 
chaieb@26125:   hence "n^2 = 4* (k^2 + k) + 1" by algebra   
chaieb@26125:   thus ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma diff_square: "(x::nat)^2 - y^2 = (x+y)*(x - y)" 
chaieb@26125: proof-
chaieb@26125:   have "x \<le> y \<or> y \<le> x" by (rule nat_le_linear)
chaieb@26125:   moreover
chaieb@26125:   {assume le: "x \<le> y"
chaieb@26125:     hence "x ^2 \<le> y^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
chaieb@26125:     with le have ?thesis by simp }
chaieb@26125:   moreover
chaieb@26125:   {assume le: "y \<le> x"
chaieb@26125:     hence le2: "y ^2 \<le> x^2" by (simp only: numeral_2_eq_2 exp_mono_le Let_def)
chaieb@26125:     from le have "\<exists>z. y + z = x" by presburger
chaieb@26125:     then obtain z where z: "x = y + z" by blast 
chaieb@26125:     from le2 have "\<exists>z. x^2 = y^2 + z" by presburger
chaieb@26125:     then obtain z2 where z2: "x^2 = y^2 + z2"  by blast
chaieb@26125:     from z z2 have ?thesis apply simp by algebra }
chaieb@26125:   ultimately show ?thesis by blast  
chaieb@26125: qed
chaieb@26125: 
wenzelm@26144: text {* Elementary theory of divisibility *}
chaieb@26125: lemma divides_ge: "(a::nat) dvd b \<Longrightarrow> b = 0 \<or> a \<le> b" unfolding dvd_def by auto
chaieb@26125: lemma divides_antisym: "(x::nat) dvd y \<and> y dvd x \<longleftrightarrow> x = y"
nipkow@33657:   using dvd_antisym[of x y] by auto
chaieb@26125: 
chaieb@26125: lemma divides_add_revr: assumes da: "(d::nat) dvd a" and dab:"d dvd (a + b)"
chaieb@26125:   shows "d dvd b"
chaieb@26125: proof-
chaieb@26125:   from da obtain k where k:"a = d*k" by (auto simp add: dvd_def)
chaieb@26125:   from dab obtain k' where k': "a + b = d*k'" by (auto simp add: dvd_def)
chaieb@26125:   from k k' have "b = d *(k' - k)" by (simp add : diff_mult_distrib2)
chaieb@26125:   thus ?thesis unfolding dvd_def by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: declare nat_mult_dvd_cancel_disj[presburger]
chaieb@26125: lemma nat_mult_dvd_cancel_disj'[presburger]: 
chaieb@26125:   "(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 
chaieb@26125: 
chaieb@26125: lemma divides_mul_l: "(a::nat) dvd b ==> (c * a) dvd (c * b)"
chaieb@26125:   by presburger
chaieb@26125: 
chaieb@26125: lemma divides_mul_r: "(a::nat) dvd b ==> (a * c) dvd (b * c)" by presburger
chaieb@26125: lemma divides_cases: "(n::nat) dvd m ==> m = 0 \<or> m = n \<or> 2 * n <= m" 
chaieb@26125:   by (auto simp add: dvd_def)
chaieb@26125: 
chaieb@26125: lemma divides_div_not: "(x::nat) = (q * n) + r \<Longrightarrow> 0 < r \<Longrightarrow> r < n ==> ~(n dvd x)"
chaieb@26125: proof(auto simp add: dvd_def)
chaieb@26125:   fix k assume H: "0 < r" "r < n" "q * n + r = n * k"
chaieb@26125:   from H(3) have r: "r = n* (k -q)" by(simp add: diff_mult_distrib2 mult_commute)
chaieb@26125:   {assume "k - q = 0" with r H(1) have False by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume "k - q \<noteq> 0" with r have "r \<ge> n" by auto
chaieb@26125:     with H(2) have False by simp}
chaieb@26125:   ultimately show False by blast
chaieb@26125: qed
chaieb@26125: lemma divides_exp: "(x::nat) dvd y ==> x ^ n dvd y ^ n"
chaieb@26125:   by (auto simp add: power_mult_distrib dvd_def)
chaieb@26125: 
chaieb@26125: lemma divides_exp2: "n \<noteq> 0 \<Longrightarrow> (x::nat) ^ n dvd y \<Longrightarrow> x dvd y" 
chaieb@26125:   by (induct n ,auto simp add: dvd_def)
chaieb@26125: 
chaieb@26125: fun fact :: "nat \<Rightarrow> nat" where
chaieb@26125:   "fact 0 = 1"
wenzelm@32960: | "fact (Suc n) = Suc n * fact n"
chaieb@26125: 
chaieb@26125: lemma fact_lt: "0 < fact n" by(induct n, simp_all)
chaieb@26125: lemma fact_le: "fact n \<ge> 1" using fact_lt[of n] by simp 
chaieb@26125: lemma fact_mono: assumes le: "m \<le> n" shows "fact m \<le> fact n"
chaieb@26125: proof-
chaieb@26125:   from le have "\<exists>i. n = m+i" by presburger
chaieb@26125:   then obtain i where i: "n = m+i" by blast 
chaieb@26125:   have "fact m \<le> fact (m + i)"
chaieb@26125:   proof(induct m)
chaieb@26125:     case 0 thus ?case using fact_le[of i] by simp
chaieb@26125:   next
chaieb@26125:     case (Suc m)
chaieb@26125:     have "fact (Suc m) = Suc m * fact m" by simp
chaieb@26125:     have th1: "Suc m \<le> Suc (m + i)" by simp
chaieb@26125:     from mult_le_mono[of "Suc m" "Suc (m+i)" "fact m" "fact (m+i)", OF th1 Suc.hyps]
chaieb@26125:     show ?case by simp
chaieb@26125:   qed
chaieb@26125:   thus ?thesis using i by simp
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma divides_fact: "1 <= p \<Longrightarrow> p <= n ==> p dvd fact n"
chaieb@26125: proof(induct n arbitrary: p)
chaieb@26125:   case 0 thus ?case by simp
chaieb@26125: next
chaieb@26125:   case (Suc n p)
chaieb@26125:   from Suc.prems have "p = Suc n \<or> p \<le> n" by presburger 
chaieb@26125:   moreover
chaieb@26125:   {assume "p = Suc n" hence ?case  by (simp only: fact.simps dvd_triv_left)}
chaieb@26125:   moreover
chaieb@26125:   {assume "p \<le> n"
chaieb@26125:     with Suc.prems(1) Suc.hyps have th: "p dvd fact n" by simp
chaieb@26125:     from dvd_mult[OF th] have ?case by (simp only: fact.simps) }
chaieb@26125:   ultimately show ?case by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: declare dvd_triv_left[presburger]
chaieb@26125: declare dvd_triv_right[presburger]
chaieb@26125: lemma divides_rexp: 
chaieb@26125:   "x dvd y \<Longrightarrow> (x::nat) dvd (y^(Suc n))" by (simp add: dvd_mult2[of x y])
chaieb@26125: 
wenzelm@26144: text {* Coprimality *}
chaieb@26125: 
chaieb@26125: lemma coprime: "coprime a b \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> d = 1)"
chaieb@26125: using gcd_unique[of 1 a b, simplified] by (auto simp add: coprime_def)
chaieb@26125: lemma coprime_commute: "coprime a b \<longleftrightarrow> coprime b a" by (simp add: coprime_def gcd_commute)
chaieb@26125: 
chaieb@26125: lemma coprime_bezout: "coprime a b \<longleftrightarrow> (\<exists>x y. a * x - b * y = 1 \<or> b * x - a * y = 1)"
chaieb@26125: using coprime_def gcd_bezout by auto
chaieb@26125: 
chaieb@26125: lemma coprime_divprod: "d dvd a * b  \<Longrightarrow> coprime d a \<Longrightarrow> d dvd b"
chaieb@26125:   using relprime_dvd_mult_iff[of d a b] by (auto simp add: coprime_def mult_commute)
chaieb@26125: 
chaieb@26125: lemma coprime_1[simp]: "coprime a 1" by (simp add: coprime_def)
chaieb@26125: lemma coprime_1'[simp]: "coprime 1 a" by (simp add: coprime_def)
chaieb@26125: lemma coprime_Suc0[simp]: "coprime a (Suc 0)" by (simp add: coprime_def)
chaieb@26125: lemma coprime_Suc0'[simp]: "coprime (Suc 0) a" by (simp add: coprime_def)
chaieb@26125: 
chaieb@26125: lemma gcd_coprime: 
haftmann@27556:   assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 
chaieb@26125:   shows    "coprime a' b'"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd a b"
chaieb@26125:   {assume bz: "a = 0" from b bz z a have ?thesis by (simp add: gcd_zero coprime_def)}
chaieb@26125:   moreover 
chaieb@26125:   {assume az: "a\<noteq> 0" 
chaieb@26125:     from z have z': "?g > 0" by simp
chaieb@26125:     from bezout_gcd_strong[OF az, of b] 
chaieb@26125:     obtain x y where xy: "a*x = b*y + ?g" by blast
nipkow@29667:     from xy a b have "?g * a'*x = ?g * (b'*y + 1)" by (simp add: algebra_simps)
chaieb@26125:     hence "?g * (a'*x) = ?g * (b'*y + 1)" by (simp add: mult_assoc)
chaieb@26125:     hence "a'*x = (b'*y + 1)"
chaieb@26125:       by (simp only: nat_mult_eq_cancel1[OF z']) 
chaieb@26125:     hence "a'*x - b'*y = 1" by simp
chaieb@26125:     with coprime_bezout[of a' b'] have ?thesis by auto}
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: lemma coprime_0: "coprime d 0 \<longleftrightarrow> d = 1" by (simp add: coprime_def)
chaieb@26125: lemma coprime_mul: assumes da: "coprime d a" and db: "coprime d b"
chaieb@26125:   shows "coprime d (a * b)"
chaieb@26125: proof-
haftmann@27556:   from da have th: "gcd a d = 1" by (simp add: coprime_def gcd_commute)
chaieb@27567:   from gcd_mult_cancel[of a d b, OF th] db[unfolded coprime_def] have "gcd d (a*b) = 1"
chaieb@26125:     by (simp add: gcd_commute)
chaieb@26125:   thus ?thesis unfolding coprime_def .
chaieb@26125: qed
chaieb@26125: lemma coprime_lmul2: assumes dab: "coprime d (a * b)" shows "coprime d b"
wenzelm@41541: using dab unfolding coprime_bezout
chaieb@26125: apply clarsimp
chaieb@26125: apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
chaieb@26125: apply (rule_tac x="x" in exI)
chaieb@26125: apply (rule_tac x="a*y" in exI)
chaieb@26125: apply (simp add: mult_ac)
chaieb@26125: apply (rule_tac x="a*x" in exI)
chaieb@26125: apply (rule_tac x="y" in exI)
chaieb@26125: apply (simp add: mult_ac)
chaieb@26125: done
chaieb@26125: 
chaieb@26125: lemma coprime_rmul2: "coprime d (a * b) \<Longrightarrow> coprime d a"
chaieb@26125: unfolding coprime_bezout
chaieb@26125: apply clarsimp
chaieb@26125: apply (case_tac "d * x - a * b * y = Suc 0 ", simp_all)
chaieb@26125: apply (rule_tac x="x" in exI)
chaieb@26125: apply (rule_tac x="b*y" in exI)
chaieb@26125: apply (simp add: mult_ac)
chaieb@26125: apply (rule_tac x="b*x" in exI)
chaieb@26125: apply (rule_tac x="y" in exI)
chaieb@26125: apply (simp add: mult_ac)
chaieb@26125: done
chaieb@26125: lemma coprime_mul_eq: "coprime d (a * b) \<longleftrightarrow> coprime d a \<and>  coprime d b"
chaieb@26125:   using coprime_rmul2[of d a b] coprime_lmul2[of d a b] coprime_mul[of d a b] 
chaieb@26125:   by blast
chaieb@26125: 
chaieb@26125: lemma gcd_coprime_exists:
haftmann@27556:   assumes nz: "gcd a b \<noteq> 0" 
haftmann@27556:   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> coprime a' b'"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd a b"
chaieb@26125:   from gcd_dvd1[of a b] gcd_dvd2[of a b] 
chaieb@26125:   obtain a' b' where "a = ?g*a'"  "b = ?g*b'" unfolding dvd_def by blast
chaieb@26125:   hence ab': "a = a'*?g" "b = b'*?g" by algebra+
chaieb@26125:   from ab' gcd_coprime[OF nz ab'] show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma coprime_exp: "coprime d a ==> coprime d (a^n)" 
chaieb@26125:   by(induct n, simp_all add: coprime_mul)
chaieb@26125: 
chaieb@26125: lemma coprime_exp_imp: "coprime a b ==> coprime (a ^n) (b ^n)"
chaieb@26125:   by (induct n, simp_all add: coprime_mul_eq coprime_commute coprime_exp)
chaieb@26125: lemma coprime_refl[simp]: "coprime n n \<longleftrightarrow> n = 1" by (simp add: coprime_def)
chaieb@26125: lemma coprime_plus1[simp]: "coprime (n + 1) n"
chaieb@26125:   apply (simp add: coprime_bezout)
chaieb@26125:   apply (rule exI[where x=1])
chaieb@26125:   apply (rule exI[where x=1])
chaieb@26125:   apply simp
chaieb@26125:   done
chaieb@26125: lemma coprime_minus1: "n \<noteq> 0 ==> coprime (n - 1) n"
chaieb@26125:   using coprime_plus1[of "n - 1"] coprime_commute[of "n - 1" n] by auto
chaieb@26125: 
haftmann@27556: 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"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd a b"
chaieb@26125:   {assume z: "?g = 0" hence ?thesis 
chaieb@26125:       apply (cases n, simp)
chaieb@26125:       apply arith
chaieb@26125:       apply (simp only: z power_0_Suc)
chaieb@26125:       apply (rule exI[where x=0])
chaieb@26125:       apply (rule exI[where x=0])
wenzelm@41541:       apply simp
wenzelm@41541:       done }
chaieb@26125:   moreover
chaieb@26125:   {assume z: "?g \<noteq> 0"
chaieb@26125:     from gcd_dvd1[of a b] gcd_dvd2[of a b] obtain a' b' where
chaieb@26125:       ab': "a = a'*?g" "b = b'*?g" unfolding dvd_def by (auto simp add: mult_ac)
chaieb@26125:     hence ab'': "?g*a' = a" "?g * b' = b" by algebra+
chaieb@26125:     from coprime_exp_imp[OF gcd_coprime[OF z ab'], unfolded coprime_bezout, of n]
chaieb@26125:     obtain x y where "a'^n * x - b'^n * y = 1 \<or> b'^n * x - a'^n * y = 1"  by blast
chaieb@26125:     hence "?g^n * (a'^n * x - b'^n * y) = ?g^n \<or> ?g^n*(b'^n * x - a'^n * y) = ?g^n"
chaieb@26125:       using z by auto 
chaieb@26125:     then have "a^n * x - b^n * y = ?g^n \<or> b^n * x - a^n * y = ?g^n"
chaieb@26125:       using z ab'' by (simp only: power_mult_distrib[symmetric] 
wenzelm@32960:         diff_mult_distrib2 mult_assoc[symmetric])
chaieb@26125:     hence  ?thesis by blast }
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@27567: 
chaieb@27567: lemma gcd_exp: "gcd (a^n) (b^n) = gcd a b^n"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd (a^n) (b^n)"
chaieb@27567:   let ?gn = "gcd a b^n"
chaieb@26125:   {fix e assume H: "e dvd a^n" "e dvd b^n"
chaieb@26125:     from bezout_gcd_pow[of a n b] obtain x y 
chaieb@26125:       where xy: "a ^ n * x - b ^ n * y = ?gn \<or> b ^ n * x - a ^ n * y = ?gn" by blast
nipkow@31952:     from dvd_diff_nat [OF dvd_mult2[OF H(1), of x] dvd_mult2[OF H(2), of y]]
nipkow@31952:       dvd_diff_nat [OF dvd_mult2[OF H(2), of x] dvd_mult2[OF H(1), of y]] xy
haftmann@27556:     have "e dvd ?gn" by (cases "a ^ n * x - b ^ n * y = gcd a b ^ n", simp_all)}
chaieb@26125:   hence th:  "\<forall>e. e dvd a^n \<and> e dvd b^n \<longrightarrow> e dvd ?gn" by blast
chaieb@26125:   from divides_exp[OF gcd_dvd1[of a b], of n] divides_exp[OF gcd_dvd2[of a b], of n] th
chaieb@26125:     gcd_unique have "?gn = ?g" by blast thus ?thesis by simp 
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma coprime_exp2:  "coprime (a ^ Suc n) (b^ Suc n) \<longleftrightarrow> coprime a b"
chaieb@26125: by (simp only: coprime_def gcd_exp exp_eq_1) simp
chaieb@26125: 
chaieb@26125: lemma division_decomp: assumes dc: "(a::nat) dvd b * c"
chaieb@26125:   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd a b"
chaieb@26125:   {assume "?g = 0" with dc have ?thesis apply (simp add: gcd_zero)
chaieb@26125:       apply (rule exI[where x="0"])
chaieb@26125:       by (rule exI[where x="c"], simp)}
chaieb@26125:   moreover
chaieb@26125:   {assume z: "?g \<noteq> 0"
chaieb@26125:     from gcd_coprime_exists[OF z]
chaieb@26125:     obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
chaieb@26125:     from gcd_dvd2[of a b] have thb: "?g dvd b" .
chaieb@26125:     from ab'(1) have "a' dvd a"  unfolding dvd_def by blast  
chaieb@26125:     with dc have th0: "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
chaieb@26125:     from dc ab'(1,2) have "a'*?g dvd (b'*?g) *c" by auto
chaieb@26125:     hence "?g*a' dvd ?g * (b' * c)" by (simp add: mult_assoc)
chaieb@26125:     with z have th_1: "a' dvd b'*c" by simp
chaieb@26125:     from coprime_divprod[OF th_1 ab'(3)] have thc: "a' dvd c" . 
chaieb@26125:     from ab' have "a = ?g*a'" by algebra
chaieb@26125:     with thb thc have ?thesis by blast }
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma nat_power_eq_0_iff: "(m::nat) ^ n = 0 \<longleftrightarrow> n \<noteq> 0 \<and> m = 0" by (induct n, auto)
chaieb@26125: 
chaieb@26125: lemma divides_rev: assumes ab: "(a::nat) ^ n dvd b ^n" and n:"n \<noteq> 0" shows "a dvd b"
chaieb@26125: proof-
haftmann@27556:   let ?g = "gcd a b"
chaieb@26125:   from n obtain m where m: "n = Suc m" by (cases n, simp_all)
chaieb@26125:   {assume "?g = 0" with ab n have ?thesis by (simp add: gcd_zero)}
chaieb@26125:   moreover
chaieb@26125:   {assume z: "?g \<noteq> 0"
wenzelm@41541:     hence zn: "?g ^ n \<noteq> 0" using n by simp
chaieb@26125:     from gcd_coprime_exists[OF z] 
chaieb@26125:     obtain a' b' where ab': "a = a' * ?g" "b = b' * ?g" "coprime a' b'" by blast
chaieb@26125:     from ab have "(a' * ?g) ^ n dvd (b' * ?g)^n" by (simp add: ab'(1,2)[symmetric])
chaieb@26125:     hence "?g^n*a'^n dvd ?g^n *b'^n" by (simp only: power_mult_distrib mult_commute)
chaieb@26125:     with zn z n have th0:"a'^n dvd b'^n" by (auto simp add: nat_power_eq_0_iff)
chaieb@26125:     have "a' dvd a'^n" by (simp add: m)
chaieb@26125:     with th0 have "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by simp
chaieb@26125:     hence th1: "a' dvd b'^m * b'" by (simp add: m mult_commute)
chaieb@26125:     from coprime_divprod[OF th1 coprime_exp[OF ab'(3), of m]]
chaieb@26125:     have "a' dvd b'" .
chaieb@26125:     hence "a'*?g dvd b'*?g" by simp
chaieb@26125:     with ab'(1,2)  have ?thesis by simp }
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma divides_mul: assumes mr: "m dvd r" and nr: "n dvd r" and mn:"coprime m n" 
chaieb@26125:   shows "m * n dvd r"
chaieb@26125: proof-
chaieb@26125:   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
chaieb@26125:     unfolding dvd_def by blast
chaieb@26125:   from mr n' have "m dvd n'*n" by (simp add: mult_commute)
chaieb@26125:   hence "m dvd n'" using relprime_dvd_mult_iff[OF mn[unfolded coprime_def]] by simp
chaieb@26125:   then obtain k where k: "n' = m*k" unfolding dvd_def by blast
chaieb@26125:   from n' k show ?thesis unfolding dvd_def by auto
chaieb@26125: qed
chaieb@26125: 
wenzelm@26144: 
wenzelm@26144: text {* A binary form of the Chinese Remainder Theorem. *}
chaieb@26125: 
chaieb@26125: lemma chinese_remainder: assumes ab: "coprime a b" and a:"a \<noteq> 0" and b:"b \<noteq> 0"
chaieb@26125:   shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
chaieb@26125: proof-
chaieb@26125:   from bezout_add_strong[OF a, of b] bezout_add_strong[OF b, of a]
chaieb@26125:   obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1" 
chaieb@26125:     and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
chaieb@26125:   from gcd_unique[of 1 a b, simplified ab[unfolded coprime_def], simplified] 
chaieb@26125:     dxy1(1,2) dxy2(1,2) have d12: "d1 = 1" "d2 =1" by auto
chaieb@26125:   let ?x = "v * a * x1 + u * b * x2"
chaieb@26125:   let ?q1 = "v * x1 + u * y2"
chaieb@26125:   let ?q2 = "v * y1 + u * x2"
chaieb@26125:   from dxy2(3)[simplified d12] dxy1(3)[simplified d12] 
chaieb@26125:   have "?x = u + ?q1 * a" "?x = v + ?q2 * b" by algebra+ 
chaieb@26125:   thus ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
wenzelm@26144: text {* Primality *}
wenzelm@26144: 
wenzelm@26144: text {* A few useful theorems about primes *}
chaieb@26125: 
chaieb@26125: lemma prime_0[simp]: "~prime 0" by (simp add: prime_def)
chaieb@26125: lemma prime_1[simp]: "~ prime 1"  by (simp add: prime_def)
chaieb@26125: lemma prime_Suc0[simp]: "~ prime (Suc 0)"  by (simp add: prime_def)
chaieb@26125: 
chaieb@26125: lemma prime_ge_2: "prime p ==> p \<ge> 2" by (simp add: prime_def)
chaieb@26125: lemma prime_factor: assumes n: "n \<noteq> 1" shows "\<exists> p. prime p \<and> p dvd n"
chaieb@26125: using n
chaieb@26125: proof(induct n rule: nat_less_induct)
chaieb@26125:   fix n
chaieb@26125:   assume H: "\<forall>m<n. m \<noteq> 1 \<longrightarrow> (\<exists>p. prime p \<and> p dvd m)" "n \<noteq> 1"
chaieb@26125:   let ?ths = "\<exists>p. prime p \<and> p dvd n"
chaieb@26125:   {assume "n=0" hence ?ths using two_is_prime by auto}
chaieb@26125:   moreover
chaieb@26125:   {assume nz: "n\<noteq>0" 
chaieb@26125:     {assume "prime n" hence ?ths by - (rule exI[where x="n"], simp)}
chaieb@26125:     moreover
chaieb@26125:     {assume n: "\<not> prime n"
chaieb@26125:       with nz H(2) 
chaieb@26125:       obtain k where k:"k dvd n" "k \<noteq> 1" "k \<noteq> n" by (auto simp add: prime_def) 
chaieb@26125:       from dvd_imp_le[OF k(1)] nz k(3) have kn: "k < n" by simp
chaieb@26125:       from H(1)[rule_format, OF kn k(2)] obtain p where p: "prime p" "p dvd k" by blast
chaieb@26125:       from dvd_trans[OF p(2) k(1)] p(1) have ?ths by blast}
chaieb@26125:     ultimately have ?ths by blast}
chaieb@26125:   ultimately show ?ths by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_factor_lt: assumes p: "prime p" and n: "n \<noteq> 0" and npm:"n = p * m"
chaieb@26125:   shows "m < n"
chaieb@26125: proof-
chaieb@26125:   {assume "m=0" with n have ?thesis by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume m: "m \<noteq> 0"
chaieb@26125:     from npm have mn: "m dvd n" unfolding dvd_def by auto
chaieb@26125:     from npm m have "n \<noteq> m" using p by auto
chaieb@26125:     with dvd_imp_le[OF mn] n have ?thesis by simp}
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma euclid_bound: "\<exists>p. prime p \<and> n < p \<and>  p <= Suc (fact n)"
chaieb@26125: proof-
chaieb@26125:   have f1: "fact n + 1 \<noteq> 1" using fact_le[of n] by arith 
chaieb@26125:   from prime_factor[OF f1] obtain p where p: "prime p" "p dvd fact n + 1" by blast
chaieb@26125:   from dvd_imp_le[OF p(2)] have pfn: "p \<le> fact n + 1" by simp
chaieb@26125:   {assume np: "p \<le> n"
chaieb@26125:     from p(1) have p1: "p \<ge> 1" by (cases p, simp_all)
chaieb@26125:     from divides_fact[OF p1 np] have pfn': "p dvd fact n" .
chaieb@26125:     from divides_add_revr[OF pfn' p(2)] p(1) have False by simp}
chaieb@26125:   hence "n < p" by arith
chaieb@26125:   with p(1) pfn show ?thesis by auto
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma euclid: "\<exists>p. prime p \<and> p > n" using euclid_bound by auto
nipkow@31044: 
chaieb@26125: lemma primes_infinite: "\<not> (finite {p. prime p})"
nipkow@31044: apply(simp add: finite_nat_set_iff_bounded_le)
nipkow@31044: apply (metis euclid linorder_not_le)
nipkow@31044: done
chaieb@26125: 
chaieb@26125: lemma coprime_prime: assumes ab: "coprime a b"
chaieb@26125:   shows "~(prime p \<and> p dvd a \<and> p dvd b)"
chaieb@26125: proof
chaieb@26125:   assume "prime p \<and> p dvd a \<and> p dvd b"
chaieb@26125:   thus False using ab gcd_greatest[of p a b] by (simp add: coprime_def)
chaieb@26125: qed
chaieb@26125: lemma coprime_prime_eq: "coprime a b \<longleftrightarrow> (\<forall>p. ~(prime p \<and> p dvd a \<and> p dvd b))" 
chaieb@26125:   (is "?lhs = ?rhs")
chaieb@26125: proof-
chaieb@26125:   {assume "?lhs" with coprime_prime  have ?rhs by blast}
chaieb@26125:   moreover
chaieb@26125:   {assume r: "?rhs" and c: "\<not> ?lhs"
chaieb@26125:     then obtain g where g: "g\<noteq>1" "g dvd a" "g dvd b" unfolding coprime_def by blast
chaieb@26125:     from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
chaieb@26125:     from dvd_trans [OF p(2) g(2)] dvd_trans [OF p(2) g(3)] 
chaieb@26125:     have "p dvd a" "p dvd b" . with p(1) r have False by blast}
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_coprime: assumes p: "prime p" 
chaieb@26125:   shows "n = 1 \<or> p dvd n \<or> coprime p n"
chaieb@26125: using p prime_imp_relprime[of p n] by (auto simp add: coprime_def)
chaieb@26125: 
chaieb@26125: lemma prime_coprime_strong: "prime p \<Longrightarrow> p dvd n \<or> coprime p n"
chaieb@26125:   using prime_coprime[of p n] by auto
chaieb@26125: 
chaieb@26125: declare  coprime_0[simp]
chaieb@26125: 
chaieb@26125: lemma coprime_0'[simp]: "coprime 0 d \<longleftrightarrow> d = 1" by (simp add: coprime_commute[of 0 d])
chaieb@26125: lemma coprime_bezout_strong: assumes ab: "coprime a b" and b: "b \<noteq> 1"
chaieb@26125:   shows "\<exists>x y. a * x = b * y + 1"
chaieb@26125: proof-
chaieb@26125:   from ab b have az: "a \<noteq> 0" by - (rule ccontr, auto)
chaieb@26125:   from bezout_gcd_strong[OF az, of b] ab[unfolded coprime_def]
chaieb@26125:   show ?thesis by auto
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma bezout_prime: assumes p: "prime p"  and pa: "\<not> p dvd a"
chaieb@26125:   shows "\<exists>x y. a*x = p*y + 1"
chaieb@26125: proof-
chaieb@26125:   from p have p1: "p \<noteq> 1" using prime_1 by blast 
chaieb@26125:   from prime_coprime[OF p, of a] p1 pa have ap: "coprime a p" 
chaieb@26125:     by (auto simp add: coprime_commute)
chaieb@26125:   from coprime_bezout_strong[OF ap p1] show ?thesis . 
chaieb@26125: qed
chaieb@26125: lemma prime_divprod: assumes p: "prime p" and pab: "p dvd a*b"
chaieb@26125:   shows "p dvd a \<or> p dvd b"
chaieb@26125: proof-
chaieb@26125:   {assume "a=1" hence ?thesis using pab by simp }
chaieb@26125:   moreover
chaieb@26125:   {assume "p dvd a" hence ?thesis by blast}
chaieb@26125:   moreover
chaieb@26125:   {assume pa: "coprime p a" from coprime_divprod[OF pab pa]  have ?thesis .. }
chaieb@26125:   ultimately show ?thesis using prime_coprime[OF p, of a] by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_divprod_eq: assumes p: "prime p"
chaieb@26125:   shows "p dvd a*b \<longleftrightarrow> p dvd a \<or> p dvd b"
chaieb@26125: using p prime_divprod dvd_mult dvd_mult2 by auto
chaieb@26125: 
chaieb@26125: lemma prime_divexp: assumes p:"prime p" and px: "p dvd x^n"
chaieb@26125:   shows "p dvd x"
chaieb@26125: using px
chaieb@26125: proof(induct n)
chaieb@26125:   case 0 thus ?case by simp
chaieb@26125: next
chaieb@26125:   case (Suc n) 
chaieb@26125:   hence th: "p dvd x*x^n" by simp
chaieb@26125:   {assume H: "p dvd x^n"
chaieb@26125:     from Suc.hyps[OF H] have ?case .}
chaieb@26125:   with prime_divprod[OF p th] show ?case by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_divexp_n: "prime p \<Longrightarrow> p dvd x^n \<Longrightarrow> p^n dvd x^n"
chaieb@26125:   using prime_divexp[of p x n] divides_exp[of p x n] by blast
chaieb@26125: 
chaieb@26125: lemma coprime_prime_dvd_ex: assumes xy: "\<not>coprime x y"
chaieb@26125:   shows "\<exists>p. prime p \<and> p dvd x \<and> p dvd y"
chaieb@26125: proof-
chaieb@26125:   from xy[unfolded coprime_def] obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd y" 
chaieb@26125:     by blast
chaieb@26125:   from prime_factor[OF g(1)] obtain p where p: "prime p" "p dvd g" by blast
chaieb@26125:   from g(2,3) dvd_trans[OF p(2)] p(1) show ?thesis by auto
chaieb@26125: qed
chaieb@26125: lemma coprime_sos: assumes xy: "coprime x y" 
chaieb@26125:   shows "coprime (x * y) (x^2 + y^2)"
chaieb@26125: proof-
chaieb@26125:   {assume c: "\<not> coprime (x * y) (x^2 + y^2)"
chaieb@26125:     from coprime_prime_dvd_ex[OF c] obtain p 
chaieb@26125:       where p: "prime p" "p dvd x*y" "p dvd x^2 + y^2" by blast
chaieb@26125:     {assume px: "p dvd x"
haftmann@27651:       from dvd_mult[OF px, of x] p(3) 
haftmann@27651:         obtain r s where "x * x = p * r" and "x^2 + y^2 = p * s"
haftmann@27651:           by (auto elim!: dvdE)
haftmann@27651:         then have "y^2 = p * (s - r)" 
haftmann@27651:           by (auto simp add: power2_eq_square diff_mult_distrib2)
haftmann@27651:         then have "p dvd y^2" ..
chaieb@26125:       with prime_divexp[OF p(1), of y 2] have py: "p dvd y" .
chaieb@26125:       from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
chaieb@26125:       have False by simp }
chaieb@26125:     moreover
chaieb@26125:     {assume py: "p dvd y"
haftmann@27651:       from dvd_mult[OF py, of y] p(3)
haftmann@27651:         obtain r s where "y * y = p * r" and "x^2 + y^2 = p * s"
haftmann@27651:           by (auto elim!: dvdE)
haftmann@27651:         then have "x^2 = p * (s - r)" 
haftmann@27651:           by (auto simp add: power2_eq_square diff_mult_distrib2)
haftmann@27651:         then have "p dvd x^2" ..
chaieb@26125:       with prime_divexp[OF p(1), of x 2] have px: "p dvd x" .
chaieb@26125:       from p(1) px py xy[unfolded coprime, rule_format, of p] prime_1  
chaieb@26125:       have False by simp }
chaieb@26125:     ultimately have False using prime_divprod[OF p(1,2)] by blast}
chaieb@26125:   thus ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma distinct_prime_coprime: "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
chaieb@26125:   unfolding prime_def coprime_prime_eq by blast
chaieb@26125: 
chaieb@26125: lemma prime_coprime_lt: assumes p: "prime p" and x: "0 < x" and xp: "x < p"
chaieb@26125:   shows "coprime x p"
chaieb@26125: proof-
chaieb@26125:   {assume c: "\<not> coprime x p"
chaieb@26125:     then obtain g where g: "g \<noteq> 1" "g dvd x" "g dvd p" unfolding coprime_def by blast
chaieb@26125:   from dvd_imp_le[OF g(2)] x xp have gp: "g < p" by arith
chaieb@26125:   from g(2) x have "g \<noteq> 0" by - (rule ccontr, simp)
chaieb@26125:   with g gp p[unfolded prime_def] have False by blast}
chaieb@26125: thus ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma even_dvd[simp]: "even (n::nat) \<longleftrightarrow> 2 dvd n" by presburger
chaieb@26125: lemma prime_odd: "prime p \<Longrightarrow> p = 2 \<or> odd p" unfolding prime_def by auto
chaieb@26125: 
wenzelm@26144: 
wenzelm@26144: text {* One property of coprimality is easier to prove via prime factors. *}
chaieb@26125: 
chaieb@26125: lemma prime_divprod_pow: 
chaieb@26125:   assumes p: "prime p" and ab: "coprime a b" and pab: "p^n dvd a * b"
chaieb@26125:   shows "p^n dvd a \<or> p^n dvd b"
chaieb@26125: proof-
chaieb@26125:   {assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis 
chaieb@26125:       apply (cases "n=0", simp_all)
chaieb@26125:       apply (cases "a=1", simp_all) done}
chaieb@26125:   moreover
chaieb@26125:   {assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1" 
chaieb@26125:     then obtain m where m: "n = Suc m" by (cases n, auto)
chaieb@26125:     from divides_exp2[OF n pab] have pab': "p dvd a*b" .
chaieb@26125:     from prime_divprod[OF p pab'] 
chaieb@26125:     have "p dvd a \<or> p dvd b" .
chaieb@26125:     moreover
chaieb@26125:     {assume pa: "p dvd a"
chaieb@26125:       have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
chaieb@26125:       from coprime_prime[OF ab, of p] p pa have "\<not> p dvd b" by blast
chaieb@26125:       with prime_coprime[OF p, of b] b 
chaieb@26125:       have cpb: "coprime b p" using coprime_commute by blast 
chaieb@26125:       from coprime_exp[OF cpb] have pnb: "coprime (p^n) b" 
wenzelm@32960:         by (simp add: coprime_commute)
chaieb@26125:       from coprime_divprod[OF pnba pnb] have ?thesis by blast }
chaieb@26125:     moreover
chaieb@26125:     {assume pb: "p dvd b"
chaieb@26125:       have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
chaieb@26125:       from coprime_prime[OF ab, of p] p pb have "\<not> p dvd a" by blast
chaieb@26125:       with prime_coprime[OF p, of a] a
chaieb@26125:       have cpb: "coprime a p" using coprime_commute by blast 
chaieb@26125:       from coprime_exp[OF cpb] have pnb: "coprime (p^n) a" 
wenzelm@32960:         by (simp add: coprime_commute)
chaieb@26125:       from coprime_divprod[OF pab pnb] have ?thesis by blast }
chaieb@26125:     ultimately have ?thesis by blast}
chaieb@26125:   ultimately show ?thesis by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma nat_mult_eq_one: "(n::nat) * m = 1 \<longleftrightarrow> n = 1 \<and> m = 1" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@26125: proof
chaieb@26125:   assume H: "?lhs"
chaieb@26125:   hence "n dvd 1" "m dvd 1" unfolding dvd_def by (auto simp add: mult_commute)
chaieb@26125:   thus ?rhs by auto
chaieb@26125: next
chaieb@26125:   assume ?rhs then show ?lhs by auto
chaieb@26125: qed
chaieb@26125:   
wenzelm@41541: lemma power_Suc0: "Suc 0 ^ n = Suc 0" 
chaieb@26125:   unfolding One_nat_def[symmetric] power_one ..
wenzelm@41541: 
chaieb@26125: lemma coprime_pow: assumes ab: "coprime a b" and abcn: "a * b = c ^n"
chaieb@26125:   shows "\<exists>r s. a = r^n  \<and> b = s ^n"
chaieb@26125:   using ab abcn
chaieb@26125: proof(induct c arbitrary: a b rule: nat_less_induct)
chaieb@26125:   fix c a b
chaieb@26125:   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" 
chaieb@26125:   let ?ths = "\<exists>r s. a = r^n  \<and> b = s ^n"
chaieb@26125:   {assume n: "n = 0"
chaieb@26125:     with H(3) power_one have "a*b = 1" by simp
chaieb@26125:     hence "a = 1 \<and> b = 1" by simp
chaieb@26125:     hence ?ths 
chaieb@26125:       apply -
chaieb@26125:       apply (rule exI[where x=1])
chaieb@26125:       apply (rule exI[where x=1])
chaieb@26125:       using power_one[of  n]
chaieb@26125:       by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume n: "n \<noteq> 0" then obtain m where m: "n = Suc m" by (cases n, auto)
chaieb@26125:     {assume c: "c = 0"
chaieb@26125:       with H(3) m H(2) have ?ths apply simp 
wenzelm@32960:         apply (cases "a=0", simp_all) 
wenzelm@32960:         apply (rule exI[where x="0"], simp)
wenzelm@32960:         apply (rule exI[where x="0"], simp)
wenzelm@32960:         done}
chaieb@26125:     moreover
chaieb@26125:     {assume "c=1" with H(3) power_one have "a*b = 1" by simp 
wenzelm@32960:         hence "a = 1 \<and> b = 1" by simp
wenzelm@32960:         hence ?ths 
chaieb@26125:       apply -
chaieb@26125:       apply (rule exI[where x=1])
chaieb@26125:       apply (rule exI[where x=1])
chaieb@26125:       using power_one[of  n]
chaieb@26125:       by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume c: "c\<noteq>1" "c \<noteq> 0"
chaieb@26125:     from prime_factor[OF c(1)] obtain p where p: "prime p" "p dvd c" by blast
chaieb@26125:     from prime_divprod_pow[OF p(1) H(2), unfolded H(3), OF divides_exp[OF p(2), of n]] 
chaieb@26125:     have pnab: "p ^ n dvd a \<or> p^n dvd b" . 
chaieb@26125:     from p(2) obtain l where l: "c = p*l" unfolding dvd_def by blast
wenzelm@41541:     have pn0: "p^n \<noteq> 0" using n prime_ge_2 [OF p(1)] by simp
chaieb@26125:     {assume pa: "p^n dvd a"
chaieb@26125:       then obtain k where k: "a = p^n * k" unfolding dvd_def by blast
chaieb@26125:       from l have "l dvd c" by auto
chaieb@26125:       with dvd_imp_le[of l c] c have "l \<le> c" by auto
chaieb@26125:       moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
chaieb@26125:       ultimately have lc: "l < c" by arith
chaieb@26125:       from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" b]]]
chaieb@26125:       have kb: "coprime k b" by (simp add: coprime_commute) 
chaieb@26125:       from H(3) l k pn0 have kbln: "k * b = l ^ n" 
wenzelm@32960:         by (auto simp add: power_mult_distrib)
chaieb@26125:       from H(1)[rule_format, OF lc kb kbln]
chaieb@26125:       obtain r s where rs: "k = r ^n" "b = s^n" by blast
chaieb@26125:       from k rs(1) have "a = (p*r)^n" by (simp add: power_mult_distrib)
chaieb@26125:       with rs(2) have ?ths by blast }
chaieb@26125:     moreover
chaieb@26125:     {assume pb: "p^n dvd b"
chaieb@26125:       then obtain k where k: "b = p^n * k" unfolding dvd_def by blast
chaieb@26125:       from l have "l dvd c" by auto
chaieb@26125:       with dvd_imp_le[of l c] c have "l \<le> c" by auto
chaieb@26125:       moreover {assume "l = c" with l c  have "p = 1" by simp with p have False by simp}
chaieb@26125:       ultimately have lc: "l < c" by arith
chaieb@26125:       from coprime_lmul2 [OF H(2)[unfolded k coprime_commute[of "p^n*k" a]]]
chaieb@26125:       have kb: "coprime k a" by (simp add: coprime_commute) 
chaieb@26125:       from H(3) l k pn0 n have kbln: "k * a = l ^ n" 
wenzelm@32960:         by (simp add: power_mult_distrib mult_commute)
chaieb@26125:       from H(1)[rule_format, OF lc kb kbln]
chaieb@26125:       obtain r s where rs: "k = r ^n" "a = s^n" by blast
chaieb@26125:       from k rs(1) have "b = (p*r)^n" by (simp add: power_mult_distrib)
chaieb@26125:       with rs(2) have ?ths by blast }
chaieb@26125:     ultimately have ?ths using pnab by blast}
chaieb@26125:   ultimately have ?ths by blast}
chaieb@26125: ultimately show ?ths by blast
chaieb@26125: qed
chaieb@26125: 
wenzelm@26144: text {* More useful lemmas. *}
chaieb@26125: lemma prime_product: 
haftmann@27651:   assumes "prime (p * q)"
haftmann@27651:   shows "p = 1 \<or> q = 1"
haftmann@27651: proof -
haftmann@27651:   from assms have 
haftmann@27651:     "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
haftmann@27651:     unfolding prime_def by auto
haftmann@27651:   from `1 < p * q` have "p \<noteq> 0" by (cases p) auto
haftmann@27651:   then have Q: "p = p * q \<longleftrightarrow> q = 1" by auto
haftmann@27651:   have "p dvd p * q" by simp
haftmann@27651:   then have "p = 1 \<or> p = p * q" by (rule P)
haftmann@27651:   then show ?thesis by (simp add: Q)
haftmann@27651: qed
chaieb@26125: 
chaieb@26125: lemma prime_exp: "prime (p^n) \<longleftrightarrow> prime p \<and> n = 1"
chaieb@26125: proof(induct n)
chaieb@26125:   case 0 thus ?case by simp
chaieb@26125: next
chaieb@26125:   case (Suc n)
chaieb@26125:   {assume "p = 0" hence ?case by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume "p=1" hence ?case by simp}
chaieb@26125:   moreover
chaieb@26125:   {assume p: "p \<noteq> 0" "p\<noteq>1"
chaieb@26125:     {assume pp: "prime (p^Suc n)"
chaieb@26125:       hence "p = 1 \<or> p^n = 1" using prime_product[of p "p^n"] by simp
chaieb@26125:       with p have n: "n = 0" 
wenzelm@32960:         by (simp only: exp_eq_1 ) simp
chaieb@26125:       with pp have "prime p \<and> Suc n = 1" by simp}
chaieb@26125:     moreover
chaieb@26125:     {assume n: "prime p \<and> Suc n = 1" hence "prime (p^Suc n)" by simp}
chaieb@26125:     ultimately have ?case by blast}
chaieb@26125:   ultimately show ?case by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_power_mult: 
chaieb@26125:   assumes p: "prime p" and xy: "x * y = p ^ k"
chaieb@26125:   shows "\<exists>i j. x = p ^i \<and> y = p^ j"
chaieb@26125:   using xy
chaieb@26125: proof(induct k arbitrary: x y)
chaieb@26125:   case 0 thus ?case apply simp by (rule exI[where x="0"], simp)
chaieb@26125: next
chaieb@26125:   case (Suc k x y)
chaieb@26125:   from Suc.prems have pxy: "p dvd x*y" by auto
chaieb@26125:   from prime_divprod[OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
chaieb@26125:   from p have p0: "p \<noteq> 0" by - (rule ccontr, simp) 
chaieb@26125:   {assume px: "p dvd x"
chaieb@26125:     then obtain d where d: "x = p*d" unfolding dvd_def by blast
chaieb@26125:     from Suc.prems d  have "p*d*y = p^Suc k" by simp
chaieb@26125:     hence th: "d*y = p^k" using p0 by simp
chaieb@26125:     from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
chaieb@26125:     with d have "x = p^Suc i" by simp 
chaieb@26125:     with ij(2) have ?case by blast}
chaieb@26125:   moreover 
chaieb@26125:   {assume px: "p dvd y"
chaieb@26125:     then obtain d where d: "y = p*d" unfolding dvd_def by blast
chaieb@26125:     from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult_commute)
chaieb@26125:     hence th: "d*x = p^k" using p0 by simp
chaieb@26125:     from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
chaieb@26125:     with d have "y = p^Suc i" by simp 
chaieb@26125:     with ij(2) have ?case by blast}
chaieb@26125:   ultimately show ?case  using pxyc by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma prime_power_exp: assumes p: "prime p" and n:"n \<noteq> 0" 
chaieb@26125:   and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
chaieb@26125:   using n xn
chaieb@26125: proof(induct n arbitrary: k)
chaieb@26125:   case 0 thus ?case by simp
chaieb@26125: next
chaieb@26125:   case (Suc n k) hence th: "x*x^n = p^k" by simp
wenzelm@41541:   {assume "n = 0" with Suc have ?case by simp (rule exI[where x="k"], simp)}
chaieb@26125:   moreover
chaieb@26125:   {assume n: "n \<noteq> 0"
chaieb@26125:     from prime_power_mult[OF p th] 
chaieb@26125:     obtain i j where ij: "x = p^i" "x^n = p^j"by blast
chaieb@26125:     from Suc.hyps[OF n ij(2)] have ?case .}
chaieb@26125:   ultimately show ?case by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma divides_primepow: assumes p: "prime p" 
chaieb@26125:   shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
chaieb@26125: proof
chaieb@26125:   assume H: "d dvd p^k" then obtain e where e: "d*e = p^k" 
chaieb@26125:     unfolding dvd_def  apply (auto simp add: mult_commute) by blast
chaieb@26125:   from prime_power_mult[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
chaieb@26125:   from prime_ge_2[OF p] have p1: "p > 1" by arith
chaieb@26125:   from e ij have "p^(i + j) = p^k" by (simp add: power_add)
chaieb@26125:   hence "i + j = k" using power_inject_exp[of p "i+j" k, OF p1] by simp 
chaieb@26125:   hence "i \<le> k" by arith
chaieb@26125:   with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
chaieb@26125: next
chaieb@26125:   {fix i assume H: "i \<le> k" "d = p^i"
chaieb@26125:     hence "\<exists>j. k = i + j" by arith
chaieb@26125:     then obtain j where j: "k = i + j" by blast
chaieb@26125:     hence "p^k = p^j*d" using H(2) by (simp add: power_add)
chaieb@26125:     hence "d dvd p^k" unfolding dvd_def by auto}
chaieb@26125:   thus "\<exists>i\<le>k. d = p ^ i \<Longrightarrow> d dvd p ^ k" by blast
chaieb@26125: qed
chaieb@26125: 
chaieb@26125: lemma coprime_divisors: "d dvd a \<Longrightarrow> e dvd b \<Longrightarrow> coprime a b \<Longrightarrow> coprime d e"
chaieb@26125:   by (auto simp add: dvd_def coprime)
chaieb@26125: 
nipkow@34223: lemma mult_inj_if_coprime_nat:
nipkow@34223:   "inj_on f A \<Longrightarrow> inj_on g B \<Longrightarrow> ALL a:A. ALL b:B. coprime (f a) (g b)
nipkow@34223:    \<Longrightarrow> inj_on (%(a,b). f a * g b::nat) (A \<times> B)"
nipkow@34223: apply(auto simp add:inj_on_def)
nipkow@34223: apply(metis coprime_def dvd_triv_left gcd_proj2_if_dvd_nat gcd_semilattice_nat.inf_commute relprime_dvd_mult)
nipkow@34223: apply(metis coprime_commute coprime_divprod dvd.neq_le_trans dvd_triv_right)
nipkow@34223: done
nipkow@34223: 
chaieb@26159: declare power_Suc0[simp del]
chaieb@26159: declare even_dvd[simp del]
haftmann@26757: 
paulson@11363: end




