src/HOL/ex/NatSum.thy
author haftmann
Fri, 17 Jun 2005 16:12:49 +0200
changeset 16417 9bc16273c2d4
parent 15561 045a07ac35a7
child 16593 0115764233e4
permissions -rw-r--r--
migrated theory headers to new format
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15045
d59f7e2e18d3 Moved to new m<..<n syntax for set intervals.
nipkow
parents: 12196
diff changeset
     1
(* Title: HOL/ex/NatSum.ML
11024
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
*)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     5
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     6
header {* Summing natural numbers *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     7
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15561
diff changeset
     8
theory NatSum imports Main begin
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     9
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    10
text {*
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    11
  Summing natural numbers, squares, cubes, etc.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    12
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    13
  Originally demonstrated permutative rewriting, but @{thm [source]
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    14
  add_ac} is no longer needed thanks to new simprocs.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    15
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    16
  Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    17
  \url{http://www.research.att.com/~njas/sequences/}.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    18
*}
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    19
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    20
lemmas [simp] =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    21
  lessThan_Suc atMost_Suc setsum_op_ivl_Suc setsum_cl_ivl_Suc
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    22
  left_distrib right_distrib
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    23
  left_diff_distrib right_diff_distrib --{*for true subtraction*}
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    24
  diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    25
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    26
text {*
12023
wenzelm
parents: 11786
diff changeset
    27
  \medskip The sum of the first @{text n} odd numbers equals @{text n}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    28
  squared.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    29
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    30
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    31
lemma sum_of_odds: "(\<Sum>i \<in> {0..<n}. Suc (i + i)) = n * n"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    32
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    33
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    34
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    35
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    36
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    37
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    38
  \medskip The sum of the first @{text n} odd squares.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    39
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    40
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    41
lemma sum_of_odd_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    42
  "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    43
  apply (induct n)
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    44
   apply (auto split: nat_diff_split) (*eliminate the subtraction*)
11024
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 {*
12023
wenzelm
parents: 11786
diff changeset
    49
  \medskip The sum of the first @{text n} odd cubes
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    50
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    51
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    52
lemma numeral_2_eq_2: "2 = Suc (Suc 0)" by auto
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    53
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    54
lemma sum_of_odd_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    55
  "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    56
    n * n * (2 * n * n - 1)"
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    57
  apply (induct n)
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    58
   apply (auto split: nat_diff_split) (*eliminate the subtraction*)
11024
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 {*
12023
wenzelm
parents: 11786
diff changeset
    62
  \medskip The sum of the first @{text n} positive integers equals
11024
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:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    66
    "2 * (\<Sum>i=0..n. i) = 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:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    72
    "6 * (\<Sum>i=0..n. i * i) = 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:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    78
    "4 * (\<Sum>i=0..n. i * i * i) = 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 {*
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    85
  \medskip Sum of fourth powers: three versions.
11024
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:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    89
  "30 * (\<Sum>i=0..n. i * i * i * i) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    90
    n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
11024
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 simp_all
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    93
  apply (case_tac n)  -- {* eliminates the subtraction *} 
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    94
   apply (simp_all (no_asm_simp))
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    95
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    96
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    97
text {*
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    98
  Tow alternative proofs, with a change of variables and much more
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    99
  subtraction, performed using the integers. *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   100
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   101
lemma int_sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   102
  "30 * int (\<Sum>i=0..<m. i * i * i * i) =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   103
    int m * (int m - 1) * (int(2 * m) - 1) *
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   104
    (int(3 * m * m) - int(3 * m) - 1)"
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   105
  apply (induct m)
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   106
   apply (simp_all add:zmult_int[symmetric])
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   107
  done
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   108
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   109
lemma of_nat_sum_of_fourth_powers:
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   110
  "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
15114
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   111
    of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   112
    (of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
11024
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 {*
12023
wenzelm
parents: 11786
diff changeset
   119
  \medskip Sums of geometric series: @{text 2}, @{text 3} and the
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   120
  general case.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   121
*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   122
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   123
lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   124
  apply (induct n)
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   125
   apply (auto split: nat_diff_split)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   126
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   127
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   128
lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   129
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   130
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   131
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   132
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   133
lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   134
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   135
   apply auto
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   136
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   137
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   138
end