author | wenzelm |
Tue, 23 Dec 2008 00:56:03 +0100 | |
changeset 29152 | 89b0803404d7 |
parent 28071 | 6ab5b4595f64 |
child 29974 | ca93255656a5 |
permissions | -rw-r--r-- |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
1 |
(* Title: HOL/Isar_examples/ThreeDivides.thy |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
2 |
ID: $Id$ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
3 |
Author: Benjamin Porter, 2005 |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
4 |
*) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
5 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
6 |
header {* Three Divides Theorem *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
7 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
8 |
theory ThreeDivides |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
9 |
imports Main LaTeXsugar |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
10 |
begin |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
11 |
|
23219 | 12 |
subsection {* Abstract *} |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
13 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
14 |
text {* |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
15 |
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
|
16 |
formalised in the Isabelle/Isar theorem proving system. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
17 |
|
19026 | 18 |
{\em Theorem}: $3$ divides $n$ if and only if $3$ divides the sum of all |
19 |
digits in $n$. |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
20 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
21 |
{\em Informal Proof}: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
- 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
|
26 |
therefore $$\forall n\; 3|n \Longleftrightarrow 3|\sum{n_j}$$ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
27 |
@{text "\<box>"} |
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 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
30 |
|
23219 | 31 |
subsection {* Formal proof *} |
32 |
||
33 |
subsubsection {* Miscellaneous summation lemmas *} |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
34 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
35 |
text {* If $a$ divides @{text "A x"} for all x then $a$ divides any |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
36 |
sum over terms of the form @{text "(A x)*(P x)"} for arbitrary $P$. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
37 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
38 |
lemma div_sum: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
39 |
fixes a::nat and n::nat |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
40 |
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
|
41 |
proof (induct n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
42 |
case 0 show ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
43 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
44 |
case (Suc n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
45 |
from Suc |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
46 |
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
|
47 |
with Suc |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
48 |
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
|
49 |
thus ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
50 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
51 |
|
23219 | 52 |
|
53 |
subsubsection {* Generalised Three Divides *} |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
54 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
55 |
text {* This section solves a generalised form of the three divides |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
56 |
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
|
57 |
holds. In the next section we specialise this theorem to apply |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
58 |
directly to the decimal expansion of the natural numbers. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
59 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
60 |
text {* Here we show that the first statement in the informal proof is |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
61 |
true for all natural numbers. Note we are using @{term "D i"} to |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
62 |
denote the $i$'th element in a sequence of numbers. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
63 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
64 |
lemma digit_diff_split: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
65 |
fixes n::nat and nd::nat and x::nat |
19026 | 66 |
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
|
67 |
(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
|
68 |
by (simp add: sum_diff_distrib diff_mult_distrib2) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
69 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
70 |
text {* Now we prove that 3 always divides numbers of the form $10^x - 1$. *} |
19026 | 71 |
lemma three_divs_0: |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
72 |
shows "(3::nat) dvd (10^x - 1)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
73 |
proof (induct x) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
74 |
case 0 show ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
75 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
76 |
case (Suc n) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
77 |
let ?thr = "(3::nat)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
78 |
have "?thr dvd 9" by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
79 |
moreover |
23373 | 80 |
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
|
81 |
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
|
82 |
ultimately |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
83 |
have"?thr dvd ((10^(n+1) - 10) + 9)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
84 |
by (simp only: add_ac) (rule dvd_add) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
85 |
thus ?case by simp |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
86 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
87 |
|
19026 | 88 |
text {* Expanding on the previous lemma and lemma @{text "div_sum"}. *} |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
89 |
lemma three_divs_1: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
90 |
fixes D :: "nat \<Rightarrow> nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
91 |
shows "3 dvd (\<Sum>x<nd. D x * (10^x - 1))" |
19026 | 92 |
by (subst nat_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
|
93 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
94 |
text {* Using lemmas @{text "digit_diff_split"} and |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
95 |
@{text "three_divs_1"} we now prove the following lemma. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
96 |
*} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
97 |
lemma three_divs_2: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
98 |
fixes nd::nat and D::"nat\<Rightarrow>nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
99 |
shows "3 dvd ((\<Sum>x<nd. (D x)*(10^x)) - (\<Sum>x<nd. (D x)))" |
19026 | 100 |
proof - |
101 |
from three_divs_1 have "3 dvd (\<Sum>x<nd. D x * (10 ^ x - 1))" . |
|
102 |
thus ?thesis by (simp only: digit_diff_split) |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
103 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
104 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
105 |
text {* |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
106 |
We now present the final theorem of this section. For any |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
107 |
sequence of numbers (defined by a function @{term "D :: (nat\<Rightarrow>nat)"}), |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
$\sum{D\;x}$. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
111 |
*} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
112 |
lemma three_div_general: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
113 |
fixes D :: "nat \<Rightarrow> nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
114 |
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
|
115 |
proof |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
116 |
have mono: "(\<Sum>x<nd. D x) \<le> (\<Sum>x<nd. D x * 10^x)" |
19026 | 117 |
by (rule setsum_mono) simp |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
118 |
txt {* This lets us form the term |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
119 |
@{term "(\<Sum>x<nd. D x * 10^x) - (\<Sum>x<nd. D x)"} *} |
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 |
{ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
122 |
assume "3 dvd (\<Sum>x<nd. D x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
123 |
with three_divs_2 mono |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
124 |
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
|
125 |
by (blast intro: dvd_diffD) |
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 |
{ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
128 |
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
|
129 |
with three_divs_2 mono |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
130 |
show "3 dvd (\<Sum>x<nd. D x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
131 |
by (blast intro: dvd_diffD1) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
132 |
} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
133 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
134 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
135 |
|
23219 | 136 |
subsubsection {* Three Divides Natural *} |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
137 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
138 |
text {* This section shows that for all natural numbers we can |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
139 |
generate a sequence of digits less than ten that represent the decimal |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
140 |
expansion of the number. We then use the lemma @{text |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
141 |
"three_div_general"} to prove our final theorem. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
142 |
|
23219 | 143 |
|
144 |
text {* \medskip Definitions of length and digit sum. *} |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
145 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
146 |
text {* This section introduces some functions to calculate the |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
147 |
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
|
148 |
properties of these functions. |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
149 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
150 |
The function @{text "nlen"} returns the number of digits in a natural |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
151 |
number n. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
152 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
153 |
consts nlen :: "nat \<Rightarrow> nat" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
154 |
recdef nlen "measure id" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
155 |
"nlen 0 = 0" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
156 |
"nlen x = 1 + nlen (x div 10)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
157 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
158 |
text {* The function @{text "sumdig"} returns the sum of all digits in |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
159 |
some number n. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
160 |
|
19736 | 161 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20503
diff
changeset
|
162 |
sumdig :: "nat \<Rightarrow> nat" where |
19736 | 163 |
"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
|
164 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
165 |
text {* Some properties of these functions follow. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
166 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
167 |
lemma nlen_zero: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
168 |
"0 = nlen x \<Longrightarrow> x = 0" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
169 |
by (induct x rule: nlen.induct) auto |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
170 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
171 |
lemma nlen_suc: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
172 |
"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
|
173 |
by (induct n rule: nlen.induct) simp_all |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
174 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
175 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
176 |
text {* The following lemma is the principle lemma required to prove |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
177 |
our theorem. It states that an expansion of some natural number $n$ |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
178 |
into a sequence of its individual digits is always possible. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
179 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
180 |
lemma exp_exists: |
19026 | 181 |
"m = (\<Sum>x<nlen m. (m div (10::nat)^x mod 10) * 10^x)" |
20503 | 182 |
proof (induct nd \<equiv> "nlen m" arbitrary: m) |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
183 |
case 0 thus ?case by (simp add: nlen_zero) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
184 |
next |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
185 |
case (Suc nd) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
186 |
hence IH: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
187 |
"nd = nlen (m div 10) \<Longrightarrow> |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
188 |
m div 10 = (\<Sum>x<nd. m div 10 div 10^x mod 10 * 10^x)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
189 |
by blast |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
190 |
have "\<exists>c. m = 10*(m div 10) + c \<and> c < 10" by presburger |
19026 | 191 |
then obtain c where mexp: "m = 10*(m div 10) + c \<and> c < 10" .. |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
192 |
then have cdef: "c = m mod 10" by arith |
19026 | 193 |
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
|
194 |
proof - |
19026 | 195 |
from `Suc nd = nlen m` |
196 |
have "nd = nlen (m div 10)" by (rule nlen_suc) |
|
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
197 |
with IH have |
19026 | 198 |
"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
|
199 |
with mexp have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
200 |
"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
|
201 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
202 |
"\<dots> = (\<Sum>x<nd. m div 10 div 10^x mod 10 * 10^(x+1)) + c" |
19279 | 203 |
by (subst setsum_right_distrib) (simp add: mult_ac) |
19022
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<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
|
206 |
by (simp add: div_mult2_eq[symmetric]) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
207 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
208 |
"\<dots> = (\<Sum>x\<in>{Suc 0..<Suc nd}. m div 10^x mod 10 * 10^x) + c" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
209 |
by (simp only: setsum_shift_bounds_Suc_ivl) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
210 |
(simp add: atLeast0LessThan) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
211 |
also have |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
212 |
"\<dots> = (\<Sum>x<Suc nd. m div 10^x mod 10 * 10^x)" |
28071 | 213 |
by (simp add: atLeast0LessThan[symmetric] setsum_head_upt_Suc cdef) |
19026 | 214 |
also note `Suc nd = nlen m` |
215 |
finally |
|
216 |
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
|
217 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
218 |
qed |
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 |
|
23219 | 221 |
text {* \medskip Final theorem. *} |
19022
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
222 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
223 |
text {* We now combine the general theorem @{text "three_div_general"} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
224 |
and existence result of @{text "exp_exists"} to prove our final |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
225 |
theorem. *} |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
226 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
227 |
theorem three_divides_nat: |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
228 |
shows "(3 dvd n) = (3 dvd sumdig n)" |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
229 |
proof (unfold sumdig_def) |
19026 | 230 |
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
|
231 |
by (rule exp_exists) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
232 |
moreover |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
233 |
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
|
234 |
(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
|
235 |
by (rule three_div_general) |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
236 |
ultimately |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
237 |
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
|
238 |
qed |
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
239 |
|
0e6ec4fd204c
* moved ThreeDivides from Isar_examples to better suited HOL/ex
kleing
parents:
diff
changeset
|
240 |
end |