NumSeq/Runs.pm

filter: Statementstmt 96.7% 90 / 93 Branchbran 87.5% 28 / 32 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 92.3% 156 / 169 CC 23 SCAR 19.4
CC
23
Cov
93%
ith
14.0
rewind
6.3
pred
3.6
CRAP 7.0
« NumSeq/SpiroFibonacci.pm ↑ Summary NumSeq/RadixWithoutDigit.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2019, 2020 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  
19 # A053615 - 0toNto0
20 # A004738 - 1toNto0
21 # cf A165162 - Nto1,N-1to1 cf A057058 making fracs A165200
22 # A010751 - runs incr then decr, up=1,down=2,up=3,down=4
23 # A055087 - runs 0toNtwice each 0 .. N, 0 .. N,
24 # A055086 - n repeat floor(n/2)+1 times, DiagonalsOctant X+Y
25 # A082375 - k to 0 by 2s
26 # A122196 - count down by 2s
27 # A111650 - 2n repeated n times
28 # A000194 - n repeated 2n times
29 # A111651 - n repeated 3n times
30 # A111652 - 3n repeated n times
31 # A121997 - 1toN repeated N times
32 # A079944 - 2^n 0s then 2^n, is second highest bit of n
33 # A004525 - 1 even then 3 odd, OFFSET=0
34 # A007001 - 1toN repeating 1toN
35 #
36 # A049581 diagonals absdiff, abs(x-y) not plain runs
37 # A061579 descending NtoPrev, permutation of the integers
38 # A076478 0 to 2^k-1, each written out in k many bits
39 # A000267 1,2,3,3,4,4,5,5,5,6,6,6, twice repeat each run length
40 # A122196 down by 2s
41 #
42 # to nearest pronic
43 # pronic(i) = i*(i+1);
44 # pronic_to_i_floor(n) = floor((sqrt(4*n + 1) - 1)/2);
45 # is_pronic(n) = n==pronic(pronic_to_i_floor(n));
46 # next_pronic(n) = if(is_pronic(n),n, pronic(1+pronic_to_i_floor(n)));
47 # prev_pronic(n) = pronic(pronic_to_i_floor(n));
48 # to_pronic(n) = min(next_pronic(n) - n, n - prev_pronic(n));
49 # to_pronic_signed(n) = my(pos=next_pronic(n)-n, neg=prev_pronic(n)-n); if(pos>-neg,neg,pos);
50 # vector(40,n,to_pronic(n))
51 # vector(40,n,to_pronic_signed(n))
52 # to_oddeven(n) = n=to_pronic_signed(n); if(n<=0, -2*n, 2*n-1);
53 # vector(40,n,to_oddeven(n))
54 # vector(40,n,n + to_oddeven(n))
55 # vector(40,n,n + to_pronic_signed(n))
56  
57 package Math::NumSeq::Runs;
58 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 2 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 $VERSION = 75;
64  
65 2 ▶use Math::NumSeq 21; # v.21 for oeis_anum field
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 @ISA = ('Math::NumSeq');
67  
68 # uncomment this to run the ### lines
69 #use Smart::Comments;
70  
71  
72 # use constant name => Math::NumSeq::__('Runs of Integers');
73 2 ▶use constant description => Math::NumSeq::__('Runs of integers of various kinds.');
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 ▶use constant characteristic_increasing => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79  
80 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
81 [
82 {
83 name => 'runs_type',
84 display => Math::NumSeq::__('Runs Type'),
85 type => 'enum',
86 default => '0toN',
87 choices => ['0toN',
88 '0to2N',
89 '1toN',
90 '1to2N',
91 '1toFib',
92 '0toNinc',
93 'Nto0',
94 'Nto1',
95 'Nrep',
96 'N+1rep',
97 '2rep',
98 '3rep',
99 ],
100 choices_display => [Math::NumSeq::__('0toN'),
101 Math::NumSeq::__('0to2N'),
102 Math::NumSeq::__('1toN'),
103 Math::NumSeq::__('1to2N'),
104 Math::NumSeq::__('1toFib'),
105 Math::NumSeq::__('0toNinc'),
106 Math::NumSeq::__('Nto0'),
107 Math::NumSeq::__('Nto1'),
108 Math::NumSeq::__('Nrep'),
109 Math::NumSeq::__('N+1rep'),
110 Math::NumSeq::__('2rep'),
111 Math::NumSeq::__('3rep'),
112 ],
113 # description => Math::NumSeq::__(''),
114 },
115 2 ];
116  
117 my %runs_type_data
118 = ('0toN' => { i_start => 0,
119 value => -1, # initial
120 values_min => 0,
121 vstart => 0,
122 vstart_inc => 0,
123 value_inc => 1,
124 c => 1, # initial
125 count => 0,
126 count_inc => 1,
127 oeis_anum => 'A002262',
128 # OEIS-Catalogue: A002262 runs_type=0toN
129 },
130  
131 '1toN' => { i_start => 1,
132 value => 0, # initial
133 values_min => 1,
134 vstart => 1,
135 vstart_inc => 0,
136 value_inc => 1,
137 c => 1, # initial
138 count => 0,
139 count_inc => 1,
140 oeis_anum => 'A002260', # 1 to N, is 0toN + 1
141 # OEIS-Catalogue: A002260 runs_type=1toN
142 },
143 '0to2N' => { i_start => 0,
144 value => -1, # initial
145 values_min => 0,
146 vstart => 0,
147 vstart_inc => 0,
148 value_inc => 1,
149 c => 1, # initial
150 count => 0,
151 count_inc => 2,
152 oeis_anum => 'A053186',
153 # OEIS-Catalogue: A053186 runs_type=0to2N
154 },
155 '1to2N' => { i_start => 1,
156 value => 0, # initial
157 values_min => 1,
158 vstart => 1,
159 vstart_inc => 0,
160 value_inc => 1,
161 c => 2, # initial
162 count => 1,
163 count_inc => 2,
164 oeis_anum => 'A074294',
165 # OEIS-Catalogue: A074294 runs_type=1to2N
166 },
167 '1to2N+1' => { i_start => 1,
168 value => 0, # initial
169 values_min => 1,
170 vstart => 1,
171 vstart_inc => 0,
172 value_inc => 1,
173 c => 1, # initial
174 count => 0,
175 count_inc => 2,
176 oeis_anum => 'A071797', # "fractal" of odds
177 # OEIS-Catalogue: A071797 runs_type=1to2N+1
178 },
179 '1toFib' => { i_start => 1,
180 value => 0, # initial
181 values_min => 1,
182 vstart => 1,
183 vstart_inc => 0,
184 value_inc => 1,
185 c => 1, # initial
186 count => 0,
187 8 ▶ count_inc_func => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
188 8 my ($self) = @_;
189 (my $ret, $self->{'f0'}, $self->{'f1'})
190 = ($self->{'f0'},
191 $self->{'f1'},
192 8 $self->{'f0'}+$self->{'f1'});
193 8 return $ret-1;
194 },
195 2 f0 => 1,
196 f1 => 2,
197 oeis_anum => 'A194029', # 1 to Fibonacci(N)
198 # OEIS-Catalogue: A194029 runs_type=1toFib
199 },
200 'Nto0' => { i_start => 0,
201 value => 1, # initial
202 values_min => 0,
203 vstart => 0,
204 vstart_inc => 1,
205 value_inc => -1,
206 c => 1, # initial
207 count => 0,
208 count_inc => 1,
209 oeis_anum => 'A025581',
210 # OEIS-Catalogue: A025581 runs_type=Nto0
211 },
212 'Nto1' => { i_start => 1,
213 value => 2, # initial
214 values_min => 1,
215 vstart => 1,
216 vstart_inc => 1,
217 value_inc => -1,
218 c => 1, # initial
219 count => 0,
220 count_inc => 1,
221 oeis_anum => 'A004736',
222 # OEIS-Catalogue: A004736 runs_type=Nto1
223 },
224 'Nrep' => { i_start => 1,
225 value => 1,
226 values_min => 1,
227 value_inc => 0,
228 vstart => 1,
229 vstart_inc => 1,
230 c => 1, # initial
231 count => 0,
232 count_inc => 1,
233 oeis_anum => 'A002024', # N appears N times
234 # OEIS-Catalogue: A002024 runs_type=Nrep
235 },
236 'N+1rep' => { i_start => 0,
237 value => 0,
238 values_min => 0,
239 value_inc => 0,
240 vstart => 0,
241 vstart_inc => 1,
242 c => 1, # initial
243 count => 0,
244 count_inc => 1,
245 oeis_anum => 'A003056', # N appears N+1 times
246 # OEIS-Catalogue: A003056 runs_type=N+1rep
247 },
248 '0toNinc' => { i_start => 0,
249 value => -1,
250 values_min => 0,
251 value_inc => 1,
252 vstart => 0,
253 vstart_inc => 1,
254 c => 1, # initial
255 count => 0,
256 count_inc => 1,
257 oeis_anum => 'A051162',
258 # OEIS-Catalogue: A051162 runs_type=0toNinc
259 },
260 );
261 # {
262 # my @a = keys %runs_type_data;
263 # ### assert: scalar(@{parameter_info_array()->[0]->{'choices'}}) == scalar(@a)
264 # }
265  
266 my @rep_oeis_anum
267 2 = ([ undef, # 0rep, nothing
268  
269 'A001477', # 1rep, integers 0 upwards
270 # OEIS-Other: A001477 runs_type=1rep
271  
272 'A004526', # 2rep, N appears 2 times, starting from 0
273 # OEIS-Catalogue: A004526 runs_type=2rep
274  
275 'A002264', # 3rep, N appears 3 times
276 # OEIS-Catalogue: A002264 runs_type=3rep
277  
278 'A002265', # 4rep
279 # OEIS-Catalogue: A002265 runs_type=4rep
280  
281 'A002266', # 5rep
282 # OEIS-Catalogue: A002266 runs_type=5rep
283  
284 'A152467', # 6rep
285 # OEIS-Catalogue: A152467 runs_type=6rep
286  
287 # no, A132270 has OFFSET=1 (with 7 initial 0s)
288 # 'A132270', # 7rep
289 # # OEIS-Catalogue: A132270 runs_type=7rep
290  
291 # no, A132292 has OFFSET=1 (with 8 initial 0s)
292 # 'A132292', # 8rep
293 # # OEIS-Catalogue: A132292 runs_type=8rep
294  
295 ],
296  
297 # starting i=1
298 [ undef, # 0rep, nothing
299  
300 'A000027', # 1rep, integers 1 upwards
301 # OEIS-Other: A000027 runs_type=1rep i_start=1
302  
303 # Not quite, A008619 starts OFFSET=0 value=1,1,2,2,
304 # 'A008619', # 2rep, N appears 2 times, starting from 0
305 # # OEIS-Catalogue: A008619 runs_type=2rep i_start=1
306 ] );
307  
308 46 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=18.4
Pod documented
309 46 my ($self) = @_;
310 46 ▶ $self->{'runs_type'} ||= '0toN';
Condition50%1/2
$self->{"runs_type"} ||= "0toN"
l!l
460
MC/DC0%0/1
$self->{"runs_type"} ||= "0toN"
$self->{"runs_type"}
Truth table50%1/2
A
A= $self->{"runs_type"}
Aresult
11
00
311  
312 46 my $data;
313 46 ▶ if ($self->{'runs_type'} =~ /^(\d+)rep/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'runs_type'} =~ /^(\d+)rep/) { } 12 34 46
314 12 my $rep = $1;
315 12 ▶ my $i_start = ($self->{'i_start'} || 0);
Condition50%1/2
$self->{'i_start'} || 0
l!l
012
MC/DC0%0/1
$self->{'i_start'} || 0
$self->{'i_start'}
Truth table50%1/2
A
A= $self->{'i_start'}
Aresult
11
00
316 12 ▶ $data = { i_start => $i_start,
Branch: 1 branch
BranchTrueFalseTotal
$i_start >= 0 ? : 12 0 12
317 value => $i_start,
318 values_min => $i_start,
319 value_inc => 0,
320 vstart => $i_start,
321 vstart_inc => 1,
322 c => $rep, # initial
323 count => $rep-1,
324 count_inc => 0,
325 oeis_anum => ($i_start >= 0
326 ? $rep_oeis_anum[$i_start][$rep]
327 : undef),
328 };
329 } else {
330 $data = $runs_type_data{$self->{'runs_type'}}
331 34 ▶ || croak "Unrecognised runs_type: ", $self->{'runs_type'};
Condition50%1/2
$runs_type_data{$self->{'runs_type'}} || croak('Unrecognised runs_type: ', $self->{'runs_type'})
l!l
340
MC/DC0%0/1
$runs_type_data{$self->{'runs_type'}} || croak('Unrecognised runs_type: ', $self->{'runs_type'})
$runs_type_data{$self->{'runs_type'}}
Truth table50%1/2
A
A= $runs_type_data{$self->{'runs_type'}}
Aresult
11
00
332 }
333 46 %$self = (%$self, %$data);
334 46 $self->{'i'} = $self->i_start;
335 }
336  
337 342 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
338 342 my ($self) = @_;
339 342 my $i = $self->{'i'}++;
340 342 ▶ if (--$self->{'c'} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{'c'} >= 0) { } 248 94 342
341 return ($i,
342 248 ($self->{'value'} += $self->{'value_inc'}));
343 } else {
344 94 ▶ if (my $func = $self->{'count_inc_func'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $self->{'count_inc_func'}) { } 8 86 94
345 8 $self->{'c'} = &$func($self);
346 } else {
347 86 $self->{'c'} = ($self->{'count'} += $self->{'count_inc'});
348 }
349 return ($i,
350 94 ($self->{'value'} = ($self->{'vstart'} += $self->{'vstart_inc'})));
351 }
352 }
353  
354 558 ▶sub ith {
Subroutine ith: called CC=14 SCAR=26.4
Pod documented
355 558 my ($self, $i) = @_;
356 ### Runs ith(): $i
357  
358 558 my $i_start = $self->{'i_start'};
359 558 ▶ if ($i < $i_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $i_start) 51 507 558
360 51 return undef;
361 }
362  
363 507 ▶ if ($self->{'runs_type'} eq 'Nto0' || $self->{'runs_type'} eq 'Nto1') {
Branch: 7 branches
BranchTrueFalseTotal
if ($self->{'runs_type'} eq 'Nto0' or $self->{'runs_type'} eq 'Nto1') { } 47 460 507
elsif ($self->{'runs_type'} eq 'Nrep' or $self->{'runs_type'} eq 'N+1rep') { } 59 401 460
elsif ($self->{'runs_type'} eq '0toNinc') { } 30 371 401
elsif ($self->{'runs_type'} =~ /^(\d+)rep/) { } 129 242 371
elsif ($self->{'runs_type'} eq '1to2N') { } 35 207 242
elsif ($self->{'runs_type'} eq '0to2N' or $self->{'runs_type'} eq '1to2N+1') { } 125 82 207
elsif ($self->{'runs_type'} eq '1toFib') { } 35 47 82
Condition100%3/3
$self->{"runs_type"} eq "Nto0" or $self->{"runs_type"} eq "Nto1"
l!l&&r!l&&!r
3017460
MC/DC100%2/2
$self->{"runs_type"} eq "Nto0" or $self->{"runs_type"} eq "Nto1"
$self->{"runs_type"} eq "Nto0" $self->{"runs_type"} eq "Nto1"
Truth table100%3/3
A or B
A= $self->{"runs_type"} eq "Nto0"B= $self->{"runs_type"} eq "Nto1"
ABresult
1X1
011
000
364 # d-(i-(d-1)*d/2)
365 # = d-i+(d-1)*d/2
366 # = d*(1+(d-1)/2) - i
367 # = d*((d+1)/2) - i
368 # = (d+1)d/2 - i
369  
370 47 $i -= $self->{'i_start'};
371 47 my $d = int((sqrt(8*$i+1) + 1) / 2);
372 ### $d
373 ### base: ($d-1)*$d/2
374 ### rem: $i - ($d-1)*$d/2
375 47 return -$i + ($d+1)*$d/2 - 1 + $self->{'values_min'};
376  
377 ▶ } elsif ($self->{'runs_type'} eq 'Nrep'
Condition100%3/3
$self->{"runs_type"} eq "Nrep" or $self->{"runs_type"} eq "N+1rep"
l!l&&r!l&&!r
2930401
MC/DC100%2/2
$self->{"runs_type"} eq "Nrep" or $self->{"runs_type"} eq "N+1rep"
$self->{"runs_type"} eq "Nrep" $self->{"runs_type"} eq "N+1rep"
Truth table100%3/3
A or B
A= $self->{"runs_type"} eq "Nrep"B= $self->{"runs_type"} eq "N+1rep"
ABresult
1X1
011
000
378 || $self->{'runs_type'} eq 'N+1rep') {
379 59 $i -= $self->{'i_start'};
380 59 return int((sqrt(8*$i+1) - 1) / 2) + $self->{'values_min'};
381  
382 } elsif ($self->{'runs_type'} eq '0toNinc') {
383 # i-(d-1)d/2 + d
384 # = i-((d-1)d/2 - d)
385 # = i-(d-3)d/2
386 30 my $d = int((sqrt(8*$i+1) + 1) / 2);
387 30 return $i - ($d-3)*$d/2 - 1;
388  
389 } elsif ($self->{'runs_type'} =~ /^(\d+)rep/) {
390 129 my $rep = $1;
391 ### $rep
392 129 ▶ if ($rep < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rep < 1) 0 129 129
393 0 return undef;
394 }
395 129 return int($i/$rep);
396  
397 } elsif ($self->{'runs_type'} eq '1to2N') {
398 # runs beginning i=1,3,7,13,21,31 (Math::NumSeq::Pronic + 1)
399 # N = (d^2 - d + 1)
400 # = ($d**2 - $d + 1)
401 # = (($d - 1)*$d + 1)
402 # d = 1/2 + sqrt(1 * $n + -3/4)
403 # = (1 + sqrt(4*$n - 3)) / 2
404  
405 35 my $d = int( (sqrt(4*$i-3) + 1) / 2);
406  
407 ### $d
408 ### base: ($d-1)*$d/2
409 ### rem: $i - ($d-1)*$d/2
410  
411 35 return $i - ($d-1)*$d;
412  
413 ▶ } elsif ($self->{'runs_type'} eq '0to2N'
Condition100%3/3
$self->{"runs_type"} eq "0to2N" or $self->{"runs_type"} eq "1to2N+1"
l!l&&r!l&&!r
784782
MC/DC100%2/2
$self->{"runs_type"} eq "0to2N" or $self->{"runs_type"} eq "1to2N+1"
$self->{"runs_type"} eq "0to2N" $self->{"runs_type"} eq "1to2N+1"
Truth table100%3/3
A or B
A= $self->{"runs_type"} eq "0to2N"B= $self->{"runs_type"} eq "1to2N+1"
ABresult
1X1
011
000
414 || $self->{'runs_type'} eq '1to2N+1') {
415 ### 1to2N+1 ...
416 # values 1, 1,2,3, 1,2,3,4,5
417 # run beginning i=1,2,5,10,17,26
418 # N = (d^2 - 2 d + 2), starting d=1
419 # d = 1 + sqrt(1 * $n + -1)
420 # = 1 + sqrt($n-1)
421  
422 125 $i -= $self->{'i_start'};
423 125 my $d = int(sqrt($i)) + 1;
424  
425 ### $d
426 ### base: ($d-2)*$d
427 ### rem: $i - ($d-2)*$d
428  
429 125 return $i - ($d-2)*$d + $self->{'vstart'}-1;
430  
431 } elsif ($self->{'runs_type'} eq '1toFib') {
432 35 my $f0 = ($i*0) + 1; # inherit bignum 1
433 35 my $f1 = $f0 + 1; # inherit bignum 2
434 35 ▶ while ($f1 <= $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 <= $i) 107 35 142
435 107 ($f0,$f1) = ($f1,$f0+$f1);
436 }
437 35 return $i - $f0 + 1;
438  
439 } else { # 0toN, 1toN
440 47 $i -= $i_start;
441 47 my $d = int((sqrt(8*$i+1) + 1) / 2);
442  
443 ### $d
444 ### base: ($d-1)*$d/2
445 ### rem: $i - ($d-1)*$d/2
446  
447 47 return $i - ($d-1)*$d/2 + $self->{'vstart'};
448 }
449 }
450  
451 171 ▶sub pred {
Subroutine pred: called CC=3 SCAR=12.7
Pod documented
452 171 my ($self, $value) = @_;
453 ### Runs pred(): $value
454  
455 171 ▶ unless ($value == int($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value == int $value) 0 171 171
456 0 return 0;
457 }
458 171 ▶ if (defined $self->{'values_min'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'values_min'}) { } 171 0 171
459 171 return ($value >= $self->{'values_min'});
460 } else {
461 0 return ($value <= $self->{'values_max'});
462 }
463 }
464  
465 2 1;
466 __END__
« NumSeq/SpiroFibonacci.pm ↑ Summary NumSeq/RadixWithoutDigit.pm »