src/HOL/ex/NatSum.thy
author nipkow
Thu, 02 Nov 2006 13:35:09 +0100
changeset 21144 17b0b4c6491b
parent 16993 2ec0b8159e8e
child 21256 47195501ecf7
permissions -rw-r--r--
added sum_squared
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
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
  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] =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    18
  left_distrib right_distrib
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    19
  left_diff_distrib right_diff_distrib --{*for true subtraction*}
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    20
  diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    21
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    22
text {*
12023
wenzelm
parents: 11786
diff changeset
    23
  \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
    24
  squared.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    25
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    26
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    27
lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
16993
wenzelm
parents: 16593
diff changeset
    28
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    29
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    30
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    31
text {*
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    32
  \medskip The sum of the first @{text n} odd squares.
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    33
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    34
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    35
lemma sum_of_odd_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    36
  "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    37
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    38
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    39
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    40
text {*
12023
wenzelm
parents: 11786
diff changeset
    41
  \medskip The sum of the first @{text n} odd cubes
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    42
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    43
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    44
lemma sum_of_odd_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    45
  "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    46
    n * n * (2 * n * n - 1)"
16993
wenzelm
parents: 16593
diff changeset
    47
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    48
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    49
text {*
12023
wenzelm
parents: 11786
diff changeset
    50
  \medskip The sum of the first @{text n} positive integers equals
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    51
  @{text "n (n + 1) / 2"}.*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    52
11586
wenzelm
parents: 11377
diff changeset
    53
lemma sum_of_naturals:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    54
    "2 * (\<Sum>i=0..n. i) = n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    55
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    56
11586
wenzelm
parents: 11377
diff changeset
    57
lemma sum_of_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    58
    "6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
16993
wenzelm
parents: 16593
diff changeset
    59
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    60
11586
wenzelm
parents: 11377
diff changeset
    61
lemma sum_of_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    62
    "4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
16993
wenzelm
parents: 16593
diff changeset
    63
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    64
21144
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    65
text{* \medskip A cute identity: *}
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    66
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    67
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
    68
proof(induct n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    69
  case 0 show ?case by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    70
next
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    71
  case (Suc n)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    72
  have "(\<Sum>i = 0..Suc n. i)^2 =
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    73
        (\<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
    74
    (is "_ = ?A + ?B")
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    75
    using Suc by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    76
  also have "?B = (n+1)^3"
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    77
    using sum_of_naturals by(simp add:nat_number)
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    78
  also have "?A + (n+1)^3 = (\<Sum>i=0..Suc n. i^3)" by simp
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    79
  finally show ?case .
17b0b4c6491b added sum_squared
nipkow
parents: 16993
diff changeset
    80
qed
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    81
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    82
text {*
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    83
  \medskip Sum of fourth powers: three versions.
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    84
*}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    85
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    86
lemma sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
    87
  "30 * (\<Sum>i=0..n. i * i * i * i) =
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
    88
    n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    89
  apply (induct n)
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    90
   apply simp_all
12196
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    91
  apply (case_tac n)  -- {* eliminates the subtraction *} 
a3be6b3a9c0b new theories from Jacques Fleuriot
paulson
parents: 12023
diff changeset
    92
   apply (simp_all (no_asm_simp))
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    93
  done
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    94
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    95
text {*
16593
0115764233e4 stylistic improvements
paulson
parents: 16417
diff changeset
    96
  Two alternative proofs, with a change of variables and much more
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    97
  subtraction, performed using the integers. *}
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    98
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
    99
lemma int_sum_of_fourth_powers:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   100
  "30 * int (\<Sum>i=0..<m. i * i * i * i) =
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   101
    int m * (int m - 1) * (int(2 * m) - 1) *
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   102
    (int(3 * m * m) - int(3 * m) - 1)"
16993
wenzelm
parents: 16593
diff changeset
   103
  by (induct m) (simp_all add: int_mult)
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   104
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   105
lemma of_nat_sum_of_fourth_powers:
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   106
  "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
15114
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   107
    of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
70d1f5b7d0ad an updated treatment of the simprules
paulson
parents: 15045
diff changeset
   108
    (of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
16993
wenzelm
parents: 16593
diff changeset
   109
  by (induct m) simp_all
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   110
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   111
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   112
text {*
12023
wenzelm
parents: 11786
diff changeset
   113
  \medskip Sums of geometric series: @{text 2}, @{text 3} and the
11786
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   114
  general case.
51ce34ef5113 setsum syntax;
wenzelm
parents: 11704
diff changeset
   115
*}
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   116
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   117
lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   118
  by (induct n) (auto split: nat_diff_split)
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   119
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15114
diff changeset
   120
lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   121
  by (induct n) auto
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_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
16993
wenzelm
parents: 16593
diff changeset
   124
  by (induct n) auto
11024
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   125
23bf8d787b04 converted to new-style theories;
wenzelm
parents: 8944
diff changeset
   126
end