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