NumSeq/Polygonal.pm

filter: Statementstmt 97.9% 142 / 145 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.2% 200 / 210 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 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 = 74;
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 =>
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 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 sub description {
65 30 ▶ my ($self) = @_;
Subroutine description: called CC=5 SCAR=16.1
Pod documented
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 # OEIS-Catalogue: A211014 polygonal=14 pairs=second
184 # OEIS-Catalogue: A195818 polygonal=14 pairs=both
185  
186 1 $oeis_anum{'both'}->[15] = 'A277082'; # 15-gonal generalized
187 # OEIS-Catalogue: A277082 polygonal=15 pairs=both
188  
189 1 $oeis_anum{'both'}->[16] = 'A274978'; # 16-gonal generalized
190 1 $oeis_anum{'average'}->[16] = 'A033582'; # (k-2)/2==7 is 7*squares
191 # OEIS-Catalogue: A033582 polygonal=16 pairs=average
192  
193 # these in sequence ...
194 1 $oeis_anum{'first'}->[13] = 'A051865'; # 13 tridecagonal
195 1 $oeis_anum{'first'}->[14] = 'A051866'; # 14-gonal
196 1 $oeis_anum{'first'}->[15] = 'A051867'; # 15
197 1 $oeis_anum{'first'}->[16] = 'A051868'; # 16
198 1 $oeis_anum{'first'}->[17] = 'A051869'; # 17
199 1 $oeis_anum{'first'}->[18] = 'A051870'; # 18
200 1 $oeis_anum{'first'}->[19] = 'A051871'; # 19
201 1 $oeis_anum{'first'}->[20] = 'A051872'; # 20
202 1 $oeis_anum{'first'}->[21] = 'A051873'; # 21
203 1 $oeis_anum{'first'}->[22] = 'A051874'; # 22
204 1 $oeis_anum{'first'}->[23] = 'A051875'; # 23
205 1 $oeis_anum{'first'}->[24] = 'A051876'; # 24
206 # OEIS-Catalogue: A051865 polygonal=13
207 # OEIS-Catalogue: A051866 polygonal=14
208 # OEIS-Catalogue: A051867 polygonal=15
209 # OEIS-Catalogue: A051868 polygonal=16
210 # OEIS-Catalogue: A051869 polygonal=17
211 # OEIS-Catalogue: A051870 polygonal=18
212 # OEIS-Catalogue: A051871 polygonal=19
213 # OEIS-Catalogue: A051872 polygonal=20
214 # OEIS-Catalogue: A051873 polygonal=21
215 # OEIS-Catalogue: A051874 polygonal=22
216 # OEIS-Catalogue: A051875 polygonal=23
217 # OEIS-Catalogue: A051876 polygonal=24
218  
219 # A161935 (n+1)*(13*n+1) gives the 28-gonals starting from i=1, ie. the n
220 # has an extra +1
221  
222 1 $oeis_anum{'second'}->[30] = 'A195028';
223 # OEIS-Catalogue: A195028 polygonal=30 pairs=second # 30 second
224  
225 1 $oeis_anum{'average'}->[14] = 'A033581'; # (k-2)/2==6 is 6*squares
226 # OEIS-Catalogue: A033581 polygonal=14 pairs=average
227  
228 1 $oeis_anum{'second'}->[18] = 'A139278';
229 1 $oeis_anum{'average'}->[18] = 'A139098'; # (k-2)/2==8 is 8*squares
230 # OEIS-Catalogue: A139278 polygonal=18 pairs=second
231 # OEIS-Catalogue: A139098 polygonal=18 pairs=average
232  
233 1 $oeis_anum{'average'}->[20] = 'A016766'; # (k-2)/2==9 is 9*squares
234 # OEIS-Catalogue: A016766 polygonal=20 pairs=average
235  
236 1 $oeis_anum{'average'}->[22] = 'A033583'; # (k-2)/2==10 is 10*squares
237 # OEIS-Catalogue: A033583 polygonal=22 pairs=average
238  
239 1 $oeis_anum{'average'}->[24] = 'A033584'; # (k-2)/2==11 is 11*squares
240 # OEIS-Catalogue: A033584 polygonal=24 pairs=average
241  
242 1 $oeis_anum{'average'}->[26] = 'A135453'; # (k-2)/2==12 is 12*squares
243 # OEIS-Catalogue: A135453 polygonal=26 pairs=average
244  
245 1 $oeis_anum{'average'}->[28] = 'A152742'; # (k-2)/2==13 is 13*squares
246 # OEIS-Catalogue: A152742 polygonal=28 pairs=average
247  
248 1 $oeis_anum{'average'}->[30] = 'A144555'; # (k-2)/2==14 is 14*squares
249 # OEIS-Catalogue: A144555 polygonal=30 pairs=average
250  
251 1 $oeis_anum{'average'}->[32] = 'A064761'; # (k-2)/2==15 is 15*squares
252 # OEIS-Catalogue: A064761 polygonal=32 pairs=average
253  
254 1 $oeis_anum{'average'}->[34] = 'A016802'; # (k-2)/2==16 is 16*squares
255 # OEIS-Catalogue: A016802 polygonal=34 pairs=average
256  
257 1 $oeis_anum{'average'}->[290] = 'A017522'; # (k-2)/2==290 is 144*squares (12n)^2
258 # OEIS-Catalogue: A017522 polygonal=290 pairs=average
259  
260  
261 sub oeis_anum {
262 15 ▶ my ($self) = @_;
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
263 15 return $oeis_anum{$self->{'pairs'}}->[$self->{'k'}];
264 }
265  
266 #------------------------------------------------------------------------------
267  
268 # ($k-2)*$i*($i+1)/2 - ($k-3)*$i
269 # = ($k-2)/2*$i*i + ($k-2)/2*$i - ($k-3)*$i
270 # = ($k-2)/2*$i*i + ($k - 2 - 2*$k + 6)/2*$i
271 # = ($k-2)/2*$i*i + (-$k + 4)/2*$i
272 # = 0.5 * (($k-2)*$i*i + (-$k +4)*$i)
273 # = 0.5 * $i * (($k-2)*$i - $k + 4)
274  
275 # 25*i*(i+1)/2 - 24i
276 # 25*i*(i+1)/2 - 48i/2
277 # i/2*(25*(i+1) - 48)
278 # i/2*(25*i + 25 - 48)
279 # i/2*(25*i - 23)
280 #
281 # P(i) = (k-2)/2 * i*(i+1) - (k-3)*i
282 # S(i) = (k-2)/2 * i*(i-1) + (k-3)*i
283 # P(i)-S(i)
284 # = (k-2)/2 * i*(i+1) - (k-3)*i - [ (k-2)/2 * i*(i-1) + (k-3)*i ]
285 # = (k-2)/2 * [ i*(i+1) - i*(i-1) ] - (k-3)*i - (k-3)*i
286 # = (k-2)/2 * [ i*i+i - (i*i-i) ] - 2*(k-3)*i
287 # = (k-2)/2 * [ i*i+i - i*i + i ] - 2*(k-3)*i
288 # = (k-2)/2 * [ i + i ] - 2*(k-3)*i
289 # = (k-2)/2 * 2*i] - 2*(k-3)*i
290 # = 2*i * [ (k-2)/2 - (k-3) ]
291 # = 2*i * [ (k-2) - (2k-6) ] / 2
292 # = i * [ -k + 4 ] / 2
293 # = i * (4-k) / 2
294 #
295 # average
296 # (P(i) + S(i)) / 2
297 # = [ (k-2)/2 * i*(i+1) - (k-3)*i + (k-2)/2 * i*(i-1) + (k-3)*i ] / 2
298 # = [ (k-2)/2 * i*(i+1) + (k-2)/2 * i*(i-1) ] / 2
299 # = (k-2)/2 * [ i*(i+1) + i*(i-1) ] / 2
300 # = (k-2)/2 * i * [ (i+1) + (i-1) ] / 2
301 # = (k-2)/2 * i * [ 2i ] / 2
302 # = (k-2)/2 * i*i
303  
304 sub rewind {
305 45 ▶ my ($self) = @_;
Subroutine rewind: called CC=7 SCAR=19.5
Pod documented
306  
307 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
308 45 my $add = 4 - $k;
309 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
310 45 ▶ if ($k >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 5) 39 6 45
311 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
312 3 $add = - $add;
313 } elsif ($pairs eq 'both') {
314 3 $add = - abs($add);
315 } elsif ($pairs eq 'average') {
316 3 $add = 0;
317 }
318 }
319 45 $self->{'k'} = $k;
320 45 $self->{'add'} = $add;
321  
322 45 $self->SUPER::rewind;
323 }
324  
325 sub ith {
326 2140 ▶ my ($self, $i) = @_;
Subroutine ith: called CC=6 SCAR=18.9
Pod documented
327 2140 my $k = $self->{'k'};
328 2140 ▶ if ($k < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 3) 0 2140 2140
329 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
330 0 return 1;
331 } else {
332 0 return undef;
333 }
334 }
335 2140 my $pairs = $self->{'pairs'};
336 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
337 93 ▶ if ($i & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i & 1) { } 41 52 93
338 41 $i = ($i+1)/2;
339 } else {
340 52 $i = -$i/2;
341 }
342 }
343 ### $i
344 2140 return $i * (($k-2)*$i + $self->{'add'}) / 2;
345 }
346  
347 # k=3 -1/2 + sqrt(2/1 * $n + 1/4)
348 # k=4 sqrt(2/2 * $n )
349 # k=5 1/6 + sqrt(2/3 * $n + 1/36)
350 # k=6 2/8 + sqrt(2/4 * $n + 4/64)
351 # k=7 3/10 + sqrt(2/5 * $n + 9/100)
352 # k=8 4/12 + sqrt(2/6 * $n + 1/9)
353 #
354 # i = 1/(2*(k-2)) * [k-4 + sqrt( 8*(k-2)*n + (4-k)^2 ) ]
355 #
356 # average A(i) = (k-2)/2 * i*i
357 # i*i = A*2/(k-2)
358 # i = sqrt(2A / (k-2))
359 # i = sqrt(2A * (k-2)) / (k-2)
360 # i = sqrt(8A * (k-2)) / (2*(k-2))
361 # which is add==0
362 #
363 sub pred {
364 7698 ▶ my ($self, $value) = @_;
Subroutine pred: called CC=4 SCAR=13.9
Pod documented
365 ### Polygonal pred(): $value
366 ### k: $self->{'k'}
367 ### add: $self->{'add'}
368  
369 7698 ▶ if ($value <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 0) 30 7668 7698
370 30 return ($value == 0);
371 }
372  
373 7668 my $k = $self->{'k'};
374 7668 my $add = $self->{'add'};
375 7668 my $sqrt = int(sqrt(int(8*($k-2) * $value + $add*$add)));
376  
377 ### sqrt of: (8*($k-2) * $value + $add*$add)
378  
379 7668 ▶ if ($self->{'pairs'} eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pairs"} eq "both") 60 7608 7668
380 60 my $i = int (($sqrt + $self->{'add'}) / (2*($k-2)));
381 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
382 8 return 1;
383 }
384 }
385 7660 my $i = int (($sqrt - $self->{'add'}) / (2*($k-2)));
386  
387 ### $sqrt
388 ### $i
389  
390 7660 return ($value == $i * (($k-2)*$i + $self->{'add'}) / 2);
391 }
392  
393 # P(i) = (k-2)/2 * i*(i+1) - (k-3)*i
394 # P(i) ~= (k-2)/2 * i*i
395 # i ~= sqrt( P(i)*2/(k-2) )
396 #
397 sub value_to_i_estimate {
398 292 ▶ my ($self, $value) = @_;
Subroutine value_to_i_estimate: called CC=2 SCAR=6.9
Pod documented
399 292 ▶ if ($value < 0) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 105 187 292
400 187 return int(sqrt(int($value)*2/($self->{'k'}-2)));
401 }
402  
403 1 1;
404 __END__
« NumSeq/AllDigits.pm ↑ Summary NumSeq/Factorials.pm »