src/HOL/ex/NatSum.thy
author wenzelm
Wed Jun 22 10:09:20 2016 +0200 (2016-06-22)
changeset 63343 fb5d8a50c641
parent 62348 9a5f43dac883
child 63680 6e1e8b5abbfa
permissions -rw-r--r--
bundle lifting_syntax;
     1 (*  Title:  HOL/ex/NatSum.thy
     2     Author: Tobias Nipkow
     3 *)
     4 
     5 section \<open>Summing natural numbers\<close>
     6 
     7 theory NatSum imports Main begin
     8 
     9 text \<open>
    10   Summing natural numbers, squares, cubes, etc.
    11 
    12   Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
    13   @{url "http://www.research.att.com/~njas/sequences/"}.
    14 \<close>
    15 
    16 lemmas [simp] =
    17   ring_distribs
    18   diff_mult_distrib diff_mult_distrib2 \<comment>\<open>for type nat\<close>
    19 
    20 text \<open>
    21   \medskip The sum of the first \<open>n\<close> odd numbers equals \<open>n\<close>
    22   squared.
    23 \<close>
    24 
    25 lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
    26   by (induct n) auto
    27 
    28 
    29 text \<open>
    30   \medskip The sum of the first \<open>n\<close> odd squares.
    31 \<close>
    32 
    33 lemma sum_of_odd_squares:
    34   "3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
    35   by (induct n) auto
    36 
    37 
    38 text \<open>
    39   \medskip The sum of the first \<open>n\<close> odd cubes
    40 \<close>
    41 
    42 lemma sum_of_odd_cubes:
    43   "(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
    44     n * n * (2 * n * n - 1)"
    45   by (induct n) auto
    46 
    47 text \<open>
    48   \medskip The sum of the first \<open>n\<close> positive integers equals
    49   \<open>n (n + 1) / 2\<close>.\<close>
    50 
    51 lemma sum_of_naturals:
    52     "2 * (\<Sum>i=0..n. i) = n * Suc n"
    53   by (induct n) auto
    54 
    55 lemma sum_of_squares:
    56     "6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
    57   by (induct n) auto
    58 
    59 lemma sum_of_cubes:
    60     "4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
    61   by (induct n) auto
    62 
    63 text\<open>\medskip A cute identity:\<close>
    64 
    65 lemma sum_squared: "(\<Sum>i=0..n. i)^2 = (\<Sum>i=0..n::nat. i^3)"
    66 proof(induct n)
    67   case 0 show ?case by simp
    68 next
    69   case (Suc n)
    70   have "(\<Sum>i = 0..Suc n. i)^2 =
    71         (\<Sum>i = 0..n. i^3) + (2*(\<Sum>i = 0..n. i)*(n+1) + (n+1)^2)"
    72     (is "_ = ?A + ?B")
    73     using Suc by(simp add:eval_nat_numeral)
    74   also have "?B = (n+1)^3"
    75     using sum_of_naturals by(simp add:eval_nat_numeral)
    76   also have "?A + (n+1)^3 = (\<Sum>i=0..Suc n. i^3)" by simp
    77   finally show ?case .
    78 qed
    79 
    80 text \<open>
    81   \medskip Sum of fourth powers: three versions.
    82 \<close>
    83 
    84 lemma sum_of_fourth_powers:
    85   "30 * (\<Sum>i=0..n. i * i * i * i) =
    86     n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
    87   apply (induct n)
    88    apply simp_all
    89   apply (case_tac n)  \<comment> \<open>eliminates the subtraction\<close> 
    90    apply (simp_all (no_asm_simp))
    91   done
    92 
    93 text \<open>
    94   Two alternative proofs, with a change of variables and much more
    95   subtraction, performed using the integers.\<close>
    96 
    97 lemma int_sum_of_fourth_powers:
    98   "30 * int (\<Sum>i=0..<m. i * i * i * i) =
    99     int m * (int m - 1) * (int(2 * m) - 1) *
   100     (int(3 * m * m) - int(3 * m) - 1)"
   101   by (induct m) (simp_all add: of_nat_mult)
   102 
   103 lemma of_nat_sum_of_fourth_powers:
   104   "30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
   105     of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
   106     (of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
   107   by (induct m) (simp_all add: of_nat_mult)
   108 
   109 
   110 text \<open>
   111   \medskip Sums of geometric series: \<open>2\<close>, \<open>3\<close> and the
   112   general case.
   113 \<close>
   114 
   115 lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
   116   by (induct n) (auto split: nat_diff_split)
   117 
   118 lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
   119   by (induct n) auto
   120 
   121 lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
   122   by (induct n) auto
   123 
   124 end