| 16993 |      1 | (*  Title: HOL/ex/NatSum.thy
 | 
| 11024 |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Tobias Nipkow
 | 
|  |      4 | *)
 | 
|  |      5 | 
 | 
|  |      6 | header {* Summing natural numbers *}
 | 
|  |      7 | 
 | 
| 16417 |      8 | theory NatSum imports Main begin
 | 
| 11024 |      9 | 
 | 
| 11786 |     10 | text {*
 | 
|  |     11 |   Summing natural numbers, squares, cubes, etc.
 | 
|  |     12 | 
 | 
|  |     13 |   Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
 | 
|  |     14 |   \url{http://www.research.att.com/~njas/sequences/}.
 | 
|  |     15 | *}
 | 
|  |     16 | 
 | 
| 15561 |     17 | lemmas [simp] =
 | 
|  |     18 |   left_distrib right_distrib
 | 
|  |     19 |   left_diff_distrib right_diff_distrib --{*for true subtraction*}
 | 
|  |     20 |   diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
 | 
| 11024 |     21 | 
 | 
|  |     22 | text {*
 | 
| 12023 |     23 |   \medskip The sum of the first @{text n} odd numbers equals @{text n}
 | 
| 11024 |     24 |   squared.
 | 
|  |     25 | *}
 | 
|  |     26 | 
 | 
| 16593 |     27 | lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
 | 
| 16993 |     28 |   by (induct n) auto
 | 
| 11024 |     29 | 
 | 
|  |     30 | 
 | 
|  |     31 | text {*
 | 
|  |     32 |   \medskip The sum of the first @{text n} odd squares.
 | 
|  |     33 | *}
 | 
|  |     34 | 
 | 
|  |     35 | lemma sum_of_odd_squares:
 | 
| 15561 |     36 |   "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
 | 
| 16993 |     37 |   by (induct n) auto
 | 
| 11024 |     38 | 
 | 
|  |     39 | 
 | 
|  |     40 | text {*
 | 
| 12023 |     41 |   \medskip The sum of the first @{text n} odd cubes
 | 
| 11024 |     42 | *}
 | 
|  |     43 | 
 | 
|  |     44 | lemma sum_of_odd_cubes:
 | 
| 15561 |     45 |   "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
 | 
| 11786 |     46 |     n * n * (2 * n * n - 1)"
 | 
| 16993 |     47 |   by (induct n) auto
 | 
| 11024 |     48 | 
 | 
|  |     49 | text {*
 | 
| 12023 |     50 |   \medskip The sum of the first @{text n} positive integers equals
 | 
| 11024 |     51 |   @{text "n (n + 1) / 2"}.*}
 | 
|  |     52 | 
 | 
| 11586 |     53 | lemma sum_of_naturals:
 | 
| 15561 |     54 |     "2 * (\<Sum>i=0..n. i) = n * Suc n"
 | 
| 16993 |     55 |   by (induct n) auto
 | 
| 11024 |     56 | 
 | 
| 11586 |     57 | lemma sum_of_squares:
 | 
| 15561 |     58 |     "6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
 | 
| 16993 |     59 |   by (induct n) auto
 | 
| 11024 |     60 | 
 | 
| 11586 |     61 | lemma sum_of_cubes:
 | 
| 15561 |     62 |     "4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
 | 
| 16993 |     63 |   by (induct n) auto
 | 
| 11024 |     64 | 
 | 
|  |     65 | 
 | 
|  |     66 | text {*
 | 
| 15561 |     67 |   \medskip Sum of fourth powers: three versions.
 | 
| 11024 |     68 | *}
 | 
|  |     69 | 
 | 
|  |     70 | lemma sum_of_fourth_powers:
 | 
| 15561 |     71 |   "30 * (\<Sum>i=0..n. i * i * i * i) =
 | 
| 11786 |     72 |     n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
 | 
| 11024 |     73 |   apply (induct n)
 | 
|  |     74 |    apply simp_all
 | 
| 12196 |     75 |   apply (case_tac n)  -- {* eliminates the subtraction *} 
 | 
|  |     76 |    apply (simp_all (no_asm_simp))
 | 
| 11024 |     77 |   done
 | 
|  |     78 | 
 | 
|  |     79 | text {*
 | 
| 16593 |     80 |   Two alternative proofs, with a change of variables and much more
 | 
| 11024 |     81 |   subtraction, performed using the integers. *}
 | 
|  |     82 | 
 | 
|  |     83 | lemma int_sum_of_fourth_powers:
 | 
| 15561 |     84 |   "30 * int (\<Sum>i=0..<m. i * i * i * i) =
 | 
|  |     85 |     int m * (int m - 1) * (int(2 * m) - 1) *
 | 
|  |     86 |     (int(3 * m * m) - int(3 * m) - 1)"
 | 
| 16993 |     87 |   by (induct m) (simp_all add: int_mult)
 | 
| 15561 |     88 | 
 | 
|  |     89 | lemma of_nat_sum_of_fourth_powers:
 | 
|  |     90 |   "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
 | 
| 15114 |     91 |     of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
 | 
|  |     92 |     (of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
 | 
| 16993 |     93 |   by (induct m) simp_all
 | 
| 11024 |     94 | 
 | 
|  |     95 | 
 | 
|  |     96 | text {*
 | 
| 12023 |     97 |   \medskip Sums of geometric series: @{text 2}, @{text 3} and the
 | 
| 11786 |     98 |   general case.
 | 
|  |     99 | *}
 | 
| 11024 |    100 | 
 | 
| 15561 |    101 | lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
 | 
| 16993 |    102 |   by (induct n) (auto split: nat_diff_split)
 | 
| 11024 |    103 | 
 | 
| 15561 |    104 | lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
 | 
| 16993 |    105 |   by (induct n) auto
 | 
| 11024 |    106 | 
 | 
| 15561 |    107 | lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
 | 
| 16993 |    108 |   by (induct n) auto
 | 
| 11024 |    109 | 
 | 
|  |    110 | end
 |