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