author | wenzelm |
Thu, 27 Sep 2001 15:42:30 +0200 | |
changeset 11587 | cf448586f26a |
parent 11586 | d8a7f6318457 |
child 11701 | 3d51fbf81c17 |
permissions | -rw-r--r-- |
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: |
|
11586 | 39 |
"#3 * setsum (\<lambda>i. Suc (i + i) * Suc (i + i)) (lessThan n) = |
40 |
n * (#4 * n * n - #1)" |
|
11024 | 41 |
apply (induct n) |
42 |
txt {* This removes the @{term "-#1"} from the inductive step *} |
|
43 |
apply (case_tac [2] n) |
|
44 |
apply auto |
|
45 |
done |
|
46 |
||
47 |
||
48 |
text {* |
|
49 |
\medskip The sum of the first @{term n} odd cubes |
|
50 |
*} |
|
51 |
||
52 |
lemma sum_of_odd_cubes: |
|
53 |
"setsum (\<lambda>i. Suc (i + i) * Suc (i + i) * Suc (i + i)) (lessThan n) = |
|
54 |
n * n * (#2 * n * n - #1)" |
|
55 |
apply (induct "n") |
|
56 |
txt {* This removes the @{term "-#1"} from the inductive step *} |
|
57 |
apply (case_tac [2] "n") |
|
58 |
apply auto |
|
59 |
done |
|
60 |
||
61 |
text {* |
|
62 |
\medskip The sum of the first @{term n} positive integers equals |
|
63 |
@{text "n (n + 1) / 2"}.*} |
|
64 |
||
11586 | 65 |
lemma sum_of_naturals: |
66 |
"#2 * setsum id (atMost n) = n * Suc n" |
|
11024 | 67 |
apply (induct n) |
68 |
apply auto |
|
69 |
done |
|
70 |
||
11586 | 71 |
lemma sum_of_squares: |
72 |
"#6 * setsum (\<lambda>i. i * i) (atMost n) = n * Suc n * Suc (#2 * n)" |
|
11024 | 73 |
apply (induct n) |
74 |
apply auto |
|
75 |
done |
|
76 |
||
11586 | 77 |
lemma sum_of_cubes: |
78 |
"#4 * setsum (\<lambda>i. i * i * i) (atMost n) = n * n * Suc n * Suc n" |
|
11024 | 79 |
apply (induct n) |
80 |
apply auto |
|
81 |
done |
|
82 |
||
83 |
||
84 |
text {* |
|
85 |
\medskip Sum of fourth powers: two versions. |
|
86 |
*} |
|
87 |
||
88 |
lemma sum_of_fourth_powers: |
|
89 |
"#30 * setsum (\<lambda>i. i * i * i * i) (atMost n) = |
|
90 |
n * Suc n * Suc (#2 * n) * (#3 * n * n + #3 * n - #1)" |
|
91 |
apply (induct n) |
|
92 |
apply auto |
|
93 |
txt {* Eliminates the subtraction *} |
|
94 |
apply (case_tac n) |
|
95 |
apply simp_all |
|
96 |
done |
|
97 |
||
98 |
text {* |
|
99 |
Alternative proof, with a change of variables and much more |
|
100 |
subtraction, performed using the integers. *} |
|
101 |
||
102 |
declare |
|
103 |
zmult_int [symmetric, simp] |
|
104 |
zadd_zmult_distrib [simp] |
|
105 |
zadd_zmult_distrib2 [simp] |
|
106 |
zdiff_zmult_distrib [simp] |
|
107 |
zdiff_zmult_distrib2 [simp] |
|
108 |
||
109 |
lemma int_sum_of_fourth_powers: |
|
110 |
"#30 * int (setsum (\<lambda>i. i * i * i * i) (lessThan m)) = |
|
111 |
int m * (int m - #1) * (int (#2 * m) - #1) * |
|
112 |
(int (#3 * m * m) - int (#3 * m) - #1)" |
|
113 |
apply (induct m) |
|
114 |
apply simp_all |
|
115 |
done |
|
116 |
||
117 |
||
118 |
text {* |
|
119 |
\medskip Sums of geometric series: 2, 3 and the general case *} |
|
120 |
||
121 |
lemma sum_of_2_powers: "setsum (\<lambda>i. #2^i) (lessThan n) = #2^n - 1" |
|
122 |
apply (induct n) |
|
11377
0f16ad464c62
Simprocs for type "nat" no longer introduce numerals unless they are already
paulson
parents:
11024
diff
changeset
|
123 |
apply (auto split: nat_diff_split) |
11024 | 124 |
done |
125 |
||
126 |
lemma sum_of_3_powers: "#2 * setsum (\<lambda>i. #3^i) (lessThan n) = #3^n - 1" |
|
127 |
apply (induct n) |
|
128 |
apply auto |
|
129 |
done |
|
130 |
||
131 |
lemma sum_of_powers: "0 < k ==> (k - 1) * setsum (\<lambda>i. k^i) (lessThan n) = k^n - 1" |
|
132 |
apply (induct n) |
|
133 |
apply auto |
|
134 |
done |
|
135 |
||
136 |
end |