NumSeq/UndulatingNumbers.pm

filter: Statementstmt 97.9% 141 / 144 Branchbran 93.7% 60 / 64 Conditioncond 91.3% 21 / 23 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 95.5% 258 / 270 CC 41 SCAR 21.6
CC
41
Cov
96%
value_to_i_floor
15.1
ith
11.0
pred
10.0
CRAP 8.7
« NumSeq/BalancedBinary.pm ↑ Summary NumSeq/LemoineCount.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 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 # http://mathworld.wolfram.com/UndulatingNumber.html
20  
21 package Math::NumSeq::UndulatingNumbers;
22 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 $VERSION = 75;
27 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 @ISA = ('Math::NumSeq');
29 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34  
35 # use constant name => Math::NumSeq::__('Undulating Numbers');
36 2 ▶use constant description => Math::NumSeq::__('Numbers like 37373 which are a pattern of digits ABAB...');
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 2 ▶use Math::NumSeq::Base::Digits;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
44 [
45 Math::NumSeq::Base::Digits::parameter_common_radix(),
46 { name => 'including_repdigits',
47 type => 'boolean',
48 display => Math::NumSeq::__('Repdigits'),
49 default => 1,
50 description => Math::NumSeq::__('Whether to include repdigits A=B.'),
51 },
52 2 ];
53  
54 #------------------------------------------------------------------------------
55  
56 # cf A046075 - decimal A!=B and min length 3
57 # A033619 - decimal A=B any length
58 #
59 # A046076 - "binary undulants", numbers k where 2^k written in decimal
60 # has an 010 or 101 somewhere
61 #
62 2 my @oeis_anum;
63  
64 2 $oeis_anum[1]->[2] = 'A000975'; # binary A!=B no consecutive equal bits
65 # OEIS-Catalogue: A000975 radix=2 including_repdigits=0
66  
67 # $oeis_anum[0]->[10] = 'A033619'; # decimal incl A=B, but OFFSET=1 value=0
68 # # OEIS-Catalogue: A033619 including_repdigits=1
69  
70 4 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
71 4 my ($self) = @_;
72 4 return $oeis_anum[!$self->{'including_repdigits'}]->[$self->{'radix'}];
73 }
74  
75 #------------------------------------------------------------------------------
76  
77 42 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=7.0
Pod documented
78 42 my ($self) = @_;
79 42 my $radix = $self->{'radix'};
80 0 ▶ if ($radix < 2) { $radix = 10; }
Branch: 1 branch
BranchTrueFalseTotal
if ($radix < 2) 0 42 42
81 42 $self->{'radix'} = $radix;
82  
83 42 $self->{'i'} = 0;
84 42 $self->{'value'} = -1;
85 42 $self->{'inc'} = 1; # 10101010 or 101010101
86 42 $self->{'inc_even'} = 0; # 1 if inc=10101010 or 0 if inc=101010101
87 42 $self->{'a'} = 0; # 1 to 9, or 0 for initial smalls
88 42 $self->{'b'} = 0; # 0 to 9, perhaps skipping a==b
89 }
90  
91 4927 ▶sub next {
Subroutine next: called CC=6 SCAR=17.9
Pod documented
92 4927 my ($self) = @_;
93 ### UndulatingNumbers next() ...
94 ### inc: $self->{'inc'}
95 ### value: $self->{'value'}
96  
97 4927 my $radix = $self->{'radix'};
98 4927 my $value;
99 4927 ▶ if ($value = ($self->{'value'} += $self->{'inc'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value = $self->{"value"} += $self->{"inc"}) 4889 38 4927
100 4889 $self->{'b'}++;
101 ### value: $self->{'value'}
102 ### a: $self->{'a'}
103 ### b: $self->{'b'}
104  
105 4889 ▶ if (! $self->{'including_repdigits'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"including_repdigits"} and $self->{"b"} == $self->{"a"}) 293 4596 4889
Condition100%3/3
not $self->{"including_repdigits"} and $self->{"b"} == $self->{"a"}
!ll&&!rl&&r
23752221293
MC/DC100%2/2
not $self->{"including_repdigits"} and $self->{"b"} == $self->{"a"}
not $self->{"including_repdigits"} $self->{"b"} == $self->{"a"}
Truth table100%3/3
not(A) and B
A= not $self->{"including_repdigits"}B= $self->{"b"} == $self->{"a"}
ABresult
0X0
100
111
106 && $self->{'b'} == $self->{'a'}) {
107 293 $self->{'value'} = ($value += $self->{'inc'});
108 293 $self->{'b'}++;
109  
110 ### no repdigits, skip a==b to new b: $self->{'b'}
111 ### value now: $value
112 }
113  
114 4889 ▶ if ($self->{'b'} >= $radix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"b"} >= $radix) 552 4337 4889
115 ### b overflow, a inc ...
116 ### inc_even: $self->{'inc_even'}
117  
118 # a9a + 10 -> (a+1)0(a)
119 # if inc=10..10 ends in 0 then need 1 extra to give (a+1)0(a+1)
120  
121 552 $self->{'b'} = 0;
122 552 $self->{'value'} = ($value += $self->{'inc_even'});
123 ### value now: $value
124  
125 552 ▶ if (++$self->{'a'} >= $radix) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"a"} >= $radix) 102 450 552
126 ### a overflow, next length ...
127  
128 # inc=101 -> 1010 inc_even=0
129 # or 1010 -> 10101 inc_even=0
130 102 my $inc_even = ($self->{'inc_even'} ^= 1);
131 102 $self->{'inc'} = $self->{'inc'} * $radix + !$inc_even;
132 102 $self->{'a'} = 1;
133 102 $self->{'value'} = ($value += $inc_even);
134  
135 ### lengthen to inc: $self->{'inc'}
136 ### n now: $value
137 }
138 }
139 }
140 4927 return ($self->{'i'}++, $value);
141 }
142  
143 # A is 0 to 9 = 10 values
144 # AB is 10 to 99 = 90 values
145 # total R*R
146 # then high AB is 10 to 99 = 90 values
147 # total 90 = (R-1)*R
148 # R=2 total 2*2-1=3 1,10,11
149 #
150 # or without repdigits
151 # AB skips 11, ..., 99 = 10 values
152 # total R*R - R = R*(R-1)
153 #
154 5146 ▶sub ith {
Subroutine ith: called CC=11 SCAR=24.0
Pod documented
155 5146 my ($self, $i) = @_;
156 ### UndulatingNumbers ith(): $i
157 5146 my $radix = $self->{'radix'};
158 5146 my $rdec = $radix - 1;
159  
160 5146 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) 12 5134 5146
161 12 return undef;
162 }
163  
164 5134 my $including_repdigits = $self->{'including_repdigits'};
165  
166 5134 ▶ my $pair_step = ($including_repdigits ? $radix*$radix : $radix*$rdec+1);
Branch: 1 branch
BranchTrueFalseTotal
$including_repdigits ? : 2404 2730 5134
167 ### small pair_step: $pair_step
168  
169 5134 ▶ if ($i < $pair_step || _is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pair_step or _is_infinite($i)) 3200 1934 5134
Condition66%2/3
$i < $pair_step or _is_infinite($i)
l!l&&r!l&&!r
320001934
MC/DC50%1/2
$i < $pair_step or _is_infinite($i)
$i < $pair_step _is_infinite($i)
Truth table66%2/3
A or B
A= $i < $pair_stepB= _is_infinite($i)
ABresult
1X1
011
000
170 ### below small pairs ...
171  
172 # at i=11 skip to value=12
173 # i=19 value=20
174 # i=20 value=21
175 # i=21 skip to value=23
176 #
177 3200 ▶ if (! $including_repdigits && $i > $radix) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $including_repdigits and $i > $radix) 1474 1726 3200
Condition100%3/3
not $including_repdigits and $i > $radix
!ll&&!rl&&r
15222041474
MC/DC100%2/2
not $including_repdigits and $i > $radix
not $including_repdigits $i > $radix
Truth table100%3/3
not(A) and B
A= not $including_repdigitsB= $i > $radix
ABresult
0X0
100
111
178 1474 $i += int(($i-1)/$radix); # skip 11,22,33 etc
179 }
180 3200 return $i;
181 }
182  
183 1934 $i -= $pair_step;
184 ### i remainder: $i
185  
186 1934 ▶ $pair_step = $rdec*($including_repdigits ? $radix : $rdec);
Branch: 1 branch
BranchTrueFalseTotal
$including_repdigits ? : 882 1052 1934
187 ### decreased pair_step: $pair_step
188  
189 1934 my $i_pair = $i % $pair_step;
190 1934 my $i_len = int($i/$pair_step);
191 ### $i_pair
192 ### $i_len
193  
194 1934 my ($a, $b);
195 1934 ▶ if ($including_repdigits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($including_repdigits) { } 882 1052 1934
196 882 $a = int($i_pair/$radix) + 1;
197 882 $b = $i_pair % $radix;
198 } else {
199 1052 $a = int($i_pair/$rdec) + 1;
200 1052 $b = $i_pair % $rdec;
201 1052 $b += ($b >= $a);
202 }
203 ### $a
204 ### $b
205  
206 1934 my $ret = $a*$radix + $b;
207 ### $ret
208 1934 ▶ while ($i_len-- >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_len-- >= 0) 2134 245 2379
209 2134 $ret = ($ret * $radix) + $a;
210 ### append A to: $ret
211  
212 2134 ▶ last unless $i_len-- >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i_len-- >= 0 1689 445 2134
213 445 $ret = ($ret * $radix) + $b;
214 ### append B to: $ret
215 }
216  
217 ### $ret
218 1934 return $ret;
219 }
220  
221 33839 ▶sub pred {
Subroutine pred: called CC=10 SCAR=23.0
Pod documented
222 33839 my ($self, $value) = @_;
223  
224 33839 ▶ if (_is_infinite($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 33839 33839
225 0 return undef;
226 }
227 33839 ▶ if ($value != int($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != int $value) 1784 32055 33839
228 1784 return 0;
229 }
230  
231 32055 my $radix = $self->{'radix'};
232 32055 my $a = $value % $radix;
233 32055 ▶ if ($value = int($value/$radix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value = int $value / $radix) 31749 306 32055
234 31749 my $b = $value % $radix;
235 31749 ▶ if (! $self->{'including_repdigits'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"including_repdigits"} and $a == $b) 2708 29041 31749
Condition100%3/3
not $self->{"including_repdigits"} and $a == $b
!ll&&!rl&&r
15144138972708
MC/DC100%2/2
not $self->{"including_repdigits"} and $a == $b
not $self->{"including_repdigits"} $a == $b
Truth table100%3/3
not(A) and B
A= not $self->{"including_repdigits"}B= $a == $b
ABresult
0X0
100
111
236 && $a == $b) {
237 2708 return 0;
238 }
239  
240 29041 ▶ while ($value = int($value/$radix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value = int $value / $radix) 27111 2996 30107
241 27111 ▶ if (($value % $radix) != $a) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value % $radix != $a) 22389 4722 27111
242  
243 4722 ▶ $value = int($value/$radix) || last;
Condition100%2/2
int $value / $radix || (last)
l!l
31361586
MC/DC100%1/1
int $value / $radix || (last)
int $value / $radix
Truth table100%2/2
A
A= int $value / $radix
Aresult
11
00
244 3136 ▶ if (($value % $radix) != $b) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value % $radix != $b) 2070 1066 3136
245 }
246 }
247 4888 return 1;
248 }
249  
250 31349 ▶sub value_to_i_floor {
Subroutine value_to_i_floor: called CC=15 SCAR=27.1
Pod documented
251 31349 my ($self, $value) = @_;
252 ### value_to_i_floor(): $value
253  
254 31349 ▶ if ($value <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 0) 90 31259 31349
255 90 return 0;
256 }
257 31259 ▶ if (_is_infinite($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 31259 31259
258 0 return $value;
259 }
260  
261 31259 my $radix = $self->{'radix'};
262 31259 my @digits = _digit_split(int($value), $radix);
263  
264 31259 my $a = pop @digits;
265 31259 ▶ @digits or return $a; # one digit only
Branch: 1 branch
BranchTrueFalseTotal
@digits or return $a 30969 290 31259
266  
267 30969 my $including_repdigits = $self->{'including_repdigits'};
268 30969 my $rdec = $radix - 1;
269  
270 30969 my $b = pop @digits;
271 ### $a
272 ### $b
273 ### $including_repdigits
274 ### digit count: scalar(@digits)
275 ### $radix
276 ### $rdec
277  
278 30969 ▶ my $i = $a * ($including_repdigits ? $radix : $rdec) + $b
Branch: 3 branches
BranchTrueFalseTotal
$including_repdigits ? : 15026 15943 30969
$including_repdigits || $a < $b ? : 22179 8790 30969
$including_repdigits ? : 15026 15943 30969
Condition100%3/3
$including_repdigits or $a < $b
l!l&&r!l&&!r
1502671538790
MC/DC100%2/2
$including_repdigits or $a < $b
$including_repdigits $a < $b
Truth table100%3/3
A or B
A= $including_repdigitsB= $a < $b
ABresult
1X1
011
000
279 + ($including_repdigits || $a < $b ? 0 : 1)
280 + scalar(@digits) * $rdec*($including_repdigits ? $radix : $rdec);
281 ### $i
282  
283 # see if the value is in fact smaller than ABABAB...
284 # if !repdigits and have a=b then it's bigger
285  
286 30969 ▶ if (! $including_repdigits && $a == $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $including_repdigits and $a == $b) 2414 28555 30969
Condition100%3/3
not $including_repdigits and $a == $b
!ll&&!rl&&r
15026135292414
MC/DC100%2/2
not $including_repdigits and $a == $b
not $including_repdigits $a == $b
Truth table100%3/3
not(A) and B
A= not $including_repdigitsB= $a == $b
ABresult
0X0
100
111
287 ### repdigits when a=b ...
288 2414 return $i-1;
289 }
290  
291 28555 ▶ if ($including_repdigits
Branch: 1 branch
BranchTrueFalseTotal
if ($including_repdigits or $a != $b) 28555 0 28555
Condition66%2/3
$including_repdigits or $a != $b
l!l&&r!l&&!r
15026135290
MC/DC0%0/2
$including_repdigits or $a != $b
$including_repdigits $a != $b
Truth table66%2/3
A or B
A= $including_repdigitsB= $a != $b
ABresult
1X1
011
000
292 || $a != $b) {
293  
294 28555 ▶ while (@digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits) 29635 4806 34441
295 29635 ▶ if ((my $c = pop @digits) != $a) { # found different than ABABAB...
Branch: 1 branch
BranchTrueFalseTotal
if ((my $c = pop @digits) != $a) 23749 5886 29635
296 23749 ▶ if ($c < $a) { $i -= 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $a) 10157 13592 23749
297 23749 return $i;
298 }
299 5886 ($a,$b) = ($b,$a);
300 }
301 }
302 # value is either ABAB exactly, or something bigger
303 4806 return $i;
304 }
305 2 *value_to_i_estimate = \&value_to_i_floor;
306  
307  
308 31259 ▶sub _digit_split {
Subroutine _digit_split: called CC=2 SCAR=6.9
309 31259 my ($n, $radix) = @_;
310 ### _digit_split(): $n
311 31259 my @ret;
312 31259 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 118703 31259 149962
313 118703 push @ret, $n % $radix;
314 118703 $n = int($n/$radix);
315 }
316 31259 return @ret; # low to high
317 }
318  
319 2 1;
320 __END__
« NumSeq/BalancedBinary.pm ↑ Summary NumSeq/LemoineCount.pm »