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