author | wenzelm |
Sat, 06 Oct 2001 00:02:46 +0200 | |
changeset 11704 | 3c50a2cd6f00 |
parent 11701 | 3d51fbf81c17 |
child 11786 | 51ce34ef5113 |
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: |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
39 |
"3 * setsum (\<lambda>i. Suc (i + i) * Suc (i + i)) (lessThan n) = |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
40 |
n * (4 * n * n - Numeral1)" |
11024 | 41 |
apply (induct n) |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11586
diff
changeset
|
42 |
txt {* This removes the @{term "-Numeral1"} from the inductive step *} |
11024 | 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) = |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
54 |
n * n * (2 * n * n - Numeral1)" |
11024 | 55 |
apply (induct "n") |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11586
diff
changeset
|
56 |
txt {* This removes the @{term "-Numeral1"} from the inductive step *} |
11024 | 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: |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
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: |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
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: |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
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: |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
89 |
"30 * setsum (\<lambda>i. i * i * i * i) (atMost n) = |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
90 |
n * Suc n * Suc (2 * n) * (3 * n * n + 3 * n - Numeral1)" |
11024 | 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: |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
110 |
"30 * int (setsum (\<lambda>i. i * i * i * i) (lessThan m)) = |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
111 |
int m * (int m - Numeral1) * (int (2 * m) - Numeral1) * |
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
112 |
(int (3 * m * m) - int (3 * m) - Numeral1)" |
11024 | 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 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
121 |
lemma sum_of_2_powers: "setsum (\<lambda>i. 2^i) (lessThan n) = 2^n - (1::nat)" |
11024 | 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 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
126 |
lemma sum_of_3_powers: "2 * setsum (\<lambda>i. 3^i) (lessThan n) = 3^n - (1::nat)" |
11024 | 127 |
apply (induct n) |
128 |
apply auto |
|
129 |
done |
|
130 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11586
diff
changeset
|
131 |
lemma sum_of_powers: "0 < k ==> (k - 1) * setsum (\<lambda>i. k^i) (lessThan n) = k^n - (1::nat)" |
11024 | 132 |
apply (induct n) |
133 |
apply auto |
|
134 |
done |
|
135 |
||
136 |
end |