NumSeq/Polygonal.pm

filter: Statementstmt 98.1% 159 / 162 Branchbran 90.6% 29 / 32 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 95.5% 217 / 227 CC 20 SCAR 14.2
CC
20
Cov
96%
rewind
7.0
ith
6.6
description
5.0
CRAP 4.1
« NumSeq/AllDigits.pm ↑ Summary NumSeq/Factorials.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2018, 2019, 2020, 2021 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-NumSeq is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Math::NumSeq::Polygonal;
19 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = 75;
24  
25 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 @ISA = ('Math::NumSeq::Base::IterateIth',
28 'Math::NumSeq');
29  
30 # uncomment this to run the ### lines
31 #use Smart::Comments;
32  
33 # use constant name => Math::NumSeq::__('Polygonal Numbers');
34 1 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
39 [
40 { name => 'polygonal',
41 display => Math::NumSeq::__('Polygonal'),
42 type => 'integer',
43 default => 5,
44 minimum => 3,
45 width => 3,
46 description => Math::NumSeq::__('Which polygonal numbers to show. 3 is the triangular numbers, 4 the perfect squares, 5 the pentagonal numbers, etc.'),
47 },
48 { name => 'pairs',
49 display => Math::NumSeq::__('Pairs'),
50 type => 'enum',
51 default => 'first',
52 choices => ['first',
53 'second',
54 'both',
55 'average'],
56 choices_display => [Math::NumSeq::__('First'),
57 Math::NumSeq::__('Second'),
58 Math::NumSeq::__('Both'),
59 Math::NumSeq::__('Average')],
60 description => Math::NumSeq::__('Which of the pair of values to show.'),
61 },
62 1 ];
63  
64 30 ▶sub description {
Subroutine description: called CC=5 SCAR=16.1
Pod documented
65 30 my ($self) = @_;
66 30 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 15 15 30
67 return "$self->{'polygonal'}-gonal numbers"
68 . ($self->{'pairs'} eq 'second' ? " of the second kind"
69 : $self->{'pairs'} eq 'both' ? " of both first and second kind"
70 15 ▶ : $self->{'pairs'} eq 'average' ? ", average of first and second kind"
Branch: 3 branches
BranchTrueFalseTotal
$self->{'pairs'} eq 'second' ? : 1 14 15
$self->{'pairs'} eq 'both' ? : 1 13 14
$self->{'pairs'} eq 'average' ? : 1 12 13
71 : '');
72 } else {
73 # class method
74 15 return Math::NumSeq::__('Polygonal numbers');
75 }
76 }
77  
78  
79 #------------------------------------------------------------------------------
80 # cf A183221 complement of 9-gonals
81 # A008795 molien from naive interleaved unsorted "average" polygonal=3
82 # A144065 generalized pentagonals - 1,
83 # being 2*3*4*(n+1)+1 is a perfect square
84  
85 1 my %oeis_anum;
86  
87 1 $oeis_anum{'first'}->[3] = 'A000217'; # 3 triangular
88 1 $oeis_anum{'second'}->[3] = 'A000217'; # triangular same as "first"
89 1 $oeis_anum{'both'}->[3] = 'A000217'; # no duplicates
90 1 $oeis_anum{'average'}->[3] = 'A000217'; # first==second so average same
91 # OEIS-Other: A000217 polygonal=3
92 # OEIS-Other: A000217 polygonal=3 pairs=second
93 # OEIS-Other: A000217 polygonal=3 pairs=both
94 # OEIS-Other: A000217 polygonal=3 pairs=average
95  
96 1 $oeis_anum{'first'}->[4] = 'A000290'; # 4 squares
97 1 $oeis_anum{'second'}->[4] = 'A000290'; # squares, same as "first"
98 1 $oeis_anum{'both'}->[4] = 'A000290'; # no duplicates
99 1 $oeis_anum{'average'}->[4] = 'A000290'; # squares, same as "first"
100 # OEIS-Other: A000290 polygonal=4
101 # OEIS-Other: A000290 polygonal=4 pairs=second
102 # OEIS-Other: A000290 polygonal=4 pairs=both
103 # OEIS-Other: A000290 polygonal=4 pairs=average
104  
105 1 $oeis_anum{'first'}->[5] = 'A000326'; # 5 pentagonal
106 1 $oeis_anum{'second'}->[5] = 'A005449';
107 1 $oeis_anum{'both'}->[5] = 'A001318';
108 # OEIS-Catalogue: A000326 polygonal=5 pairs=first
109 # OEIS-Catalogue: A005449 polygonal=5 pairs=second
110 # OEIS-Catalogue: A001318 polygonal=5 pairs=both
111  
112 1 $oeis_anum{'first'}->[6] = 'A000384'; # 6 hexagonal
113 1 $oeis_anum{'second'}->[6] = 'A014105';
114 1 $oeis_anum{'both'}->[6] = 'A000217'; # together triangular numbers
115 1 $oeis_anum{'average'}->[6] = 'A001105'; # (k-2)/2==2 is 2*n^2
116 # OEIS-Catalogue: A000384 polygonal=6 pairs=first
117 # OEIS-Catalogue: A014105 polygonal=6 pairs=second
118 # OEIS-Other: A000217 polygonal=6 pairs=both
119 # OEIS-Catalogue: A001105 polygonal=6 pairs=average
120  
121 1 $oeis_anum{'first'}->[7] = 'A000566'; # 7 heptagonal n(5n-3)/2
122 1 $oeis_anum{'both'}->[7] = 'A085787';
123 # OEIS-Catalogue: A000566 polygonal=7
124 # OEIS-Catalogue: A085787 polygonal=7 pairs=both
125 #
126 # Not quite, (5n-2)(n-1)/2 starting n=1 is the same values, whereas
127 # Polygonal seconds starting n=0 would be (-n)(5*-n-3)/2=n(5n+3)
128 # # $oeis_anum{'second'}->[7] = 'A147875'; # (5n-2)(n-1)/2
129 # # # OEIS-Catalogue: A147875 polygonal=7 pairs=second
130  
131 1 $oeis_anum{'first'}->[8] = 'A000567'; # 8 octagonal
132 1 $oeis_anum{'second'}->[8] = 'A045944'; # Rhombic matchstick n*(3*n+2)
133 1 $oeis_anum{'average'}->[8] = 'A033428'; # (k-2)/2==3 is 3*squares
134 # OEIS-Catalogue: A000567 polygonal=8
135 # OEIS-Catalogue: A045944 polygonal=8 pairs=second
136 # OEIS-Catalogue: A033428 polygonal=8 pairs=average
137 #
138 # A001082 n(3n-4)/4 if n even, (n-1)(3n+1)/4 if n odd
139 # is not quite generalized octagonals
140 # Generalized would be n*(3n-2) for n=0,1,-1,2,-2,etc
141 # # $oeis_anum{'both'}->[8] = 'A001082';
142 # # # OEIS-Catalogue: A001082 polygonal=8 pairs=both
143  
144 1 $oeis_anum{'first'}->[9] = 'A001106'; # 9 nonagonal
145 1 $oeis_anum{'second'}->[9] = 'A179986'; # 9 nonagonal second n*(7*n+5)/2
146 1 $oeis_anum{'both'}->[9] = 'A118277'; # 9 nonagonal "generalized"
147 # OEIS-Catalogue: A001106 polygonal=9
148 # OEIS-Catalogue: A179986 polygonal=9 pairs=second
149 # OEIS-Catalogue: A118277 polygonal=9 pairs=both
150  
151 1 $oeis_anum{'first'}->[10] = 'A001107'; # 10 decogaonal
152 1 $oeis_anum{'second'}->[10] = 'A033954'; # 10 second n*(4*n+3)
153 1 $oeis_anum{'both'}->[10] = 'A074377'; # 10 both "generalized"
154 1 $oeis_anum{'average'}->[10] = 'A016742'; # (k-2)/2==4 is 4*squares
155 # OEIS-Catalogue: A001107 polygonal=10
156 # OEIS-Catalogue: A033954 polygonal=10 pairs=second
157 # OEIS-Catalogue: A074377 polygonal=10 pairs=both
158 # OEIS-Catalogue: A016742 polygonal=10 pairs=average
159  
160 1 $oeis_anum{'first'}->[11] = 'A051682'; # 11 hendecagonal
161 1 $oeis_anum{'second'}->[11] = 'A062728'; # 11 second n*(9n+7)/2
162 1 $oeis_anum{'both'}->[11] = 'A195160'; # 11 generalized
163 # OEIS-Catalogue: A051682 polygonal=11
164 # OEIS-Catalogue: A062728 polygonal=11 pairs=second
165 # OEIS-Catalogue: A195160 polygonal=11 pairs=both
166  
167 1 $oeis_anum{'first'}->[12] = 'A051624'; # 12-gonal
168 1 $oeis_anum{'second'}->[12] = 'A135705'; # 12-gonal second
169 1 $oeis_anum{'both'}->[12] = 'A195162'; # 12-gonal generalized
170 1 $oeis_anum{'average'}->[12] = 'A033429'; # (k-2)/2==5 is 5*squares
171 # OEIS-Catalogue: A051624 polygonal=12
172 # OEIS-Catalogue: A135705 polygonal=12 pairs=second
173 # OEIS-Catalogue: A195162 polygonal=12 pairs=both
174 # OEIS-Catalogue: A033429 polygonal=12 pairs=average
175  
176 1 $oeis_anum{'second'}->[13] = 'A211013'; # 13-gonal second
177 1 $oeis_anum{'both'}->[13] = 'A195313'; # 13-gonal generalized
178 # OEIS-Catalogue: A211013 polygonal=13 pairs=second
179 # OEIS-Catalogue: A195313 polygonal=13 pairs=both
180  
181 1 $oeis_anum{'second'}->[14] = 'A211014'; # 14-gonal second
182 1 $oeis_anum{'both'}->[14] = 'A195818'; # 14-gonal generalized
183 1 $oeis_anum{'average'}->[14] = 'A033581'; # (k-2)/2==6 is 6*squares
184 # OEIS-Catalogue: A211014 polygonal=14 pairs=second
185 # OEIS-Catalogue: A195818 polygonal=14 pairs=both
186 # OEIS-Catalogue: A033581 polygonal=14 pairs=average
187  
188 1 $oeis_anum{'both'}->[15] = 'A277082'; # 15-gonal generalized
189 # OEIS-Catalogue: A277082 polygonal=15 pairs=both
190  
191 1 $oeis_anum{'both'}->[16] = 'A274978'; # 16-gonal generalized
192 1 $oeis_anum{'average'}->[16] = 'A033582'; # (k-2)/2==7 is 7*squares
193 # OEIS-Catalogue: A033582 polygonal=16 pairs=average
194  
195 # these in sequence ...
196 1 $oeis_anum{'first'}->[13] = 'A051865'; # 13 tridecagonal
197 1 $oeis_anum{'first'}->[14] = 'A051866'; # 14-gonal
198 1 $oeis_anum{'first'}->[15] = 'A051867'; # 15
199 1 $oeis_anum{'first'}->[16] = 'A051868'; # 16
200 1 $oeis_anum{'first'}->[17] = 'A051869'; # 17
201 1 $oeis_anum{'first'}->[18] = 'A051870'; # 18
202 1 $oeis_anum{'first'}->[19] = 'A051871'; # 19
203 1 $oeis_anum{'first'}->[20] = 'A051872'; # 20
204 1 $oeis_anum{'first'}->[21] = 'A051873'; # 21
205 1 $oeis_anum{'first'}->[22] = 'A051874'; # 22
206 1 $oeis_anum{'first'}->[23] = 'A051875'; # 23
207 1 $oeis_anum{'first'}->[24] = 'A051876'; # 24
208 # OEIS-Catalogue: A051865 polygonal=13
209 # OEIS-Catalogue: A051866 polygonal=14
210 # OEIS-Catalogue: A051867 polygonal=15
211 # OEIS-Catalogue: A051868 polygonal=16
212 # OEIS-Catalogue: A051869 polygonal=17
213 # OEIS-Catalogue: A051870 polygonal=18
214 # OEIS-Catalogue: A051871 polygonal=19
215 # OEIS-Catalogue: A051872 polygonal=20
216 # OEIS-Catalogue: A051873 polygonal=21
217 # OEIS-Catalogue: A051874 polygonal=22
218 # OEIS-Catalogue: A051875 polygonal=23
219 # OEIS-Catalogue: A051876 polygonal=24
220  
221 1 $oeis_anum{'first'}->[25] = 'A255184'; # 25
222 1 $oeis_anum{'first'}->[26] = 'A255185'; # 26
223 1 $oeis_anum{'first'}->[27] = 'A255186'; # 27
224 1 $oeis_anum{'first'}->[29] = 'A255187'; # 29
225 # OEIS-Catalogue: A255184 polygonal=25
226 # OEIS-Catalogue: A255185 polygonal=26
227 # OEIS-Catalogue: A255186 polygonal=27
228 # OEIS-Catalogue: A255187 polygonal=29
229  
230 # A161935 (n+1)*(13*n+1) is 28-gonals but OFFSET=1
231  
232 1 $oeis_anum{'first'}->[30] = 'A254474';
233 1 $oeis_anum{'second'}->[30] = 'A195028';
234 1 $oeis_anum{'average'}->[30] = 'A144555'; # (k-2)/2==14 is 14*squares
235 # OEIS-Catalogue: A254474 polygonal=30
236 # OEIS-Catalogue: A195028 polygonal=30 pairs=second
237 # OEIS-Catalogue: A144555 polygonal=30 pairs=average
238  
239 1 $oeis_anum{'second'}->[18] = 'A139278';
240 1 $oeis_anum{'average'}->[18] = 'A139098'; # (k-2)/2==8 is 8*squares
241 # OEIS-Catalogue: A139278 polygonal=18 pairs=second
242 # OEIS-Catalogue: A139098 polygonal=18 pairs=average
243  
244 1 $oeis_anum{'average'}->[20] = 'A016766'; # (k-2)/2==9 is 9*squares
245 # OEIS-Catalogue: A016766 polygonal=20 pairs=average
246  
247 1 $oeis_anum{'average'}->[22] = 'A033583'; # (k-2)/2==10 is 10*squares
248 # OEIS-Catalogue: A033583 polygonal=22 pairs=average
249  
250 1 $oeis_anum{'average'}->[24] = 'A033584'; # (k-2)/2==11 is 11*squares
251 # OEIS-Catalogue: A033584 polygonal=24 pairs=average
252  
253 1 $oeis_anum{'average'}->[26] = 'A135453'; # (k-2)/2==12 is 12*squares
254 # OEIS-Catalogue: A135453 polygonal=26 pairs=average
255  
256 1 $oeis_anum{'average'}->[28] = 'A152742'; # (k-2)/2==13 is 13*squares
257 # OEIS-Catalogue: A152742 polygonal=28 pairs=average
258  
259 1 $oeis_anum{'average'}->[32] = 'A064761'; # (k-2)/2==15 is 15*squares
260 # OEIS-Catalogue: A064761 polygonal=32 pairs=average
261  
262 1 $oeis_anum{'first'}->[33] = 'A098923'; # 33
263 # OEIS-Catalogue: A098923 polygonal=33
264  
265 1 $oeis_anum{'first'}->[34] = 'A282854'; # 34
266 1 $oeis_anum{'average'}->[34] = 'A016802'; # (k-2)/2==16 is 16*squares
267 # OEIS-Catalogue: A282854 polygonal=34
268 # OEIS-Catalogue: A016802 polygonal=34 pairs=average
269  
270 1 $oeis_anum{'first'}->[35] = 'A282851'; # 35
271 # OEIS-Catalogue: A282851 polygonal=35
272  
273 1 $oeis_anum{'first'}->[36] = 'A282853'; # 36
274 1 $oeis_anum{'first'}->[37] = 'A282852'; # 37
275 1 $oeis_anum{'first'}->[38] = 'A282850'; # 38
276 1 $oeis_anum{'first'}->[40] = 'A261191'; # 40
277 # OEIS-Catalogue: A282853 polygonal=36
278 # OEIS-Catalogue: A282852 polygonal=37
279 # OEIS-Catalogue: A282850 polygonal=38
280 # OEIS-Catalogue: A261191 polygonal=40
281  
282 # ENHANCE-ME: let i_start=1 get the right oeis_anum()
283 # $oeis_anum{'first'}->[45] = 'A098924'; # 45
284 # NOT Catalogue: A098924 polygonal=45 i_start=1
285 # $oeis_anum{'first'}->[47] = 'A095311'; # 47
286 # NOT Catalogue: A095311 polygonal=47 i_start=1
287  
288 1 $oeis_anum{'first'}->[50] = 'A261343'; # 50
289 # OEIS-Catalogue: A261343 polygonal=50
290  
291 1 $oeis_anum{'first'}->[60] = 'A249911'; # 60
292 1 $oeis_anum{'first'}->[63] = 'A098140'; # 63
293 # OEIS-Catalogue: A249911 polygonal=60
294 # OEIS-Catalogue: A098140 polygonal=63
295  
296 1 $oeis_anum{'first'}->[75] = 'A098230'; # 75
297 # OEIS-Catalogue: A098230 polygonal=75
298  
299 1 $oeis_anum{'first'}->[100] = 'A261276'; # 100
300 # OEIS-Catalogue: A261276 polygonal=100
301  
302 1 $oeis_anum{'average'}->[290] = 'A017522'; # (k-2)/2==290 is 144*squares (12n)^2
303 # OEIS-Catalogue: A017522 polygonal=290 pairs=average
304  
305  
306 15 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
307 15 my ($self) = @_;
308 15 return $oeis_anum{$self->{'pairs'}}->[$self->{'k'}];
309 }
310  
311 #------------------------------------------------------------------------------
312  
313 # ($k-2)*$i*($i+1)/2 - ($k-3)*$i
314 # = ($k-2)/2*$i*i + ($k-2)/2*$i - ($k-3)*$i
315 # = ($k-2)/2*$i*i + ($k - 2 - 2*$k + 6)/2*$i
316 # = ($k-2)/2*$i*i + (-$k + 4)/2*$i
317 # = 0.5 * (($k-2)*$i*i + (-$k +4)*$i)
318 # = 0.5 * $i * (($k-2)*$i - $k + 4)
319  
320 # 25*i*(i+1)/2 - 24i
321 # 25*i*(i+1)/2 - 48i/2
322 # i/2*(25*(i+1) - 48)
323 # i/2*(25*i + 25 - 48)
324 # i/2*(25*i - 23)
325 #
326 # P(i) = (k-2)/2 * i*(i+1) - (k-3)*i
327 # S(i) = (k-2)/2 * i*(i-1) + (k-3)*i
328 # P(i)-S(i)
329 # = (k-2)/2 * i*(i+1) - (k-3)*i - [ (k-2)/2 * i*(i-1) + (k-3)*i ]
330 # = (k-2)/2 * [ i*(i+1) - i*(i-1) ] - (k-3)*i - (k-3)*i
331 # = (k-2)/2 * [ i*i+i - (i*i-i) ] - 2*(k-3)*i
332 # = (k-2)/2 * [ i*i+i - i*i + i ] - 2*(k-3)*i
333 # = (k-2)/2 * [ i + i ] - 2*(k-3)*i
334 # = (k-2)/2 * 2*i] - 2*(k-3)*i
335 # = 2*i * [ (k-2)/2 - (k-3) ]
336 # = 2*i * [ (k-2) - (2k-6) ] / 2
337 # = i * [ -k + 4 ] / 2
338 # = i * (4-k) / 2
339 #
340 # average
341 # (P(i) + S(i)) / 2
342 # = [ (k-2)/2 * i*(i+1) - (k-3)*i + (k-2)/2 * i*(i-1) + (k-3)*i ] / 2
343 # = [ (k-2)/2 * i*(i+1) + (k-2)/2 * i*(i-1) ] / 2
344 # = (k-2)/2 * [ i*(i+1) + i*(i-1) ] / 2
345 # = (k-2)/2 * i * [ (i+1) + (i-1) ] / 2
346 # = (k-2)/2 * i * [ 2i ] / 2
347 # = (k-2)/2 * i*i
348  
349 45 ▶sub rewind {
Subroutine rewind: called CC=7 SCAR=19.5
Pod documented
350 45 my ($self) = @_;
351  
352 45 ▶ my $k = $self->{'polygonal'} || 2;
Condition50%1/2
$self->{'polygonal'} || 2
l!l
450
MC/DC0%0/1
$self->{'polygonal'} || 2
$self->{'polygonal'}
Truth table50%1/2
A
A= $self->{'polygonal'}
Aresult
11
00
353 45 my $add = 4 - $k;
354 45 ▶ my $pairs = $self->{'pairs'} || ($self->{'pairs'} = 'first');
Condition50%1/2
$self->{'pairs'} || ($self->{'pairs'} = 'first')
l!l
450
MC/DC0%0/1
$self->{'pairs'} || ($self->{'pairs'} = 'first')
$self->{'pairs'}
Truth table50%1/2
A
A= $self->{'pairs'}
Aresult
11
00
355 45 ▶ if ($k >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 5) 39 6 45
356 39 ▶ if ($pairs eq 'second') {
Branch: 3 branches
BranchTrueFalseTotal
if ($pairs eq 'second') { } 3 36 39
elsif ($pairs eq 'both') { } 3 33 36
elsif ($pairs eq 'average') { } 3 30 33
357 3 $add = - $add;
358 } elsif ($pairs eq 'both') {
359 3 $add = - abs($add);
360 } elsif ($pairs eq 'average') {
361 3 $add = 0;
362 }
363 }
364 45 $self->{'k'} = $k;
365 45 $self->{'add'} = $add;
366  
367 45 $self->SUPER::rewind;
368 }
369  
370 2140 ▶sub ith {
Subroutine ith: called CC=6 SCAR=18.9
Pod documented
371 2140 my ($self, $i) = @_;
372 2140 my $k = $self->{'k'};
373 2140 ▶ if ($k < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 3) 0 2140 2140
374 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
375 0 return 1;
376 } else {
377 0 return undef;
378 }
379 }
380 2140 my $pairs = $self->{'pairs'};
381 2140 ▶ if ($k >= 5 && $pairs eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 5 and $pairs eq "both") 93 2047 2140
Condition100%3/3
$k >= 5 and $pairs eq "both"
!ll&&!rl&&r
109193893
MC/DC100%2/2
$k >= 5 and $pairs eq "both"
$k >= 5 $pairs eq "both"
Truth table100%3/3
A and B
A= $k >= 5B= $pairs eq "both"
ABresult
0X0
100
111
382 93 ▶ if ($i & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 41 52 93
383 41 $i = ($i+1)/2;
384 } else {
385 52 $i = -$i/2;
386 }
387 }
388 ### $i
389 2140 return $i * (($k-2)*$i + $self->{'add'}) / 2;
390 }
391  
392 # k=3 -1/2 + sqrt(2/1 * $n + 1/4)
393 # k=4 sqrt(2/2 * $n )
394 # k=5 1/6 + sqrt(2/3 * $n + 1/36)
395 # k=6 2/8 + sqrt(2/4 * $n + 4/64)
396 # k=7 3/10 + sqrt(2/5 * $n + 9/100)
397 # k=8 4/12 + sqrt(2/6 * $n + 1/9)
398 #
399 # i = 1/(2*(k-2)) * [k-4 + sqrt( 8*(k-2)*n + (4-k)^2 ) ]
400 #
401 # average A(i) = (k-2)/2 * i*i
402 # i*i = A*2/(k-2)
403 # i = sqrt(2A / (k-2))
404 # i = sqrt(2A * (k-2)) / (k-2)
405 # i = sqrt(8A * (k-2)) / (2*(k-2))
406 # which is add==0
407 #
408 7698 ▶sub pred {
Subroutine pred: called CC=4 SCAR=13.9
Pod documented
409 7698 my ($self, $value) = @_;
410 ### Polygonal pred(): $value
411 ### k: $self->{'k'}
412 ### add: $self->{'add'}
413  
414 7698 ▶ if ($value <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 0) 30 7668 7698
415 30 return ($value == 0);
416 }
417  
418 7668 my $k = $self->{'k'};
419 7668 my $add = $self->{'add'};
420 7668 my $sqrt = int(sqrt(int(8*($k-2) * $value + $add*$add)));
421  
422 ### sqrt of: (8*($k-2) * $value + $add*$add)
423  
424 7668 ▶ if ($self->{'pairs'} eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pairs"} eq "both") 60 7608 7668
425 60 my $i = int (($sqrt + $self->{'add'}) / (2*($k-2)));
426 60 ▶ if ($value == $i * (($k-2)*$i - $self->{'add'}) / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == $i * (($k - 2) * $i - $self->{"add"}) / 2) 8 52 60
427 8 return 1;
428 }
429 }
430 7660 my $i = int (($sqrt - $self->{'add'}) / (2*($k-2)));
431  
432 ### $sqrt
433 ### $i
434  
435 7660 return ($value == $i * (($k-2)*$i + $self->{'add'}) / 2);
436 }
437  
438 # P(i) = (k-2)/2 * i*(i+1) - (k-3)*i
439 # P(i) ~= (k-2)/2 * i*i
440 # i ~= sqrt( P(i)*2/(k-2) )
441 #
442 292 ▶sub value_to_i_estimate {
Subroutine value_to_i_estimate: called CC=2 SCAR=6.9
Pod documented
443 292 my ($self, $value) = @_;
444 292 ▶ if ($value < 0) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 105 187 292
445 187 return int(sqrt(int($value)*2/($self->{'k'}-2)));
446 }
447  
448 1 1;
449 __END__
« NumSeq/AllDigits.pm ↑ Summary NumSeq/Factorials.pm »