File: figurate.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (294 lines) | stat: -rw-r--r-- 7,949 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# figurate.tcl --
#     Package for evaluating expressions regarding so-called figurate numbers:
#     - triangular numbers: sum of 1, 2, 3, ... n
#     - hex numbers: sum of centred hexagons with sides of n long, sum of 1, 6, 12, 18, ..., 6n
#     - etc.
#     - also sums of kth powers of 1, 2, 3, ... n (k = 1 to 10)
#
#     Inspired by "gold", definitions of the figurate numbers following https://mathworld.wolfram.com/FigurateNumber.html
#
package require Tcl 8.6 9
package provide math::figurate 1.1

namespace eval ::math::figurate {
    namespace export sum_sequence sum_squares sum_cubes sum_4th_power sum_5th_power sum_6th_power \
              sum_7th_power sum_8th_power sum_9th_power sum_10th_power \
              sum_sequence_odd sum_squares_odd sum_cubes_odd sum_4th_power_odd sum_5th_power_odd sum_6th_power_odd \
              sum_7th_power_odd sum_8th_power_odd sum_9th_power_odd sum_10th_power_odd \
              oblong pronic triangular square cubic biquadratic centeredSquare centeredCube centeredPentagonal \
              centeredHexagonal decagonal heptagonal hexagonal octagonal octahedral pentagonal squarePyramidal \
              tetrahedral pentatope centeredTriangular
}

# sum_* --
#     Compute the sums of powers of integers 1 to n
#
# Arguments:
#     n         Largest integer in the sum
#
# Returns:
#     Sum 1**k + 2**k + ... + n**k
#
proc ::math::figurate::sum_sequence {n} {
    expr {$n > 0 ? $n * ($n+1) / 2 : 0}
}

proc ::math::figurate::sum_squares {n} {
    expr {$n > 0 ? $n*($n + 1) * (2*$n +1 ) / 6 : 0}
}

proc ::math::figurate::sum_cubes {n} {
    expr {$n > 0 ? $n**2 * ($n + 1)**2 / 4 : 0}
}

proc ::math::figurate::sum_4th_power {n} {
    expr {$n > 0 ? $n* ($n + 1) * (2*$n + 1) * (3*$n**2 + 3*$n -1 ) / 30 : 0}
}

proc ::math::figurate::sum_5th_power {n} {
    expr {$n > 0 ? $n**2 * ($n + 1)**2 * (2*$n**2 + 2*$n - 1) / 12 : 0}
}

proc ::math::figurate::sum_6th_power {n} {
    expr {$n > 0 ? $n * ($n + 1) * (2*$n + 1 ) * (3*$n**4 + 6*$n**3 - 3*$n + 1) / 42 : 0}
}

proc ::math::figurate::sum_7th_power {n} {
    expr {$n > 0 ? $n**2 * ($n + 1)**2 * (3*$n**4 + 6*$n**3 - $n**2 - 4*$n + 2) / 24 : 0}
}

proc ::math::figurate::sum_8th_power {n} {
    expr {$n > 0 ? $n * ($n + 1) * (2*$n + 1) * (5*$n**6 + 15*$n**5 + 5*$n**4 - 15*$n**3 - $n**2 + 9*$n - 3) / 90 : 0}
}

proc ::math::figurate::sum_9th_power {n} {
    expr {$n > 0 ? $n**2 * ($n + 1)**2 * (2*$n**6 + 6*$n**5 + $n**4 - 8*$n**3 + $n**2 + 6*$n - 3) / 20 : 0}
}

proc ::math::figurate::sum_10th_power {n} {
    expr {$n > 0 ? $n * ($n + 1) * (2*$n + 1) * (3*$n**8 + 12*$n**7 + 8*$n**6 - 18*$n**5 - 10*$n**4 + 24*$n**3 + 2*$n**2 - 15*$n + 5) / 66 : 0}
}

