src/HOL/ex/NatSum.thy
author wenzelm
Thu, 27 Sep 2001 15:42:08 +0200
changeset 11586 d8a7f6318457
parent 11377 0f16ad464c62
child 11701 3d51fbf81c17
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     1
(*  Title:      HOL/ex/NatSum.ML
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     2
    ID:         $Id$
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     3
    Author:     Tobias Nipkow
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     4
    Copyright   1994 TU Muenchen
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     5
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     6
Summing natural numbers, squares, cubes, etc.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     7
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     8
Originally demonstrated permutative rewriting, but add_ac is no longer
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     9
needed thanks to new simprocs.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    10
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    11
Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    12
http://www.research.att.com/~njas/sequences/
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    13
*)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    14
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    15
header {* Summing natural numbers *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    16
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    17
theory NatSum = Main:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    18
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    19
declare lessThan_Suc [simp] atMost_Suc [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    20
declare add_mult_distrib [simp] add_mult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    21
declare diff_mult_distrib [simp] diff_mult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    22
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    23
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    24
  \medskip The sum of the first @{term n} odd numbers equals @{term n}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    25
  squared.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    26
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    27
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    28
lemma sum_of_odds: "setsum (\<lambda>i. Suc (i + i)) (lessThan n) = n * n"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    29
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    30
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    31
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    32
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    33
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    34
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    35
  \medskip The sum of the first @{text n} odd squares.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    36
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    37
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    38
lemma sum_of_odd_squares:
11586
wenzelm
parents: 11377
diff changeset
    39
  "#3 * setsum (\<lambda>i. Suc (i + i) * Suc (i + i)) (lessThan n) =
wenzelm
parents: 11377
diff changeset
    40
    n * (#4 * n * n - #1)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    41
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    42
  txt {* This removes the @{term "-#1"} from the inductive step *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    43
   apply (case_tac [2] n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    44
    apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    45
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    46
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    47
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    48
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    49
  \medskip The sum of the first @{term n} odd cubes
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    50
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    51
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    52
lemma sum_of_odd_cubes:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    53
  "setsum (\<lambda>i. Suc (i + i) * Suc (i + i) * Suc (i + i)) (lessThan n) =
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    54
    n * n * (#2 * n * n - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    55
  apply (induct "n")
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    56
  txt {* This removes the @{term "-#1"} from the inductive step *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    57
   apply (case_tac [2] "n")
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    58
    apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    59
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    60
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    61
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    62
  \medskip The sum of the first @{term n} positive integers equals
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    63
  @{text "n (n + 1) / 2"}.*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    64
11586
wenzelm
parents: 11377
diff changeset
    65
lemma sum_of_naturals:
wenzelm
parents: 11377
diff changeset
    66
    "#2 * setsum id (atMost n) = n * Suc n"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    67
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    68
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    69
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    70
11586
wenzelm
parents: 11377
diff changeset
    71
lemma sum_of_squares:
wenzelm
parents: 11377
diff changeset
    72
    "#6 * setsum (\<lambda>i. i * i) (atMost n) = n * Suc n * Suc (#2 * n)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    73
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    74
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    75
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    76
11586
wenzelm
parents: 11377
diff changeset
    77
lemma sum_of_cubes:
wenzelm
parents: 11377
diff changeset
    78
    "#4 * setsum (\<lambda>i. i * i * i) (atMost n) = n * n * Suc n * Suc n"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    79
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    80
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    81
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    82
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    83
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    84
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    85
  \medskip Sum of fourth powers: two versions.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    86
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    87
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    88
lemma sum_of_fourth_powers:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    89
  "#30 * setsum (\<lambda>i. i * i * i * i) (atMost n) =
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    90
    n * Suc n * Suc (#2 * n) * (#3 * n * n + #3 * n - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    91
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    92
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    93
  txt {* Eliminates the subtraction *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    94
  apply (case_tac n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    95
   apply simp_all
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    96
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    97
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    98
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    99
  Alternative proof, with a change of variables and much more
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   100
  subtraction, performed using the integers. *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   101
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   102
declare
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   103
  zmult_int [symmetric, simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   104
  zadd_zmult_distrib [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   105
  zadd_zmult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   106
  zdiff_zmult_distrib [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   107
  zdiff_zmult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   108
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   109
lemma int_sum_of_fourth_powers:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   110
  "#30 * int (setsum (\<lambda>i. i * i * i * i) (lessThan m)) =
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   111
    int m * (int m - #1) * (int (#2 * m) - #1) *
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   112
    (int (#3 * m * m) - int (#3 * m) - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   113
  apply (induct m)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   114
   apply simp_all
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   115
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   116
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   117
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   118
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   119
  \medskip Sums of geometric series: 2, 3 and the general case *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   120
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   121
lemma sum_of_2_powers: "setsum (\<lambda>i. #2^i) (lessThan n) = #2^n - 1"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   122
  apply (induct n)
11377
0f16ad464c62 Simprocs for type "nat" no longer introduce numerals unless they are already
paulson
parents: 11024
diff changeset
   123
   apply (auto split: nat_diff_split) 
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   124
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   125
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   126
lemma sum_of_3_powers: "#2 * setsum (\<lambda>i. #3^i) (lessThan n) = #3^n - 1"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   127
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   128
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   129
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   130
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   131
lemma sum_of_powers: "0 < k ==> (k - 1) * setsum (\<lambda>i. k^i) (lessThan n) = k^n - 1"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   132
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   133
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   134
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   135
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   136
end