equal
deleted
inserted
replaced
1 (* Title: HOL/NumberTheory/Primes.thy |
1 (* Title: HOL/Library/Primes.thy |
2 ID: $Id$ |
2 ID: $Id$ |
3 Author: Christophe Tabacznyj and Lawrence C Paulson |
3 Author: Christophe Tabacznyj and Lawrence C Paulson |
4 Copyright 1996 University of Cambridge |
4 Copyright 1996 University of Cambridge |
5 *) |
5 *) |
6 |
6 |
7 header {* The Greatest Common Divisor and Euclid's algorithm *} |
7 header {* |
|
8 \title{The Greatest Common Divisor and Euclid's algorithm} |
|
9 \author{Christophe Tabacznyj and Lawrence C Paulson} *} |
8 |
10 |
9 theory Primes = Main: |
11 theory Primes = Main: |
10 |
12 |
11 text {* |
13 text {* |
12 (See H. Davenport, "The Higher Arithmetic". 6th edition. (CUP, 1992)) |
14 See \cite{davenport92}. |
13 |
|
14 \bigskip |
15 \bigskip |
15 *} |
16 *} |
16 |
17 |
17 consts |
18 consts |
18 gcd :: "nat * nat => nat" -- {* Euclid's algorithm *} |
19 gcd :: "nat \<times> nat => nat" -- {* Euclid's algorithm *} |
19 |
20 |
20 recdef gcd "measure ((\<lambda>(m, n). n) :: nat * nat => nat)" |
21 recdef gcd "measure ((\<lambda>(m, n). n) :: nat \<times> nat => nat)" |
21 "gcd (m, n) = (if n = 0 then m else gcd (n, m mod n))" |
22 "gcd (m, n) = (if n = 0 then m else gcd (n, m mod n))" |
22 |
23 |
23 constdefs |
24 constdefs |
24 is_gcd :: "nat => nat => nat => bool" -- {* @{term gcd} as a relation *} |
25 is_gcd :: "nat => nat => nat => bool" -- {* @{term gcd} as a relation *} |
25 "is_gcd p m n == p dvd m \<and> p dvd n \<and> |
26 "is_gcd p m n == p dvd m \<and> p dvd n \<and> |
68 done |
69 done |
69 |
70 |
70 lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1, standard] |
71 lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1, standard] |
71 lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2, standard] |
72 lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2, standard] |
72 |
73 |
|
74 lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)" |
|
75 proof |
|
76 have "gcd (m, n) dvd m \<and> gcd (m, n) dvd n" by simp |
|
77 also assume "gcd (m, n) = 0" |
|
78 finally have "0 dvd m \<and> 0 dvd n" . |
|
79 thus "m = 0 \<and> n = 0" by (simp add: dvd_0_left) |
|
80 next |
|
81 assume "m = 0 \<and> n = 0" |
|
82 thus "gcd (m, n) = 0" by simp |
|
83 qed |
|
84 |
73 |
85 |
74 text {* |
86 text {* |
75 \medskip Maximality: for all @{term m}, @{term n}, @{term k} |
87 \medskip Maximality: for all @{term m}, @{term n}, @{term k} |
76 naturals, if @{term k} divides @{term m} and @{term k} divides |
88 naturals, if @{term k} divides @{term m} and @{term k} divides |
77 @{term n} then @{term k} divides @{term "gcd (m, n)"}. |
89 @{term n} then @{term k} divides @{term "gcd (m, n)"}. |
143 text {* |
155 text {* |
144 \medskip Multiplication laws |
156 \medskip Multiplication laws |
145 *} |
157 *} |
146 |
158 |
147 lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)" |
159 lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)" |
148 -- {* Davenport, page 27 *} |
160 -- {* \cite[page 27]{davenport92} *} |
149 apply (induct m n rule: gcd_induct) |
161 apply (induct m n rule: gcd_induct) |
150 apply simp |
162 apply simp |
151 apply (case_tac "k = 0") |
163 apply (case_tac "k = 0") |
152 apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2) |
164 apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2) |
153 done |
165 done |
185 one of those primes. |
197 one of those primes. |
186 *} |
198 *} |
187 |
199 |
188 lemma prime_dvd_mult: "p \<in> prime ==> p dvd m * n ==> p dvd m \<or> p dvd n" |
200 lemma prime_dvd_mult: "p \<in> prime ==> p dvd m * n ==> p dvd m \<or> p dvd n" |
189 apply (blast intro: relprime_dvd_mult prime_imp_relprime) |
201 apply (blast intro: relprime_dvd_mult prime_imp_relprime) |
|
202 done |
|
203 |
|
204 lemma prime_dvd_square: "p \<in> prime ==> p dvd m^2 ==> p dvd m" |
|
205 apply (auto dest: prime_dvd_mult) |
190 done |
206 done |
191 |
207 |
192 |
208 |
193 text {* \medskip Addition laws *} |
209 text {* \medskip Addition laws *} |
194 |
210 |