author | wenzelm |
Thu, 08 Dec 2022 11:24:43 +0100 | |
changeset 76598 | 9f97eda3fcf1 |
parent 70113 | c8deb8ba6d05 |
permissions | -rw-r--r-- |
33025 | 1 |
(* Title: HOL/ex/ThreeDivides.thy |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
2 |
Author: Benjamin Porter, 2005 |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
3 |
*) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
4 |
|
61343 | 5 |
section \<open>Three Divides Theorem\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
6 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
7 |
theory ThreeDivides |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
64267
diff
changeset
|
8 |
imports Main "HOL-Library.LaTeXsugar" |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
9 |
begin |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
10 |
|
61343 | 11 |
subsection \<open>Abstract\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
12 |
|
61343 | 13 |
text \<open> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
14 |
The following document presents a proof of the Three Divides N theorem |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
15 |
formalised in the Isabelle/Isar theorem proving system. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
16 |
|
19026 | 17 |
{\em Theorem}: $3$ divides $n$ if and only if $3$ divides the sum of all |
18 |
digits in $n$. |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
19 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
20 |
{\em Informal Proof}: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
21 |
Take $n = \sum{n_j * 10^j}$ where $n_j$ is the $j$'th least |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
22 |
significant digit of the decimal denotation of the number n and the |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
23 |
sum ranges over all digits. Then $$ (n - \sum{n_j}) = \sum{n_j * (10^j |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
24 |
- 1)} $$ We know $\forall j\; 3|(10^j - 1) $ and hence $3|LHS$, |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
25 |
therefore $$\forall n\; 3|n \Longleftrightarrow 3|\sum{n_j}$$ |
61933 | 26 |
\<open>\<box>\<close> |
61343 | 27 |
\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
28 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
29 |
|
61343 | 30 |
subsection \<open>Formal proof\<close> |
23219 | 31 |
|
61343 | 32 |
subsubsection \<open>Miscellaneous summation lemmas\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
33 |
|
61933 | 34 |
text \<open>If $a$ divides \<open>A x\<close> for all x then $a$ divides any |
35 |
sum over terms of the form \<open>(A x)*(P x)\<close> for arbitrary $P$.\<close> |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
36 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
37 |
lemma div_sum: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
38 |
fixes a::nat and n::nat |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
39 |
shows "\<forall>x. a dvd A x \<Longrightarrow> a dvd (\<Sum>x<n. A x * D x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
40 |
proof (induct n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
41 |
case 0 show ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
42 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
43 |
case (Suc n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
44 |
from Suc |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
45 |
have "a dvd (A n * D n)" by (simp add: dvd_mult2) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
46 |
with Suc |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
47 |
have "a dvd ((\<Sum>x<n. A x * D x) + (A n * D n))" by (simp add: dvd_add) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
48 |
thus ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
49 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
50 |
|
23219 | 51 |
|
61343 | 52 |
subsubsection \<open>Generalised Three Divides\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
53 |
|
61343 | 54 |
text \<open>This section solves a generalised form of the three divides |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
55 |
problem. Here we show that for any sequence of numbers the theorem |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
56 |
holds. In the next section we specialise this theorem to apply |
61343 | 57 |
directly to the decimal expansion of the natural numbers.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
58 |
|
61343 | 59 |
text \<open>Here we show that the first statement in the informal proof is |
69597 | 60 |
true for all natural numbers. Note we are using \<^term>\<open>D i\<close> to |
61343 | 61 |
denote the $i$'th element in a sequence of numbers.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
62 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
63 |
lemma digit_diff_split: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
64 |
fixes n::nat and nd::nat and x::nat |
19026 | 65 |
shows "n = (\<Sum>x\<in>{..<nd}. (D x)*((10::nat)^x)) \<Longrightarrow> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
66 |
(n - (\<Sum>x<nd. (D x))) = (\<Sum>x<nd. (D x)*(10^x - 1))" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
67 |
by (simp add: sum_diff_distrib diff_mult_distrib2) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
68 |
|
61343 | 69 |
text \<open>Now we prove that 3 always divides numbers of the form $10^x - 1$.\<close> |
19026 | 70 |
lemma three_divs_0: |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
71 |
shows "(3::nat) dvd (10^x - 1)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
72 |
proof (induct x) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
73 |
case 0 show ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
74 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
75 |
case (Suc n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
76 |
let ?thr = "(3::nat)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
77 |
have "?thr dvd 9" by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
78 |
moreover |
23373 | 79 |
have "?thr dvd (10*(10^n - 1))" by (rule dvd_mult) (rule Suc) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
80 |
hence "?thr dvd (10^(n+1) - 10)" by (simp add: nat_distrib) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
81 |
ultimately |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
82 |
have"?thr dvd ((10^(n+1) - 10) + 9)" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
83 |
by (simp only: ac_simps) (rule dvd_add) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
84 |
thus ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
85 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
86 |
|
61933 | 87 |
text \<open>Expanding on the previous lemma and lemma \<open>div_sum\<close>.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
88 |
lemma three_divs_1: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
89 |
fixes D :: "nat \<Rightarrow> nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
90 |
shows "3 dvd (\<Sum>x<nd. D x * (10^x - 1))" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
41413
diff
changeset
|
91 |
by (subst mult.commute, rule div_sum) (simp add: three_divs_0 [simplified]) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
92 |
|
61933 | 93 |
text \<open>Using lemmas \<open>digit_diff_split\<close> and |
94 |
\<open>three_divs_1\<close> we now prove the following lemma. |
|
61343 | 95 |
\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
96 |
lemma three_divs_2: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
97 |
fixes nd::nat and D::"nat\<Rightarrow>nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
98 |
shows "3 dvd ((\<Sum>x<nd. (D x)*(10^x)) - (\<Sum>x<nd. (D x)))" |
19026 | 99 |
proof - |
100 |
from three_divs_1 have "3 dvd (\<Sum>x<nd. D x * (10 ^ x - 1))" . |
|
101 |
thus ?thesis by (simp only: digit_diff_split) |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
102 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
103 |
|
61343 | 104 |
text \<open> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
105 |
We now present the final theorem of this section. For any |
69597 | 106 |
sequence of numbers (defined by a function \<^term>\<open>D :: (nat\<Rightarrow>nat)\<close>), |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
107 |
we show that 3 divides the expansive sum $\sum{(D\;x)*10^x}$ over $x$ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
108 |
if and only if 3 divides the sum of the individual numbers |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
109 |
$\sum{D\;x}$. |
61343 | 110 |
\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
111 |
lemma three_div_general: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
112 |
fixes D :: "nat \<Rightarrow> nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
113 |
shows "(3 dvd (\<Sum>x<nd. D x * 10^x)) = (3 dvd (\<Sum>x<nd. D x))" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
114 |
proof |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
115 |
have mono: "(\<Sum>x<nd. D x) \<le> (\<Sum>x<nd. D x * 10^x)" |
64267 | 116 |
by (rule sum_mono) simp |
61343 | 117 |
txt \<open>This lets us form the term |
69597 | 118 |
\<^term>\<open>(\<Sum>x<nd. D x * 10^x) - (\<Sum>x<nd. D x)\<close>\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
119 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
120 |
{ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
121 |
assume "3 dvd (\<Sum>x<nd. D x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
122 |
with three_divs_2 mono |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
123 |
show "3 dvd (\<Sum>x<nd. D x * 10^x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
124 |
by (blast intro: dvd_diffD) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
125 |
} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
126 |
{ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
127 |
assume "3 dvd (\<Sum>x<nd. D x * 10^x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
128 |
with three_divs_2 mono |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
129 |
show "3 dvd (\<Sum>x<nd. D x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
130 |
by (blast intro: dvd_diffD1) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
131 |
} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
132 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
133 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
134 |
|
61343 | 135 |
subsubsection \<open>Three Divides Natural\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
136 |
|
61343 | 137 |
text \<open>This section shows that for all natural numbers we can |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
138 |
generate a sequence of digits less than ten that represent the decimal |
61933 | 139 |
expansion of the number. We then use the lemma \<open>three_div_general\<close> to prove our final theorem.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
140 |
|
23219 | 141 |
|
61343 | 142 |
text \<open>\medskip Definitions of length and digit sum.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
143 |
|
61343 | 144 |
text \<open>This section introduces some functions to calculate the |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
145 |
required properties of natural numbers. We then proceed to prove some |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
146 |
properties of these functions. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
147 |
|
61933 | 148 |
The function \<open>nlen\<close> returns the number of digits in a natural |
61343 | 149 |
number n.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
150 |
|
35419 | 151 |
fun nlen :: "nat \<Rightarrow> nat" |
152 |
where |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
153 |
"nlen 0 = 0" |
35419 | 154 |
| "nlen x = 1 + nlen (x div 10)" |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
155 |
|
61933 | 156 |
text \<open>The function \<open>sumdig\<close> returns the sum of all digits in |
61343 | 157 |
some number n.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
158 |
|
19736 | 159 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20503
diff
changeset
|
160 |
sumdig :: "nat \<Rightarrow> nat" where |
19736 | 161 |
"sumdig n = (\<Sum>x < nlen n. n div 10^x mod 10)" |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
162 |
|
61343 | 163 |
text \<open>Some properties of these functions follow.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
164 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
165 |
lemma nlen_zero: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
166 |
"0 = nlen x \<Longrightarrow> x = 0" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
167 |
by (induct x rule: nlen.induct) auto |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
168 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
169 |
lemma nlen_suc: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
170 |
"Suc m = nlen n \<Longrightarrow> m = nlen (n div 10)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
171 |
by (induct n rule: nlen.induct) simp_all |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
172 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
173 |
|
61343 | 174 |
text \<open>The following lemma is the principle lemma required to prove |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
175 |
our theorem. It states that an expansion of some natural number $n$ |
61343 | 176 |
into a sequence of its individual digits is always possible.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
177 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
178 |
lemma exp_exists: |
19026 | 179 |
"m = (\<Sum>x<nlen m. (m div (10::nat)^x mod 10) * 10^x)" |
34915 | 180 |
proof (induct "nlen m" arbitrary: m) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
181 |
case 0 thus ?case by (simp add: nlen_zero) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
182 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
183 |
case (Suc nd) |
29974 | 184 |
obtain c where mexp: "m = 10*(m div 10) + c \<and> c < 10" |
185 |
and cdef: "c = m mod 10" by simp |
|
19026 | 186 |
show "m = (\<Sum>x<nlen m. m div 10^x mod 10 * 10^x)" |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
187 |
proof - |
61343 | 188 |
from \<open>Suc nd = nlen m\<close> |
19026 | 189 |
have "nd = nlen (m div 10)" by (rule nlen_suc) |
34915 | 190 |
with Suc have |
19026 | 191 |
"m div 10 = (\<Sum>x<nd. m div 10 div 10^x mod 10 * 10^x)" by simp |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
192 |
with mexp have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
193 |
"m = 10*(\<Sum>x<nd. m div 10 div 10^x mod 10 * 10^x) + c" by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
194 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
195 |
"\<dots> = (\<Sum>x<nd. m div 10 div 10^x mod 10 * 10^(x+1)) + c" |
64267 | 196 |
by (subst sum_distrib_left) (simp add: ac_simps) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
197 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
198 |
"\<dots> = (\<Sum>x<nd. m div 10^(Suc x) mod 10 * 10^(Suc x)) + c" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
199 |
by (simp add: div_mult2_eq[symmetric]) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
200 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
201 |
"\<dots> = (\<Sum>x\<in>{Suc 0..<Suc nd}. m div 10^x mod 10 * 10^x) + c" |
70113
c8deb8ba6d05
Fixing the main Homology theory; also moving a lot of sum/prod lemmas into their generic context
paulson <lp15@cam.ac.uk>
parents:
70097
diff
changeset
|
202 |
by (simp only: sum.shift_bounds_Suc_ivl) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
203 |
(simp add: atLeast0LessThan) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
204 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
205 |
"\<dots> = (\<Sum>x<Suc nd. m div 10^x mod 10 * 10^x)" |
70097
4005298550a6
The last big tranche of Homology material: invariance of domain; renamings to use generic sum/prod lemmas from their locale
paulson <lp15@cam.ac.uk>
parents:
69597
diff
changeset
|
206 |
by (simp add: atLeast0LessThan[symmetric] sum.atLeast_Suc_lessThan cdef) |
61343 | 207 |
also note \<open>Suc nd = nlen m\<close> |
19026 | 208 |
finally |
209 |
show "m = (\<Sum>x<nlen m. m div 10^x mod 10 * 10^x)" . |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
210 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
211 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
212 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
213 |
|
61343 | 214 |
text \<open>\medskip Final theorem.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
215 |
|
61933 | 216 |
text \<open>We now combine the general theorem \<open>three_div_general\<close> |
217 |
and existence result of \<open>exp_exists\<close> to prove our final |
|
61343 | 218 |
theorem.\<close> |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
219 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
220 |
theorem three_divides_nat: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
221 |
shows "(3 dvd n) = (3 dvd sumdig n)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
222 |
proof (unfold sumdig_def) |
19026 | 223 |
have "n = (\<Sum>x<nlen n. (n div (10::nat)^x mod 10) * 10^x)" |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
224 |
by (rule exp_exists) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
225 |
moreover |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
226 |
have "3 dvd (\<Sum>x<nlen n. (n div (10::nat)^x mod 10) * 10^x) = |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
227 |
(3 dvd (\<Sum>x<nlen n. n div 10^x mod 10))" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
228 |
by (rule three_div_general) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
229 |
ultimately |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
230 |
show "3 dvd n = (3 dvd (\<Sum>x<nlen n. n div 10^x mod 10))" by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
231 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
232 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
233 |
end |