# calculate sums of odd integers:
#
# Arguments:
#     n         Number of odd integers (not the largest number)
#
# Note:
#     The procedures sum the values (2*k+1)**m from k = 1 to n
#     The calculations rely on the following identity:
#
#     Sum (2k+1)**m = Sum j**m - 2**m Sum k**m, where k = 0,...,n, j = 0,..., 2*n+1
#
proc ::math::figurate::sum_sequence_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_sequence [expr {$n-1}]]
        set sum2 [sum_sequence $maxnum]

        return [expr {$sum2 - 2 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_squares_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_squares [expr {$n-1}]]
        set sum2 [sum_squares $maxnum]

        return [expr {$sum2 - 4 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_cubes_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_cubes [expr {$n-1}]]
        set sum2 [sum_cubes $maxnum]

        return [expr {$sum2 - 8 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_4th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_4th_power [expr {$n-1}]]
        set sum2 [sum_4th_power $maxnum]

        return [expr {$sum2 - 16 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_5th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_5th_power [expr {$n-1}]]
        set sum2 [sum_5th_power $maxnum]

        return [expr {$sum2 - 32 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_6th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_6th_power [expr {$n-1}]]
        set sum2 [sum_6th_power $maxnum]

        return [expr {$sum2 - 64 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_7th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_7th_power [expr {$n-1}]]
        set sum2 [sum_7th_power $maxnum]

        return [expr {$sum2 - 128 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_8th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_8th_power [expr {$n-1}]]
        set sum2 [sum_8th_power $maxnum]

        return [expr {$sum2 - 256 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_9th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_9th_power [expr {$n-1}]]
        set sum2 [sum_9th_power $maxnum]

        return [expr {$sum2 - 512 * $sum1}]
    } else {
        return 0
    }
}

proc ::math::figurate::sum_10th_power_odd {n} {
    if { $n > 0 } {
        set maxnum [expr {2 * $n - 1}]
        set sum1 [sum_10th_power [expr {$n-1}]]
        set sum2 [sum_10th_power $maxnum]

        return [expr {$sum2 - 1024 * $sum1}]
    } else {
        return 0
    }
}

# calculate figurate numbers --
#
# Arguments:
#     n         Largest integer in the sum
#
# Notes:
#     - pronic and oblong are identical (see Mathworld page)
#     - definitions follow the Mathworld page, though in some cases a more verbose name has been chosen
#       (instead of hex, centeredHexagonal)
#     - there are some trivial procedures as well (square for instance)
#     - for the interpretation: again see the Mathworld page
#
proc ::math::figurate::oblong {n} {
    expr {$n > 0 ? $n * ($n + 1) : 0}
}

proc ::math::figurate::pronic {n} {
    expr {$n > 0 ? $n * ($n + 1) : 0}
}

proc ::math::figurate::triangular {n} {
    expr {$n > 0 ? $n * ($n + 1)/2 : 0}
}

proc ::math::figurate::square {n} {
    expr {$n > 0 ? $n**2 : 0}
}

proc ::math::figurate::cubic {n} {
    expr {$n > 0 ? $n**3 : 0}
}

proc ::math::figurate::biquadratic {n} {
    expr {$n > 0 ? $n**4 : 0}
}

proc ::math::figurate::centeredTriangular {n} {
    expr {$n > 0 ? (3*$n**2 - 3*$n + 2) / 2 : 0}
}

proc ::math::figurate::centeredSquare {n} {
    expr {$n >0 ? $n**2 + ($n-1)**2 : 0}
}

proc ::math::figurate::centeredCube {n} {
    expr {$n > 0 ? $n**3 + ($n-1)**3 : 0}
}

proc ::math::figurate::centeredPentagonal {n} {
    expr {$n > 0 ? (5*($n-1)**2 + 5*($n-1) + 2) / 2 : 0}
}

proc ::math::figurate::centeredHexagonal {n} {
    expr {$n > 0 ? 3*($n-1)**2 + 3*($n-1) + 1 : 0}
}

proc ::math::figurate::decagonal {n} {
    expr {$n > 0 ? 4*$n**2 - 3*$n : 0}
}

proc ::math::figurate::heptagonal {n} {
    expr {$n > 0 ? $n * (5*$n - 3) / 2 : 0}
}

proc ::math::figurate::hexagonal {n} {
    expr {$n > 0 ? $n * (2*$n - 1) : 0}
}

proc ::math::figurate::octagonal {n} {
    expr {$n > 0 ? $n * (3*$n - 2) : 0}
}

proc ::math::figurate::octahedral {n} {
    expr {$n > 0 ? $n * (2*$n**2 + 1) / 3 : 0}
}

proc ::math::figurate::pentagonal {n} {
    expr {$n > 0 ? $n * (3*$n - 1) / 2 : 0}
}

proc ::math::figurate::squarePyramidal {n} {
    expr {$n > 0 ? $n * ($n + 1) * (2*$n + 1)/ 6 : 0}
}

proc ::math::figurate::tetrahedral {n} {
    expr {$n > 0 ? $n * ($n + 1) *  ($n + 2) / 6 : 0}
}

proc ::math::figurate::pentatope {n} {
    expr {$n > 0 ? $n * ($n + 1) *  ($n + 2) * ($n + 3) / 24 : 0}
}