NumSeq/Repdigits.pm

filter: Statementstmt 84.1% 149 / 177 Branchbran 78.7% 52 / 66 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 85.1% 23 / 27 Podpod 100% 8 / 8 totaltotal 83.2% 239 / 287 CC 38 SCAR 15.7
CC
38
Cov
83%
_UNTESTED__seek_to_i
12.0
value_to_i_ceil
8.1
value_to_i_floor
6.1
CRAP 4.8
« NumSeq/Base/Array.pm ↑ Summary NumSeq/FibbinaryBitCount.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 package Math::NumSeq::Repdigits;
19 6 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 $VERSION = 75;
24 6 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 @ISA = ('Math::NumSeq');
26 6 *_is_infinite = \&Math::NumSeq::_is_infinite;
27 6 *_to_bigint = \&Math::NumSeq::_to_bigint;
28  
29 6 ▶use Math::NumSeq::NumAronson 8; # new in v.8
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 *_round_down_pow = \&Math::NumSeq::NumAronson::_round_down_pow;
31  
32 # uncomment this to run the ### lines
33 #use Smart::Comments;
34  
35  
36 # use constant name => Math::NumSeq::__('Repdigits');
37 6 ▶use constant description => Math::NumSeq::__('Numbers which are a "repdigit", meaning 0, 1 ... 9, 11, 22, 33, ... 99, 111, 222, 333, ..., 999, etc. The default is decimal, or select a radix.');
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use constant characteristic_increasing => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 use Math::NumSeq::Base::Digits
44 6 ▶ 'parameter_info_array'; # radix parameter
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 #------------------------------------------------------------------------------
47 # cf A002275 - repunits
48 # A108850 - repunits count of 1 bits
49  
50 6 my @oeis_anum = (
51 # OEIS-Catalogue array begin
52 undef, # 0
53 undef, # 1
54 'A000225', # radix=2 # 2^i-1
55 'A048328', # radix=3
56 'A048329', # radix=4
57 undef, # A048330 starts OFFSET=1 value=0
58 'A048331', # radix=6
59 'A048332', # radix=7
60 undef, # A048333 starts OFFSET=1 value=0
61 'A048334', # radix=9
62 'A010785', # radix=10 # starting from OFFSET=0 value=0
63 'A048335', # radix=11
64 'A048336', # radix=12
65 'A048337', # radix=13
66 'A048338', # radix=14
67 'A048339', # radix=15
68 'A048340', # radix=16
69 # OEIS-Catalogue array end
70 );
71 4 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
72 4 my ($self) = @_;
73 4 return $oeis_anum[$self->{'radix'}];
74 }
75  
76 #------------------------------------------------------------------------------
77  
78 20 ▶sub rewind {
Subroutine rewind: called CC=3 SCAR=11.1
Pod documented
79 20 my ($self) = @_;
80 20 my $radix = $self->{'radix'};
81 20 ▶ if ($radix < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix < 2) 0 20 20
82 0 $radix = $self->{'radix'} = 10;
83 }
84  
85 20 $self->{'i'} = $self->i_start;
86 20 $self->{'n'} = -1;
87 20 ▶ if ($radix != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix != 2) 16 4 20
88 16 $self->{'inc'} = 1;
89 16 $self->{'digit'} = -1;
90 }
91 }
92 0 ▶sub _UNTESTED__seek_to_i {
Subroutine _UNTESTED__seek_to_i: not called CC=3 SCAR=24.8
93 0 my ($self, $i) = @_;
94 0 $self->{'i'} = $i;
95 0 my $radix = $self->{'radix'};
96 0 ▶ if ($radix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 2) { } 0 0 0
97 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
98 0 $self->{'n'} = -1;
99 } else {
100 0 $self->{'n'} = $self->ith($i-1);
101 }
102 } else {
103 0 my $digit = $self->{'digit'} = ($i % $radix) - 1;
104 0 my $exp = int($i/$radix);
105 0 $self->{'inc'} = $self->ith($i-$digit);
106 0 $self->{'n'} = $self->{'inc'} * $digit;
107 }
108 }
109 0 ▶sub _UNTESTED__seek_to_value {
Subroutine _UNTESTED__seek_to_value: not called CC=1 SCAR=6.9
110 0 my ($self, $value) = @_;
111 0 $self->seek_to_i($self->value_to_i_ceil($value));
112 }
113  
114 132 ▶sub next {
Subroutine next: called CC=5 SCAR=16.1
Pod documented
115 132 my ($self) = @_;
116  
117 132 my $i = $self->{'i'}++;
118 132 my $radix = $self->{'radix'};
119 132 ▶ if ($radix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 2) { } 14 118 132
120 14 ▶ if ($i == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 31) 0 14 14
121 0 $self->{'n'} = _to_bigint($self->{'n'});
122 }
123 14 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 12 2 14
124 12 $self->{'n'} *= 2;
125 }
126 14 return ($i, $self->{'n'} += 1);
127  
128 } else {
129 # ENHANCE-ME: automatic promote to bigint
130  
131 118 my $n = ($self->{'n'} += $self->{'inc'});
132 118 ▶ if (++$self->{'digit'} >= $radix) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"digit"} >= $radix) 14 104 118
133 14 $self->{'inc'} = $self->{'inc'} * $radix + 1;
134 14 $self->{'digit'} = 1;
135 14 $self->{'n'} = ($n += 1); # not ++$n as that gives warnings on overflow
136 ### digit: $self->{'digit'}
137 ### inc: $self->{'inc'}
138 ### $n
139 }
140 118 return ($i, $n);
141 }
142 }
143  
144 371 ▶sub ith {
Subroutine ith: called CC=5 SCAR=16.1
Pod documented
145 371 my ($self, $i) = @_;
146 371 my $radix = $self->{'radix'};
147  
148 371 ▶ if (_is_infinite ($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 371 371
149 0 return $i;
150 }
151  
152 371 ▶ if ($radix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 2) 29 342 371
153 29 ▶ my $one = ($i >= 31 ? _to_bigint(1) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$i >= 31 ? : 1 28 29
154 29 return ($one << $i) - 1;
155 }
156  
157 342 ▶ if (($i-=1) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($i -= 1) < 0) 18 324 342
158 18 return 0;
159 }
160 324 my $digit = ($i % ($radix-1)) + 1;
161 324 $i = int($i/($radix-1)) + 1;
162 324 return ($radix ** $i - 1) / ($radix - 1) * $digit;
163 }
164  
165 3376 ▶sub pred {
Subroutine pred: called CC=6 SCAR=17.9
Pod documented
166 3376 my ($self, $value) = @_;
167  
168 {
169 3376 my $int = int($value);
170 3376 ▶ if ($value != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $int) 1653 1723 3376
171 1653 return 0;
172 }
173 1723 $value = $int; # prefer BigInt if input BigFloat
174 }
175  
176 1723 my $radix = $self->{'radix'};
177 1723 ▶ if ($radix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 2) 71 1652 1723
178 71 return ! (($value+1) & $value);
179  
180 }
181 1652 ▶ if ($radix == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 10) 1028 624 1652
182 1028 my $digit = substr($value,0,1);
183 1028 return ($value !~ /[^$digit]/);
184 }
185  
186 624 my $digit = $value % $radix;
187 624 ▶ while ($value = int($value/$radix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value = int $value / $radix) 701 62 763
188 701 ▶ unless (($value % $radix) == $digit) { # false for inf or nan
Branch: 1 branch
BranchTrueFalseTotal
unless ($value % $radix == $digit) 562 139 701
189 562 return 0;
190 }
191 }
192 62 return 1;
193 }
194  
195 835 ▶sub value_to_i_ceil {
Subroutine value_to_i_ceil: called CC=8 SCAR=20.9
Pod documented
196 835 my ($self, $value) = @_;
197 ### value_to_i_ceil(): $value
198  
199 835 ▶ if (_is_infinite ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 835 835
200 0 return $value;
201 }
202 835 ▶ if ($value <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 0) 23 812 835
203 23 return 0;
204 }
205 812 my $int = int($value);
206 812 ▶ if ($value != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $int) 63 749 812
207 63 $int += 1;
208 }
209 ### $int
210  
211 812 my $radix = $self->{'radix'};
212 812 ▶ my @digits = _digit_split_lowtohigh($int, $radix)
Branch: 1 branch
BranchTrueFalseTotal
my(@digits) = _digit_split_lowtohigh($int, $radix) or return 0 812 0 812
213 or return 0; # if $value==0
214  
215 812 my $high_digit = pop @digits;
216 812 my $i = $high_digit + ($radix-1) * scalar(@digits);
217 ### $high_digit
218 ### $i
219  
220 812 foreach my $digit (reverse @digits) { # high to low
221 1233 ▶ if ($digit > $high_digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit > $high_digit) 204 1029 1233
222 204 return $i + 1;
223 }
224 1029 ▶ if ($digit < $high_digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < $high_digit) 206 823 1029
225 206 last;
226 }
227 }
228 608 return $i;
229 }
230 855 ▶sub value_to_i_floor {
Subroutine value_to_i_floor: called CC=6 SCAR=18.1
Pod documented
231 855 my ($self, $value) = @_;
232  
233 855 ▶ if ($value < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 1) 46 809 855
234 46 return 0;
235 }
236 809 ▶ if (_is_infinite ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 809 809
237 0 return $value;
238 }
239 809 $value = int($value);
240  
241 809 my $radix = $self->{'radix'};
242 809 ▶ my @digits = _digit_split_lowtohigh($value, $radix)
Branch: 1 branch
BranchTrueFalseTotal
my(@digits) = _digit_split_lowtohigh($value, $radix) or return 0 809 0 809
243 or return 0; # if $value==0
244  
245 809 my $high_digit = pop @digits;
246 809 my $i = $high_digit + ($radix-1) * scalar(@digits);
247  
248 809 foreach my $digit (reverse @digits) { # high to low
249 1240 ▶ if ($digit < $high_digit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digit < $high_digit) 281 959 1240
250 281 return $i - 1;
251 }
252 }
253 528 return $i;
254 }
255  
256 # either floor or 1 too big
257 102 ▶sub value_to_i_estimate {
Subroutine value_to_i_estimate: called CC=3 SCAR=11.1
Pod documented
258 102 my ($self, $value) = @_;
259 ### value_to_i_estimate() ...
260  
261 102 ▶ if ($value < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 1) 36 66 102
262 36 return 0;
263 }
264 66 ▶ if (_is_infinite ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($value)) 0 66 66
265 0 return $value;
266 }
267 66 my $radix = $self->{'radix'};
268 66 my ($power, $exp) = _round_down_pow ($value, $radix);
269 66 return int($value/$power) # high digit
270 + ($radix-1) * $exp;
271 }
272  
273 #------------------------------------------------------------------------------
274  
275 {
276 6 my %binary_to_base4 = ('00' => '0',
277 '01' => '1',
278 '10' => '2',
279 '11' => '3');
280 6 my @radix_to_coderef;
281 45 ▶ $radix_to_coderef[2] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 45 (my $str = $_[0]->as_bin) =~ s/^0b//; # strip leading 0b
283 45 return reverse split //, $str;
284 6 };
285 0 ▶ $radix_to_coderef[4] = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
286 0 (my $str = $_[0]->as_bin) =~ s/^0b//; # strip leading 0b
287 0 ▶ if (length($str) & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str & 1) 0 0 0
288 0 $str = "0$str";
289 }
290 0 $str =~ s/(..)/$binary_to_base4{$1}/ge;
291 0 return reverse split //, $str;
292 6 };
293 42 ▶ $radix_to_coderef[8] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
294 42 (my $str = $_[0]->as_oct) =~ s/^0//; # strip leading 0
295 42 return reverse split //, $str;
296 6 };
297 54 ▶ $radix_to_coderef[10] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
298 54 return reverse split //, $_[0]->bstr;
299 6 };
300 0 ▶ $radix_to_coderef[16] = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 0 (my $str = $_[0]->as_hex) =~ s/^0x//; # strip leading 0x
302 0 return reverse map {hex} split //, $str;
303 6 };
304  
305 35502 ▶ sub _digit_split_lowtohigh {
Subroutine _digit_split_lowtohigh: called CC=6 SCAR=17.9
306 35502 my ($n, $radix) = @_;
307 ### _digit_split_lowtohigh(): $n
308  
309 35502 ▶ $n || return; # don't return '0' from BigInt stringize
Branch: 1 branch
BranchTrueFalseTotal
$n or return 35296 206 35502
310  
311 35296 ▶ if (ref $n
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n and $n->isa("Math::BigInt") and my $coderef = $radix_to_coderef[$radix]) 141 35155 35296
Condition83%5/6
ref $n and $n->isa("Math::BigInt") and my $coderef = $radix_to_coderef[$radix]
!ll&&!rl&&r
3513916141
ref $n and $n->isa("Math::BigInt")
!ll&&!rl&&r
351390157
MC/DC66%2/3
ref $n and $n->isa("Math::BigInt") and my $coderef = $radix_to_coderef[$radix]
ref $n $n->isa("Math::BigInt") my $coderef = $radix_to_coderef[$radix]
Truth table75%3/4
A and B and C
A= ref $nB= $n->isa("Math::BigInt")C= my $coderef = $radix_to_coderef[$radix]
ABCresult
0XX0
10X0
1100
1111
312 && $n->isa('Math::BigInt')
313 && (my $coderef = $radix_to_coderef[$radix])) {
314 141 return $coderef->($_[0]);
315 }
316  
317 35155 my @ret;
318 35155 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$n = int $n / $radix and do { do { push @ret, $n % $radix } } 274796 35155 309951
319 309951 push @ret, $n % $radix;
320 } while ($n = int($n/$radix));
321 35155 return @ret; # array[0] low digit
322 }
323 }
324  
325 6 1;
326 __END__
« NumSeq/Base/Array.pm ↑ Summary NumSeq/FibbinaryBitCount.pm »