NumSeq/FractionDigits.pm

filter: Statementstmt 93.3% 112 / 120 Branchbran 69.2% 18 / 26 Conditioncond 22.2% 8 / 36 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 17 / 17 Podpod 100% 5 / 5 totaltotal 73.3% 162 / 221 CC 27 SCAR 28.7
CC
27
Cov
76%
oeis_anum
64.8
new
6.4
_modpow
5.2
CRAP 17.7
« NumSeq/Fibonacci.pm ↑ Summary NumSeq/Kolakoski.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::FractionDigits;
19 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
25 2 *_to_bigint = \&Math::NumSeq::_to_bigint;
26  
27 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 $VERSION = 75;
29 2 ▶use Math::NumSeq::Base::Digits;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 @ISA = ('Math::NumSeq::Base::Digits');
31  
32 # uncomment this to run the ### lines
33 #use Smart::Comments;
34  
35  
36 # use constant name => Math::NumSeq::__('Fraction Digits');
37 2 ▶use constant description => Math::NumSeq::__('A given fraction number written out in binary.');
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
41 [ Math::NumSeq::Base::Digits->parameter_info_list,
42 { name => 'fraction',
43 display => Math::NumSeq::__('Fraction'),
44 type => 'string',
45 type_hint => 'fraction',
46 width => 10,
47 default => '5/29', # an arbitrary choice
48 description => Math::NumSeq::__('The fraction to show, for example 5/29. Press Return when ready to display the expression.'),
49 },
50 2 ];
51  
52 #------------------------------------------------------------------------------
53  
54 2 my @oeis_anum;
55  
56 # table
57 # Math::NumSeq::OEIS::Catalogue::Plugin::FractionDigits
58 #
59 2 $oeis_anum[10] =
60 {
61 # Any fixed-length repeating sequence is a fraction of some sort in
62 # some radix. There's many more not expressed here, and constant
63 # digits sequences can be done by more than one fraction, etc.
64  
65 '1/7' => 'A020806', # 1/7 decimal
66 # OEIS-Catalogue: A020806 fraction=1/7
67  
68 # OFFSET=1, unlike other fractions which are OFFSET=0
69 # # '22/7' => 'A068028', # 22/7 decimal
70 # # # OEIS-Catalogue: A068028 fraction=22/7
71  
72 '1/9' => 'A000012', # 1/9 decimal, is just 1,1,1,1
73 # pending something better for a constant sequence
74 # OEIS-Catalogue: A000012 fraction=1/9
75  
76 '1/11' => 'A010680', # 1/11 decimal
77 # OEIS-Catalogue: A010680 fraction=1/11
78  
79 # OEIS-Catalogue: A021015 fraction=1/11 # duplicate of A010680
80 # OEIS-Catalogue: A021016 fraction=1/12
81 # OEIS-Catalogue: A021017 fraction=1/13
82 # OEIS-Catalogue: A021018 fraction=1/14
83 # OEIS-Catalogue: A021019 fraction=1/15
84 # OEIS-Catalogue: A021020 fraction=1/16
85  
86 '1/17' => 'A007450', # 1/17 decimal
87 # OEIS-Catalogue: A007450 fraction=1/17
88  
89 # Math::NumSeq::OEIS::Catalogue::Plugin::FractionDigits has A021022
90 # through A021999, being 1/18 to 1/995.
91 # A022000 is not 1/996, that fraction missing apparently.
92 #
93 # OEIS-Catalogue: A022001 fraction=1/997
94 # OEIS-Catalogue: A022002 fraction=1/998
95 # OEIS-Catalogue: A022003 fraction=1/999
96  
97 # OFFSET ?
98 # '1/999999' => 'A172051',
99 # # OEIS-Catalogue: A172051 fraction=1/999999
100  
101 #---------------
102  
103 # extra 10 in the denominator to give the leading 0
104 '13717421/1111111110' => 'A010888', # .012345678912...
105 # OEIS-Catalogue: A010888 fraction=13717421/1111111110
106  
107 #---------------
108  
109 # constant digits 3,3,3,...
110 '10/3' => 'A010701',
111 # ENHANCE-ME: of course can generate 3s more efficiently just as a
112 # constant sequence, in which case would prefer that over this for the
113 # catalogue.
114 # OEIS-Catalogue: A010701 fraction=10/3
115 };
116 1 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=10 SCAR=41.7
Pod documented
117 1 my ($self) = @_;
118 ### oeis_anum() ...
119 1 my $radix = $self->{'radix'};
120 1 my $fraction = $self->{'fraction'};
121 1 ▶ if (my $anum = $oeis_anum[$radix]->{$fraction}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $anum = $oeis_anum[$radix]{$fraction}) 1 0 1
122 1 return $anum;
123 }
124 0 ▶ if ($radix == 10
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999 and $2 != 996 and $2 % 10 != 0 and $2 != 25) 0 0 0
Condition0%0/21
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999 and $2 != 996 and $2 % 10 != 0 and $2 != 25
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999 and $2 != 996 and $2 % 10 != 0
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999 and $2 != 996
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1
!ll&&!rl&&r
000
$radix == 10 and $fraction =~ m[(\d+)/(\d+)]
!ll&&!rl&&r
000
MC/DC0%0/8
$radix == 10 and $fraction =~ m[(\d+)/(\d+)] and $1 == 1 and $2 >= 12 and $2 <= 999 and $2 != 996 and $2 % 10 != 0 and $2 != 25
$radix == 10 $fraction =~ m[(\d+)/(\d+)] $1 == 1 $2 >= 12 $2 <= 999 $2 != 996 $2 % 10 != 0 $2 != 25
Truth table0%0/9
A and B and C and D and E and F and G and H
A= $radix == 10B= $fraction =~ m[(\d+)/(\d+)]C= $1 == 1D= $2 >= 12E= $2 <= 999F= $2 != 996G= $2 % 10 != 0H= $2 != 25
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
125 && $fraction =~ m{(\d+)/(\d+)}
126 && $1 == 1
127 && $2 >= 12 && $2 <= 999 && $2 != 996
128 && ($2 % 10) != 0
129 && $2 != 25) {
130 0 return 'A0'.($2 + 21016-12);
131 }
132 ### $fraction
133 0 return undef;
134 }
135  
136 #------------------------------------------------------------------------------
137  
138 3 ▶sub new {
Subroutine new: called CC=6 SCAR=18.5
Pod documented
139 3 my $self = shift->SUPER::new(@_);
140  
141 3 my $radix = $self->{'radix'};
142 3 my $fraction = $self->{'fraction'};
143  
144 3 my $num = 0; # 0/0 if unrecognised
145 3 my $den = 0;
146 3 ($num, $den) = ($fraction =~ m{^\s*
147 ([.[:digit:]]+)?
148 \s*
149 (?:/\s*
150 ([.[:digit:]]+)?
151 )?
152 \s*$}x);
153 0 ▶ if (! defined $num) { $num = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 3 3
154 0 ▶ if (! defined $den) { $den = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $den) 0 3 3
155 ### $num
156 ### $den
157 3 $fraction = "$num/$den";
158  
159 # decimals like 1.5/2.75 become 150/275
160 {
161 3 ($num, my $num_decimals) = _to_int_and_decimals ($num);
162 3 ($den, my $den_decimals) = _to_int_and_decimals ($den);
163 3 $num .= '0' x max(0, $den_decimals - $num_decimals);
164 3 $den .= '0' x max(0, $num_decimals - $den_decimals);
165 }
166  
167 3 ▶ if (max(length($num),length($den)) >= length(int (~0 / $radix))) {
Branch: 1 branch
BranchTrueFalseTotal
if (&max(length $num, length $den) >= length int '18446744073709551615' / $radix) 1 2 3
168 1 $num = _to_bigint($num);
169 1 $den = _to_bigint($den);
170 }
171  
172 # increase den so first digit is 0 to radix-1
173 3 ▶ while ($den != 0 && $num >= $den) {
Branch: 1 branch
BranchTrueFalseTotal
if ($den != 0 and $num >= $den) 0 3 3
Condition33%1/3
$den != 0 and $num >= $den
!ll&&!rl&&r
030
MC/DC0%0/2
$den != 0 and $num >= $den
$den != 0 $num >= $den
Truth table33%1/3
A and B
A= $den != 0B= $num >= $den
ABresult
0X0
100
111
174 0 $den *= $radix;
175 }
176  
177 ### create
178 ### $num
179 ### $den
180 3 $self->{'fraction'} = $fraction;
181 3 $self->{'initial_num'} = $num;
182 3 $self->{'den'} = $den;
183  
184 3 $self->rewind;
185 3 return $self;
186 }
187  
188 14 ▶sub _to_int_and_decimals {
Subroutine _to_int_and_decimals: called CC=2 SCAR=6.9
189 14 my ($n) = @_;
190 14 ▶ if ($n =~ m{^(\d*)\.(\d*?)0*$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /^(\d*)\.(\d*?)0*$/) { } 7 7 14
191 7 return ($1 . $2,
192 length($2));
193 } else {
194 7 return ($n, 0);
195 }
196 }
197  
198 8 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
199 8 my ($self) = @_;
200 8 $self->{'i'} = $self->i_start;
201 8 $self->{'num'} = $self->{'initial_num'};
202 }
203  
204 144 ▶sub next {
Subroutine next: called CC=3 SCAR=11.1
Pod documented
205 144 my ($self) = @_;
206  
207 144 ▶ my $num = $self->{'num'} || return; # num==0 exact radix frac
Condition50%1/2
$self->{'num'} || (return)
l!l
1440
MC/DC0%0/1
$self->{'num'} || (return)
$self->{'num'}
Truth table50%1/2
A
A= $self->{'num'}
Aresult
11
00
208 144 ▶ my $den = $self->{'den'} || return; # den==0 invalid
Condition50%1/2
$self->{'den'} || (return)
l!l
1440
MC/DC0%0/1
$self->{'den'} || (return)
$self->{'den'}
Truth table50%1/2
A
A= $self->{'den'}
Aresult
11
00
209 144 my $radix = $self->{'radix'};
210 ### FractionDigits next(): "$self->{'i'} $num/$den"
211  
212 144 $num *= $radix;
213 144 my $quot = int ($num / $den);
214 144 $self->{'num'} = $num - $quot * $den;
215  
216 ### $quot
217 ### rem: $self->{'num'}
218  
219 144 return ($self->{'i'}++, $quot);
220 }
221  
222 39 ▶sub ith {
Subroutine ith: called CC=3 SCAR=11.0
Pod documented
223 39 my ($self, $i) = @_;
224  
225 39 ▶ if ($i < 0 || _is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0 or _is_infinite($i)) 3 36 39
Condition66%2/3
$i < 0 or _is_infinite($i)
l!l&&r!l&&!r
3036
MC/DC50%1/2
$i < 0 or _is_infinite($i)
$i < 0 _is_infinite($i)
Truth table66%2/3
A or B
A= $i < 0B= _is_infinite($i)
ABresult
1X1
011
000
226 3 return undef;
227 }
228  
229 36 my $radix = $self->{'radix'};
230 36 my $den = $self->{'den'};
231 36 my $num = (($self->{'initial_num'} * _modpow ($self->{'radix'}, $i, $den))
232 % $den);
233 36 return int (($num * $radix) / $den);
234 }
235  
236 2 ▶use constant 1.02 _UV_MAX_SQRT => do {
Subroutine BEGIN: called CC=4 SCAR=13.9
237 2 my $uv_max = ~0;
238 2 my $bit = 1;
239 2 my $shift = 0;
240 2 for (;;) {
241 64 $shift++;
242 64 my $try_bit = $bit << 1;
243 64 ▶ if ($uv_max >> $shift < $try_bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uv_max >> $shift < $try_bit) 2 62 64
244 2 last;
245 }
246 62 $bit = $try_bit;
247 }
248 ### $bit
249  
250 2 my $uv_max_sqrt = $bit;
251 2 for (;;) {
252 64 $bit >>= 1;
253 64 ▶ last if $bit == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bit == 0 2 62 64
254 62 my $try_sqrt = $uv_max_sqrt + $bit;
255 62 ▶ if (int($uv_max / $try_sqrt) <= $try_sqrt) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $uv_max / $try_sqrt <= $try_sqrt) 0 62 62
256 0 $uv_max_sqrt = $try_sqrt;
257 }
258 }
259  
260 ### $uv_max_sqrt
261 ### uv_max_sqrt: sprintf '%#X', $uv_max_sqrt
262 ### squared: $uv_max_sqrt*$uv_max_sqrt
263 ### squared: sprintf '%#X', $uv_max_sqrt*$uv_max_sqrt
264  
265 2 $uv_max_sqrt
266 2 };
267  
268 56 ▶sub _modpow {
Subroutine _modpow: called CC=5 SCAR=16.5
269 56 my ($base, $exp, $mod) = @_;
270 ### _modpow(): "$base $exp $mod"
271  
272 56 my $ret = 1;
273 56 ▶ if (ref $mod || $mod > _UV_MAX_SQRT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mod or $mod > 2147483648) 0 56 56
Condition33%1/3
ref $mod or $mod > 2147483648
l!l&&r!l&&!r
0056
MC/DC0%0/2
ref $mod or $mod > 2147483648
ref $mod $mod > 2147483648
Truth table33%1/3
A or B
A= ref $modB= $mod > 2147483648
ABresult
1X1
011
000
274 0 return _to_bigint($base)->bmodpow($exp,$mod);
275 }
276  
277 # only if base and mod have no common factor ...
278 # $exp %= $mod-1;
279  
280 56 my $power = $base;
281 56 for (;;) {
282 ### $exp
283 160 ▶ if ($exp % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp % 2) 91 69 160
284 ### step: "power=$power"
285 91 $ret = ($ret * $power) % $mod;
286 }
287 160 ▶ $exp = int($exp/2) || last;
Condition100%2/2
int $exp / 2 || (last)
l!l
10456
MC/DC100%1/1
int $exp / 2 || (last)
int $exp / 2
Truth table100%2/2
A
A= int $exp / 2
Aresult
11
00
288 104 $power = ($power*$power) % $mod;
289 }
290 56 return $ret;
291 }
292  
293  
294 # ENHANCE-ME: only some digits occur, being the modulo den residue class
295 # containing num.
296 # sub pred {
297 # my ($self, $value) = @_;
298 # }
299 #
300 # =item C<$bool = $seq-E<gt>pred($value)>
301 #
302 # Return true if C<$value> occurs as a digit in the fraction.
303  
304 2 1;
305 __END__
« NumSeq/Fibonacci.pm ↑ Summary NumSeq/Kolakoski.pm »