15045
|
1 |
(* Title: HOL/ex/NatSum.ML
|
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 |
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 |
|
15561
|
20 |
lemmas [simp] =
|
|
21 |
left_distrib right_distrib
|
|
22 |
left_diff_distrib right_diff_distrib --{*for true subtraction*}
|
|
23 |
diff_mult_distrib diff_mult_distrib2 --{*for type nat*}
|
11024
|
24 |
|
|
25 |
text {*
|
12023
|
26 |
\medskip The sum of the first @{text n} odd numbers equals @{text n}
|
11024
|
27 |
squared.
|
|
28 |
*}
|
|
29 |
|
16593
|
30 |
lemma sum_of_odds: "(\<Sum>i=0..<n. Suc (i + i)) = n * n"
|
|
31 |
by (induct n, auto)
|
11024
|
32 |
|
|
33 |
|
|
34 |
text {*
|
|
35 |
\medskip The sum of the first @{text n} odd squares.
|
|
36 |
*}
|
|
37 |
|
|
38 |
lemma sum_of_odd_squares:
|
15561
|
39 |
"3 * (\<Sum>i=0..<n. Suc(2*i) * Suc(2*i)) = n * (4 * n * n - 1)"
|
16593
|
40 |
by (induct n, auto)
|
11024
|
41 |
|
|
42 |
|
|
43 |
text {*
|
12023
|
44 |
\medskip The sum of the first @{text n} odd cubes
|
11024
|
45 |
*}
|
|
46 |
|
|
47 |
lemma sum_of_odd_cubes:
|
15561
|
48 |
"(\<Sum>i=0..<n. Suc (2*i) * Suc (2*i) * Suc (2*i)) =
|
11786
|
49 |
n * n * (2 * n * n - 1)"
|
16593
|
50 |
by (induct n, auto)
|
11024
|
51 |
|
|
52 |
text {*
|
12023
|
53 |
\medskip The sum of the first @{text n} positive integers equals
|
11024
|
54 |
@{text "n (n + 1) / 2"}.*}
|
|
55 |
|
11586
|
56 |
lemma sum_of_naturals:
|
15561
|
57 |
"2 * (\<Sum>i=0..n. i) = n * Suc n"
|
16593
|
58 |
by (induct n, auto)
|
11024
|
59 |
|
11586
|
60 |
lemma sum_of_squares:
|
15561
|
61 |
"6 * (\<Sum>i=0..n. i * i) = n * Suc n * Suc (2 * n)"
|
16593
|
62 |
by (induct n, auto)
|
11024
|
63 |
|
11586
|
64 |
lemma sum_of_cubes:
|
15561
|
65 |
"4 * (\<Sum>i=0..n. i * i * i) = n * n * Suc n * Suc n"
|
16593
|
66 |
by (induct n, auto)
|
11024
|
67 |
|
|
68 |
|
|
69 |
text {*
|
15561
|
70 |
\medskip Sum of fourth powers: three versions.
|
11024
|
71 |
*}
|
|
72 |
|
|
73 |
lemma sum_of_fourth_powers:
|
15561
|
74 |
"30 * (\<Sum>i=0..n. i * i * i * i) =
|
11786
|
75 |
n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - 1)"
|
11024
|
76 |
apply (induct n)
|
|
77 |
apply simp_all
|
12196
|
78 |
apply (case_tac n) -- {* eliminates the subtraction *}
|
|
79 |
apply (simp_all (no_asm_simp))
|
11024
|
80 |
done
|
|
81 |
|
|
82 |
text {*
|
16593
|
83 |
Two alternative proofs, with a change of variables and much more
|
11024
|
84 |
subtraction, performed using the integers. *}
|
|
85 |
|
|
86 |
lemma int_sum_of_fourth_powers:
|
15561
|
87 |
"30 * int (\<Sum>i=0..<m. i * i * i * i) =
|
|
88 |
int m * (int m - 1) * (int(2 * m) - 1) *
|
|
89 |
(int(3 * m * m) - int(3 * m) - 1)"
|
16593
|
90 |
by (induct m, simp_all add: int_mult)
|
15561
|
91 |
|
|
92 |
lemma of_nat_sum_of_fourth_powers:
|
|
93 |
"30 * of_nat (\<Sum>i=0..<m. i * i * i * i) =
|
15114
|
94 |
of_nat m * (of_nat m - 1) * (of_nat (2 * m) - 1) *
|
|
95 |
(of_nat (3 * m * m) - of_nat (3 * m) - (1::int))"
|
16593
|
96 |
by (induct m, simp_all)
|
11024
|
97 |
|
|
98 |
|
|
99 |
text {*
|
12023
|
100 |
\medskip Sums of geometric series: @{text 2}, @{text 3} and the
|
11786
|
101 |
general case.
|
|
102 |
*}
|
11024
|
103 |
|
15561
|
104 |
lemma sum_of_2_powers: "(\<Sum>i=0..<n. 2^i) = 2^n - (1::nat)"
|
11024
|
105 |
apply (induct n)
|
11786
|
106 |
apply (auto split: nat_diff_split)
|
11024
|
107 |
done
|
|
108 |
|
15561
|
109 |
lemma sum_of_3_powers: "2 * (\<Sum>i=0..<n. 3^i) = 3^n - (1::nat)"
|
16593
|
110 |
by (induct n, auto)
|
11024
|
111 |
|
15561
|
112 |
lemma sum_of_powers: "0 < k ==> (k - 1) * (\<Sum>i=0..<n. k^i) = k^n - (1::nat)"
|
16593
|
113 |
by (induct n, auto)
|
|
114 |
|
11024
|
115 |
|
|
116 |
end
|