src/HOL/ex/NatSum.thy
author chaieb
Tue, 03 Jul 2007 17:50:00 +0200
changeset 23548 e25991f126ce
parent 23477 f4b83f03cac9
child 28952 15a4b2cf8c34
permissions -rw-r--r--
Generalized case for atoms. Selection of environment lists is allowed more than once.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16993
wenzelm
parents: 16593
diff changeset
     1
(*  Title: HOL/ex/NatSum.thy
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
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents: 21144
diff changeset
     8
theory NatSum imports Main Parity 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
  Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    14
  \url{http://www.research.att.com/~njas/sequences/}.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    15
*}
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    16
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    17
lemmas [simp] =
23477
f4b83f03cac9 tuned and renamed group_eq_simps and ring_eq_simps
nipkow
parents: 23431
diff changeset
    18
  ring_distribs
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    19
  diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    20
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    21
text {*
12023
wenzelm
parents: 11786
diff changeset
    22
  \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
    23
  squared.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    24
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    25
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    26
lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
16993
wenzelm
parents: 16593
diff changeset
    27
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    28
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    29
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    30
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    31
  \medskip The sum of the first @{text n} odd squares.
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
lemma sum_of_odd_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    35
  "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    36
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    37
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    38
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    39
text {*
12023
wenzelm
parents: 11786
diff changeset
    40
  \medskip The sum of the first @{text n} odd cubes
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    41
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    42
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    43
lemma sum_of_odd_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    44
  "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    45
    n * n * (2 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    46
  by (induct n) auto
11024
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} positive integers equals
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    50
  @{text "n (n + 1) / 2"}.*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    51
11586
wenzelm
parents: 11377
diff changeset
    52
lemma sum_of_naturals:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    53
    "2 * (\<Sum>i=0..n. i) = n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    54
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    55
11586
wenzelm
parents: 11377
diff changeset
    56
lemma sum_of_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    57
    "6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
16993
wenzelm
parents: 16593
diff changeset
    58
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    59
11586
wenzelm
parents: 11377
diff changeset
    60
lemma sum_of_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    61
    "4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    62
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    63
21144
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    64
text{* \medskip A cute identity: *}
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    65
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    66
lemma sum_squared: "(\<Sum>i=0..n. i)^2 = (\<Sum>i=0..n::nat. i^3)"
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    67
proof(induct n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    68
  case 0 show ?case by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    69
next
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    70
  case (Suc n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    71
  have "(\<Sum>i = 0..Suc n. i)^2 =
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    72
        (\<Sum>i = 0..n. i^3) + (2*(\<Sum>i = 0..n. i)*(n+1) + (n+1)^2)"
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    73
    (is "_ = ?A + ?B")
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    74
    using Suc by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    75
  also have "?B = (n+1)^3"
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    76
    using sum_of_naturals by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    77
  also have "?A + (n+1)^3 = (\<Sum>i=0..Suc n. i^3)" by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    78
  finally show ?case .
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    79
qed
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    80
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    81
text {*
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    82
  \medskip Sum of fourth powers: three versions.
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    83
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    84
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    85
lemma sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    86
  "30 * (\<Sum>i=0..n. i * i * i * i) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    87
    n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    88
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    89
   apply simp_all
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    90
  apply (case_tac n)  -- {* eliminates the subtraction *} 
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    91
   apply (simp_all (no_asm_simp))
11024
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 {*
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    95
  Two alternative proofs, with a change of variables and much more
11024
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
lemma int_sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    99
  "30 * int (\<Sum>i=0..<m. i * i * i * i) =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   100
    int m * (int m - 1) * (int(2 * m) - 1) *
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   101
    (int(3 * m * m) - int(3 * m) - 1)"
16993
wenzelm
parents: 16593
diff changeset
   102
  by (induct m) (simp_all add: int_mult)
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   103
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   104
lemma of_nat_sum_of_fourth_powers:
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   105
  "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
15114
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   106
    of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   107
    (of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
23431
25ca91279a9b change simp rules for of_nat to work like int did previously (reorient of_nat_Suc, remove of_nat_mult [simp]); preserve original variable names in legacy int theorems
huffman
parents: 21256
diff changeset
   108
  by (induct m) (simp_all add: of_nat_mult)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   109
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   110
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   111
text {*
12023
wenzelm
parents: 11786
diff changeset
   112
  \medskip Sums of geometric series: @{text 2}, @{text 3} and the
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   113
  general case.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   114
*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   115
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   116
lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   117
  by (induct n) (auto split: nat_diff_split)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   118
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   119
lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   120
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   121
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   122
lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   123
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   124
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   125
end