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