src/HOL/ex/NatSum.thy
author wenzelm
Thu, 01 Feb 2001 20:51:13 +0100
changeset 11024 23bf8d787b04
parent 8944 96964d43a472
child 11377 0f16ad464c62
permissions -rw-r--r--
converted to new-style theories;
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:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    39
  "#3 * setsum (\<lambda>i. Suc (i + i) * Suc (i + i)) (lessThan n) = n * (#4 * n * n - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    40
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    41
  txt {* This removes the @{term "-#1"} from the inductive step *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    42
   apply (case_tac [2] n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    43
    apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    44
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    45
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    46
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    47
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    48
  \medskip The sum of the first @{term n} odd cubes
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    49
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    50
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    51
lemma sum_of_odd_cubes:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    52
  "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
    53
    n * n * (#2 * n * n - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    54
  apply (induct "n")
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    55
  txt {* This removes the @{term "-#1"} from the inductive step *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    56
   apply (case_tac [2] "n")
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    57
    apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    58
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    59
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    60
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    61
  \medskip The sum of the first @{term n} positive integers equals
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    62
  @{text "n (n + 1) / 2"}.*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    63
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    64
lemma sum_of_naturals: "#2 * setsum id (atMost n) = n * Suc n"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    65
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    66
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    67
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    68
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    69
lemma sum_of_squares: "#6 * setsum (\<lambda>i. i * i) (atMost n) = n * Suc n * Suc (#2 * n)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    70
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    71
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    72
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    73
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    74
lemma sum_of_cubes: "#4 * setsum (\<lambda>i. i * i * i) (atMost n) = n * n * Suc n * Suc n"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    75
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    76
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    77
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    78
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    79
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    80
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    81
  \medskip Sum of fourth powers: two versions.
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
lemma sum_of_fourth_powers:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    85
  "#30 * setsum (\<lambda>i. i * i * i * i) (atMost n) =
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    86
    n * Suc n * Suc (#2 * n) * (#3 * n * n + #3 * n - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    87
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    88
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    89
  txt {* Eliminates the subtraction *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    90
  apply (case_tac n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    91
   apply simp_all
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    92
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    93
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    94
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    95
  Alternative proof, with a change of variables and much more
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    96
  subtraction, performed using the integers. *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    97
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    98
declare
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    99
  zmult_int [symmetric, simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   100
  zadd_zmult_distrib [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   101
  zadd_zmult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   102
  zdiff_zmult_distrib [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   103
  zdiff_zmult_distrib2 [simp]
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   104
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   105
lemma int_sum_of_fourth_powers:
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   106
  "#30 * int (setsum (\<lambda>i. i * i * i * i) (lessThan m)) =
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   107
    int m * (int m - #1) * (int (#2 * m) - #1) *
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   108
    (int (#3 * m * m) - int (#3 * m) - #1)"
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   109
  apply (induct m)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   110
   apply simp_all
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   111
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   112
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   113
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   114
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   115
  \medskip Sums of geometric series: 2, 3 and the general case *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   116
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   117
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
   118
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   119
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   120
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   121
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   122
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
   123
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   124
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   125
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   126
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   127
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
   128
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   129
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   130
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   131
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   132
end