11024
|
1 |
(* Title: HOL/ex/NatSum.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Tobias Nipkow
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Summing natural numbers *}
|
|
7 |
|
|
8 |
theory NatSum = Main:
|
|
9 |
|
11786
|
10 |
text {*
|
|
11 |
Summing natural numbers, squares, cubes, etc.
|
|
12 |
|
|
13 |
Originally demonstrated permutative rewriting, but @{thm [source]
|
|
14 |
add_ac} is no longer needed thanks to new simprocs.
|
|
15 |
|
|
16 |
Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
|
|
17 |
\url{http://www.research.att.com/~njas/sequences/}.
|
|
18 |
*}
|
|
19 |
|
11024
|
20 |
declare lessThan_Suc [simp] atMost_Suc [simp]
|
|
21 |
declare add_mult_distrib [simp] add_mult_distrib2 [simp]
|
|
22 |
declare diff_mult_distrib [simp] diff_mult_distrib2 [simp]
|
|
23 |
|
|
24 |
text {*
|
12023
|
25 |
\medskip The sum of the first @{text n} odd numbers equals @{text n}
|
11024
|
26 |
squared.
|
|
27 |
*}
|
|
28 |
|
11786
|
29 |
lemma sum_of_odds: "(\<Sum>i \<in> {..n(}. Suc (i + i)) = n * n"
|
11024
|
30 |
apply (induct n)
|
|
31 |
apply auto
|
|
32 |
done
|
|
33 |
|
|
34 |
|
|
35 |
text {*
|
|
36 |
\medskip The sum of the first @{text n} odd squares.
|
|
37 |
*}
|
|
38 |
|
|
39 |
lemma sum_of_odd_squares:
|
12196
|
40 |
"3 * (\<Sum>i \<in> {..n(}. Suc (2*i) * Suc (2*i)) =
|
11786
|
41 |
n * (4 * n * n - 1)"
|
11024
|
42 |
apply (induct n)
|
12196
|
43 |
apply (auto split: nat_diff_split) (*eliminate the subtraction*)
|
11024
|
44 |
done
|
|
45 |
|
|
46 |
|
|
47 |
text {*
|
12023
|
48 |
\medskip The sum of the first @{text n} odd cubes
|
11024
|
49 |
*}
|
|
50 |
|
12196
|
51 |
lemma numeral_2_eq_2: "2 = Suc (Suc 0)" by (auto );
|
|
52 |
|
11024
|
53 |
lemma sum_of_odd_cubes:
|
12196
|
54 |
"(\<Sum>i \<in> {..n(}. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
|
11786
|
55 |
n * n * (2 * n * n - 1)"
|
|
56 |
apply (induct n)
|
12196
|
57 |
apply (auto split: nat_diff_split) (*eliminate the subtraction*)
|
11024
|
58 |
done
|
|
59 |
|
|
60 |
text {*
|
12023
|
61 |
\medskip The sum of the first @{text n} positive integers equals
|
11024
|
62 |
@{text "n (n + 1) / 2"}.*}
|
|
63 |
|
11586
|
64 |
lemma sum_of_naturals:
|
11786
|
65 |
"2 * (\<Sum>i \<in> {..n}. i) = n * Suc n"
|
11024
|
66 |
apply (induct n)
|
|
67 |
apply auto
|
|
68 |
done
|
|
69 |
|
11586
|
70 |
lemma sum_of_squares:
|
11786
|
71 |
"6 * (\<Sum>i \<in> {..n}. i * i) = n * Suc n * Suc (2 * n)"
|
11024
|
72 |
apply (induct n)
|
|
73 |
apply auto
|
|
74 |
done
|
|
75 |
|
11586
|
76 |
lemma sum_of_cubes:
|
11786
|
77 |
"4 * (\<Sum>i \<in> {..n}. i * i * i) = n * n * Suc n * Suc n"
|
11024
|
78 |
apply (induct n)
|
|
79 |
apply auto
|
|
80 |
done
|
|
81 |
|
|
82 |
|
|
83 |
text {*
|
|
84 |
\medskip Sum of fourth powers: two versions.
|
|
85 |
*}
|
|
86 |
|
|
87 |
lemma sum_of_fourth_powers:
|
11786
|
88 |
"30 * (\<Sum>i \<in> {..n}. i * i * i * i) =
|
|
89 |
n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
|
11024
|
90 |
apply (induct n)
|
|
91 |
apply simp_all
|
12196
|
92 |
apply (case_tac n) -- {* eliminates the subtraction *}
|
|
93 |
apply (simp_all (no_asm_simp))
|
11024
|
94 |
done
|
|
95 |
|
|
96 |
text {*
|
|
97 |
Alternative proof, with a change of variables and much more
|
|
98 |
subtraction, performed using the integers. *}
|
|
99 |
|
|
100 |
declare
|
|
101 |
zmult_int [symmetric, simp]
|
|
102 |
zadd_zmult_distrib [simp]
|
|
103 |
zadd_zmult_distrib2 [simp]
|
|
104 |
zdiff_zmult_distrib [simp]
|
|
105 |
zdiff_zmult_distrib2 [simp]
|
|
106 |
|
|
107 |
lemma int_sum_of_fourth_powers:
|
11786
|
108 |
"30 * int (\<Sum>i \<in> {..m(}. i * i * i * i) =
|
|
109 |
int m * (int m - 1) * (int (2 * m) - 1) *
|
|
110 |
(int (3 * m * m) - int (3 * m) - 1)"
|
11024
|
111 |
apply (induct m)
|
|
112 |
apply simp_all
|
|
113 |
done
|
|
114 |
|
|
115 |
|
|
116 |
text {*
|
12023
|
117 |
\medskip Sums of geometric series: @{text 2}, @{text 3} and the
|
11786
|
118 |
general case.
|
|
119 |
*}
|
11024
|
120 |
|
11786
|
121 |
lemma sum_of_2_powers: "(\<Sum>i \<in> {..n(}. 2^i) = 2^n - (1::nat)"
|
11024
|
122 |
apply (induct n)
|
11786
|
123 |
apply (auto split: nat_diff_split)
|
11024
|
124 |
done
|
|
125 |
|
11786
|
126 |
lemma sum_of_3_powers: "2 * (\<Sum>i \<in> {..n(}. 3^i) = 3^n - (1::nat)"
|
11024
|
127 |
apply (induct n)
|
|
128 |
apply auto
|
|
129 |
done
|
|
130 |
|
11786
|
131 |
lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i \<in> {..n(}. k^i) = k^n - (1::nat)"
|
11024
|
132 |
apply (induct n)
|
|
133 |
apply auto
|
|
134 |
done
|
|
135 |
|
|
136 |
end
|