NumSeq/ConcatNumbers.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 81.2% 13 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 93.0% 107 / 115 CC 10 SCAR 13.3
CC
10
Cov
93%
ith
8.1
oeis_anum
2.3
value_to_i_estimate
2.0
CRAP 3.8
« NumSeq/Fibbinary.pm ↑ Summary NumSeq/AsciiSelf.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2016, 2018, 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 package Math::NumSeq::ConcatNumbers;
20 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 $VERSION = 75;
25  
26 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 @ISA = ('Math::NumSeq::Base::IterateIth',
29 'Math::NumSeq');
30  
31 1 *_is_infinite = \&Math::NumSeq::_is_infinite;
32 1 *_to_bigint = \&Math::NumSeq::_to_bigint;
33  
34 1 ▶use Math::NumSeq::NumAronson 8; # new in v.8
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 *_round_down_pow = \&Math::NumSeq::NumAronson::_round_down_pow;
36  
37  
38 # use constant name => Math::NumSeq::__('Concatenate Numbers');
39 1 ▶use constant description =>
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 Math::NumSeq::__('Concatenate i and i+1, eg. at i=99 value is 99100.');
41 1 ▶use constant default_i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶use Math::NumSeq::Base::Digits;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
45 [
46 {
47 name => 'concat_count',
48 type => 'integer',
49 default => 2,
50 minimum => 1,
51 width => 2,
52 description => Math::NumSeq::__('How many numbers to concatenate.'),
53 },
54 Math::NumSeq::Base::Digits->parameter_info_list, # radix
55 1 ];
56  
57 #------------------------------------------------------------------------------
58  
59 # cf A033308 - concatenate primes
60 # A127421 - concat 2 starting n=1 for value 1, 12, 23 etc so n-1,n
61 # A074991 - concat 3, divided by 3
62 # A030655 - concat 2, step by 2, starting 1
63 # A193492 - concat 4
64 # A077298 - concat 5, step by 5
65 #
66 # A098080 - digits making an increasing sequence
67 #
68 # A058935 - binary concatenate successively, to make bignums
69 # A047778 - binary in decimal
70 # A001855 - binary number of digits
71 # A007908 - decimal concatenate successively, to make bignums
72 #
73 1 my @oeis_anum;
74  
75 1 $oeis_anum[1]->[2]->[2] = 'A087737'; # binary i,i+1 i=1
76 # OEIS-Catalogue: A087737 radix=2 i_start=1
77  
78 # A127421 OFFSET=1 so i-1,i rather than i,i+1
79 # $oeis_anum[0]->[10]->[2] = 'A127421'; # decimal i,i+1 starting i=1
80 # # OEIS-Catalogue: A127421
81  
82 1 $oeis_anum[1]->[10]->[2] = 'A001704'; # decimal i,i+1 starting i=1
83 # OEIS-Catalogue: A001704 i_start=1
84  
85 1 $oeis_anum[1]->[10]->[3] = 'A001703'; # decimal i,i+1,i+2 starting i=1
86 # OEIS-Catalogue: A001703 i_start=1 concat_count=3
87  
88 1 $oeis_anum[1]->[10]->[4] = 'A279204'; # decimal i,i+1,i+2,i+3 starting i=1
89 # OEIS-Catalogue: A279204 i_start=1 concat_count=4
90  
91 1 $oeis_anum[1]->[10]->[10] = 'A287747'; # decimal i,i+1,... 10 values starting i=1
92 # OEIS-Catalogue: A287747 i_start=1 concat_count=10
93  
94 8 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=2 SCAR=8.4
Pod documented
95 8 my ($self) = @_;
96  
97 8 ▶ if ($self->{'concat_count'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"concat_count"} == 1) 0 8 8
98 # all integers, starting from 0 or 1 according to i_start
99 0 require Math::NumSeq::All;
100 0 return $self->Math::NumSeq::All::oeis_anum;
101 # OEIS-Other: A001477 concat_count=1
102 # OEIS-Other: A001477 concat_count=1 radix=2
103 # OEIS-Other: A000027 concat_count=1 i_start=1
104 # OEIS-Other: A000027 concat_count=1 i_start=1 radix=2
105 }
106  
107 return $oeis_anum[$self->i_start]
108 ->[$self->{'radix'}]
109 8 ->[$self->{'concat_count'}];
110 }
111  
112 #------------------------------------------------------------------------------
113  
114 1096 ▶sub ith {
Subroutine ith: called CC=8 SCAR=20.9
Pod documented
115 1096 my ($self, $i) = @_;
116 ### ConcatNumbers ith(): $i
117 1096 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) 24 1072 1096
118 24 return undef;
119 }
120 1072 ▶ if (_is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 1072 1072
121 0 return $i;
122 }
123  
124 1072 my $radix = $self->{'radix'};
125 1072 my $count = $self->{'concat_count'};
126  
127 1072 my $value = $i + --$count;
128 ### initial value: "$value"
129  
130 1072 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 1072 0 1072
131 1072 my $v = $value;
132 1072 ▶ if ($count > 1 && ! ref $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 1 and not ref $i) 264 808 1072
Condition66%2/3
$count > 1 and not ref $i
!ll&&!rl&&r
8080264
MC/DC50%1/2
$count > 1 and not ref $i
$count > 1 not ref $i
Truth table66%2/3
A and not(B)
A= $count > 1B= not ref $i
ABresult
0X0
100
111
133 264 $value = _to_bigint($value);
134 }
135  
136 1072 my ($pow) = _round_down_pow ($v, $radix);
137 ### $pow
138 1072 my $value_pow = $pow * $radix;
139  
140 1072 for (;;) {
141 ### $v
142 1399 $v -= 1;
143 1399 $value += $value_pow * $v;
144 1399 ▶ last unless --$count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless --$count > 0 1072 327 1399
145  
146 327 ▶ if ($v < $pow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < $pow) 82 245 327
147 82 $pow /= $radix;
148 }
149 327 $value_pow *= $pow * $radix;
150 }
151 ### final value: "$value"
152  
153 ### assert: $v == $i
154 }
155  
156 1072 return $value;
157 }
158  
159 # sub _NOTWORKING_pred {
160 # my ($self, $value) = @_;
161 # ### ConcatNumbers pred(): $value
162 #
163 # my $int = int($value);
164 # if ($value != $int) {
165 # return 0;
166 # }
167 #
168 # my $count = $self->{'concat_count'};
169 # if ($count <= 1) {
170 # return ($count == 1);
171 # }
172 #
173 # my $radix = $self->{'radix'};
174 # my ($pow, $exp) = _round_down_pow ($int, $radix);
175 #
176 # $exp = int(($exp+1)/$count);
177 # $pow = $radix ** $exp;
178 #
179 # my $v = $value % $pow;
180 # my $rem = $value;
181 # for (;;) {
182 # $rem = int($rem/$pow);
183 # $v2 = $rem % $pow;
184 # }
185 # ### half exp: $exp
186 # ### half pow: $pow
187 # ### high: int($value/$pow)
188 # ### low: $value % $pow
189 #
190 # # return ( + 1 == ());
191 # }
192  
193 # 123124 round down pow=100_000 exp=5 want to chop low 3 digits
194 # 999_1000 round down pow=100_0000 exp=6 want to chop low 4 digits
195 # so floor (exp+2)/2
196 #
197 # 123_124_125 round down pow=100_000_000 exp=8 want to chop low 6 digits
198 # 999_1000_1001 round down pow=100_0000_0000 exp=4+4+2=10 want to chop low 8
199 # so floor (exp+2)*2/3
200 #
201 # 123_124_125_126 round down pow=100_000_000_000 exp=11 want to chop low 9
202 # 999_1000_1001_1002 round down pow=100_0000_0000_0000 exp=14 chop low 12
203 # so floor (exp+2)*3/4
204 #
205 # usual case multiple of k digits, get exp=k*ilen-1 so ilen=(exp+1)/k
206 # in between round up, so ilen=(exp+1+k-1)/k = 1+floor(exp/k)
207 #
208 163 ▶sub value_to_i_estimate {
Subroutine value_to_i_estimate: called CC=2 SCAR=6.9
Pod documented
209 163 my ($self, $value) = @_;
210 ### value_to_i_estimate(): "$value"
211  
212 163 my $radix = $self->{'radix'};
213 163 my $count = $self->{'concat_count'};
214 163 $value = int($value);
215  
216 163 my ($pow, $exp) = _round_down_pow ($value, $radix);
217 # if $value==infinity then $exp==$pow==infinity here
218  
219 163 $exp += 1;
220 163 my $ilen = int($exp/$count);
221 ### $ilen
222  
223 163 ▶ if ($exp % $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp % $count) { } 92 71 163
224 ### intermediate accumulation, treat as i=100-1 ...
225 92 return $radix ** $ilen - 1;
226 } else {
227 ### keep high ilen digits of value ...
228 71 return int ($value / ($radix ** ($exp-$ilen)))
229 }
230 }
231  
232 1 1;
233 __END__
« NumSeq/Fibbinary.pm ↑ Summary NumSeq/AsciiSelf.pm »