src/HOL/ex/NatSum.thy
author krauss
Tue, 28 Sep 2010 09:54:07 +0200
changeset 39754 150f831ce4a3
parent 28952 15a4b2cf8c34
child 40077 c8a9eaaa2f59
permissions -rw-r--r--
no longer declare .psimps rules as [simp]. This regularly caused confusion (e.g., they show up in simp traces when the regular simp rules are disabled). In the few places where the rules are used, explicitly mentioning them actually clarifies the proof text.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 23477
diff changeset
     1
(*  Title:  HOL/ex/NatSum.thy
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 23477
diff changeset
     2
    Author: Tobias Nipkow
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     3
*)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     4
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     5
header {* Summing natural numbers *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     6
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents: 21144
diff changeset
     7
theory NatSum imports Main Parity begin
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
     8
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
     9
text {*
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    10
  Summing natural numbers, squares, cubes, etc.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    11
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    12
  Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    13
  \url{http://www.research.att.com/~njas/sequences/}.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    14
*}
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    15
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    16
lemmas [simp] =
23477
f4b83f03cac9 tuned and renamed group_eq_simps and ring_eq_simps
nipkow
parents: 23431
diff changeset
    17
  ring_distribs
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    18
  diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    19
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    20
text {*
12023
wenzelm
parents: 11786
diff changeset
    21
  \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
    22
  squared.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    23
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    24
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    25
lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
16993
wenzelm
parents: 16593
diff changeset
    26
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    27
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    28
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    29
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    30
  \medskip The sum of the first @{text n} odd squares.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    31
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    32
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    33
lemma sum_of_odd_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    34
  "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    35
  by (induct n) auto
11024
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
text {*
12023
wenzelm
parents: 11786
diff changeset
    39
  \medskip The sum of the first @{text n} odd cubes
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    40
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    41
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    42
lemma sum_of_odd_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    43
  "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    44
    n * n * (2 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    45
  by (induct n) auto
11024
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 {*
12023
wenzelm
parents: 11786
diff changeset
    48
  \medskip The sum of the first @{text n} positive integers equals
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    49
  @{text "n (n + 1) / 2"}.*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    50
11586
wenzelm
parents: 11377
diff changeset
    51
lemma sum_of_naturals:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    52
    "2 * (\<Sum>i=0..n. i) = n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    53
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    54
11586
wenzelm
parents: 11377
diff changeset
    55
lemma sum_of_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    56
    "6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
16993
wenzelm
parents: 16593
diff changeset
    57
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    58
11586
wenzelm
parents: 11377
diff changeset
    59
lemma sum_of_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    60
    "4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    61
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    62
21144
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    63
text{* \medskip A cute identity: *}
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    64
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    65
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
    66
proof(induct n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    67
  case 0 show ?case by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    68
next
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    69
  case (Suc n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    70
  have "(\<Sum>i = 0..Suc n. i)^2 =
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    71
        (\<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
    72
    (is "_ = ?A + ?B")
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    73
    using Suc by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    74
  also have "?B = (n+1)^3"
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    75
    using sum_of_naturals by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    76
  also have "?A + (n+1)^3 = (\<Sum>i=0..Suc n. i^3)" by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    77
  finally show ?case .
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    78
qed
11024
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 {*
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    81
  \medskip Sum of fourth powers: three versions.
11024
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:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    85
  "30 * (\<Sum>i=0..n. i * i * i * i) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    86
    n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
11024
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 simp_all
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    89
  apply (case_tac n)  -- {* eliminates the subtraction *} 
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    90
   apply (simp_all (no_asm_simp))
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    91
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    92
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    93
text {*
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    94
  Two alternative proofs, with a change of variables and much more
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    95
  subtraction, performed using the integers. *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    96
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    97
lemma int_sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    98
  "30 * int (\<Sum>i=0..<m. i * i * i * i) =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    99
    int m * (int m - 1) * (int(2 * m) - 1) *
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   100
    (int(3 * m * m) - int(3 * m) - 1)"
16993
wenzelm
parents: 16593
diff changeset
   101
  by (induct m) (simp_all add: int_mult)
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   102
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   103
lemma of_nat_sum_of_fourth_powers:
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   104
  "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
15114
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   105
    of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   106
    (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
   107
  by (induct m) (simp_all add: of_nat_mult)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   108
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   109
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   110
text {*
12023
wenzelm
parents: 11786
diff changeset
   111
  \medskip Sums of geometric series: @{text 2}, @{text 3} and the
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   112
  general case.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   113
*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   114
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   115
lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   116
  by (induct n) (auto split: nat_diff_split)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   117
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   118
lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   119
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   120
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   121
lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   122
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   123
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   124
end