PlanePath/DivisibleColumns.pm

filter: Statementstmt 66.1% 117 / 177 Branchbran 45.0% 36 / 80 Conditioncond 14.5% 7 / 48 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 83.3% 20 / 24 Podpod 100% 7 / 7 totaltotal 51.9% 187 / 360 CC 59 SCAR 37.9
CC
59
Cov
52%
n_to_xy
156.0
xy_to_n
44.7
rect_to_n_range
42.4
CRAP 44.2
« PlanePath/TerdragonCurve.pm ↑ Summary PlanePath/Staircase.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath 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-PlanePath 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-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # A006218 - cumulative count of divisors
20 #
21 # Dirichlet:
22 # n * (log(n) + 2*gamma - 1) + O(sqrt(n)) gamma=0.57721... Euler-Mascheroni
23 #
24 # n * (log(n) + 2*gamma - 1) + O(log(n)*n^(1/3))
25 #
26 # Chandrasekharan: bounds
27 # n log(n) + (2 gamma - 1) n - 4 sqrt(n) - 1
28 # <= a(n) <=
29 # n log(n) + (2 gamma - 1) n + 4 sqrt(n)
30 #
31 # a(n)=2 * sum[ i=1 to floor(sqrt(n)) of floor(n/i) ] - floor(sqrt(n))^2
32 #
33 # cf A003988,A010766 - triangle with values floor(i/j)
34 #
35 # http://mathworld.wolfram.com/DirichletDivisorProblem.html
36 #
37 # compile-command: "math-image --path=DivisibleColumns --all"
38 #
39 # math-image --path=DivisibleColumns --output=numbers --all
40 #
41  
42 package Math::PlanePath::DivisibleColumns;
43 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 $VERSION = 128;
48 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
50 1 @ISA = ('Math::PlanePath');
51  
52 use Math::PlanePath::Base::Generic
53 1 'is_infinite',
54 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # uncomment this to run the ### lines
57 # use Smart::Comments;
58  
59 1 use constant parameter_info_array =>
60 [ { name => 'divisor_type',
61 share_key => 'divisor_type_allproper',
62 display => 'Divisor Type',
63 type => 'enum',
64 choices => ['all','proper'],
65 default => 'all',
66 description => 'Divisor type, with "proper" meaning divisors d<X, so excluding d=X itself.',
67 },
68 # { name => 'direction',
69 # share_key => 'direction_updown',
70 # display => 'Direction',
71 # type => 'enum',
72 # default => 'up',
73 # choices => ['up','down'],
74 # choices_display => ['Down','Up'],
75 # description => 'Number points upwards or downwards in the columns.',
76 # },
77 Math::PlanePath::Base::Generic::parameter_info_nstart0(),
78 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 1 ▶use constant default_n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 # X=2,Y=1 when proper
86 # X=1,Y=1 when not
87 sub x_minimum {
88 0 ▶ my ($self) = @_;
Subroutine x_minimum: not called CC=2 SCAR=17.9
Pod documented
89 0 ▶ return ($self->{'proper'} ? 2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'proper'} ? : 0 0 0
90 }
91 1 ▶use constant y_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 sub diffxy_minimum {
94 0 ▶ my ($self) = @_;
Subroutine diffxy_minimum: not called CC=2 SCAR=17.9
Pod documented
95 # octant Y<=X so X-Y>=0
96 0 ▶ return ($self->{'proper'} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'proper'} ? : 0 0 0
97 }
98  
99 1 ▶use constant dx_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103  
104 #------------------------------------------------------------------------------
105  
106 sub new {
107 4 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=4 SCAR=14.0
Pod documented
108  
109 4 ▶ my $divisor_type = ($self->{'divisor_type'} ||= 'all');
Condition50%1/2
$self->{"divisor_type"} ||= "all"
l!l
04
MC/DC0%0/1
$self->{"divisor_type"} ||= "all"
$self->{"divisor_type"}
Truth table50%1/2
A
A= $self->{"divisor_type"}
Aresult
11
00
110 4 $self->{'proper'} = ($divisor_type eq 'proper'); # bool
111  
112 4 ▶ $self->{'direction'} ||= 'up';
Condition50%1/2
$self->{"direction"} ||= "up"
l!l
04
MC/DC0%0/1
$self->{"direction"} ||= "up"
$self->{"direction"}
Truth table50%1/2
A
A= $self->{"direction"}
Aresult
11
00
113  
114 4 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 3 1 4
115 3 $self->{'n_start'} = $self->default_n_start;
116 }
117 4 return $self;
118 }
119  
120 1 my @x_to_n = (0,0,1);
121 sub _extend {
122 ### _extend(): $#x_to_n
123 198 ▶ my $x = $#x_to_n;
Subroutine _extend: called CC=1 SCAR=0.0
124 198 push @x_to_n, $x_to_n[$x] + _count_divisors($x);
125  
126 # if ($x > 2) {
127 # if (($x & 3) == 2) {
128 # $x >>= 1;
129 # $next_n += $x_to_n[$x] - $x_to_n[$x-1];
130 # } else {
131 # $next_n +=
132 # }
133 # }
134 ### last x: $#x_to_n
135 ### second last: $x_to_n[$#x_to_n-2]
136 ### last: $x_to_n[$#x_to_n-1]
137 ### diff: $x_to_n[$#x_to_n-1] - $x_to_n[$#x_to_n-2]
138 ### divisors of: $#x_to_n - 2
139 ### divisors: _count_divisors($#x_to_n-2)
140 ### assert: $x_to_n[$#x_to_n-1] - $x_to_n[$#x_to_n-2] == _count_divisors($#x_to_n-2)
141 }
142  
143 sub n_to_xy {
144 0 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: not called CC=12 SCAR=50.5
Pod documented
145 ### DivisibleColumns n_to_xy(): "$n"
146  
147 0 $n = $n - $self->{'n_start'}; # to N=0 basis, and warn on undef
148  
149 # $n<-0.5 works with Math::BigInt circa Perl 5.12, it seems
150 0 ▶ if ($n < -0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < "-0.5") 0 0 0
151 0 return;
152 }
153 0 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
154 0 return ($n,$n);
155 }
156  
157 0 my $frac;
158 {
159 0 my $int = int($n);
160 0 ▶ if ($n == $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == $int) { } 0 0 0
161 0 $frac = 0;
162 } else {
163 0 $frac = $n - $int; # -.5 <= $frac < 1
164 0 $n = $int; # BigFloat int() gives BigInt, use that
165 0 ▶ if ($frac > .5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frac > "0.5") 0 0 0
166 0 $frac--;
167 0 $n += 1;
168 # now -.5 <= $frac < .5
169 }
170 }
171 ### $n
172 ### n: "$n"
173 ### $frac
174 ### assert: $frac >= -.5
175 ### assert: $frac < .5
176 }
177 0 ▶ my $proper = $self->{'proper'} || 0; # cannot add false '' to BigInt
Condition0%0/2
$self->{'proper'} || 0
l!l
00
MC/DC0%0/1
$self->{'proper'} || 0
$self->{'proper'}
Truth table0%0/2
A
A= $self->{'proper'}
Aresult
11
00
178 ### $proper
179  
180 0 my $x;
181 0 ▶ if ($proper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proper) { } 0 0 0
182 0 $x = 2;
183 ### proper adjusted n: $n
184 } else {
185 0 $x = 1;
186 }
187  
188 0 for (;;) {
189 0 ▶ while ($x > $#x_to_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $#x_to_n) 0 0 0
190 0 _extend();
191 }
192 0 $n += $proper;
193 ### consider: "n=$n x=$x x_to_n=".$x_to_n[$x]
194 0 ▶ if ($x_to_n[$x] > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_to_n[$x] > $n) 0 0 0
195 0 $x--;
196 0 last;
197 }
198 0 $x++;
199 }
200 0 $n -= $x_to_n[$x];
201 0 $n -= $proper;
202 ### $x
203 ### x_to_n: $x_to_n[$x]
204 ### x_to_n next: $x_to_n[$x+1]
205 ### remainder: $n
206  
207 0 my $y = 1;
208 0 for (;;) {
209 0 ▶ unless ($x % $y) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x % $y) 0 0 0
210 0 ▶ if (--$n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$n < 0) 0 0 0
211 0 return ($x, $frac + $y);
212 }
213 }
214 0 ▶ if (++$y > $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$y > $x) 0 0 0
215 ### oops, not enough in this column
216 0 return;
217 }
218 }
219 }
220  
221 # Feturn a count of the number of integers dividing $x, including 1 and $x
222 # itself. Cf Math::Factor::XS maybe.
223 sub _count_divisors {
224 705 ▶ my ($x) = @_;
Subroutine _count_divisors: called CC=7 SCAR=19.5
225 705 my $ret = 1;
226 705 ▶ unless ($x % 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x % 2) 352 353 705
227 352 my $count = 1;
228 352 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$x % 2 or do { do { $x /= 2; ++$count } } 340 352 692
229 692 $x /= 2;
230 692 $count++;
231 } until ($x % 2);
232 352 $ret *= $count;
233 }
234 705 my $limit = _sqrtint($x);
235 705 ▶ for (my $d = 3; $d <= $limit; $d+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d <= $limit) 2007 705 2712
236 2007 ▶ unless ($x % $d) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x % $d) 414 1593 2007
237 414 my $count = 1;
238 414 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$x % $d or do { do { $x /= $d; ++$count } } 169 414 583
239 583 $x /= $d;
240 583 $count++;
241 } until ($x % $d);
242 414 $limit = sqrt($x);
243 414 $ret *= $count;
244 }
245 }
246 705 ▶ if ($x > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 1) 616 89 705
247 616 $ret *= 2;
248 }
249 705 return $ret;
250 }
251  
252 sub xy_is_visited {
253 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=6 SCAR=37.4
Pod documented
254 0 $x = round_nearest ($x);
255 0 $y = round_nearest ($y);
256 return ($y >= 1
257 0 ▶ && ($self->{'proper'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'proper'} ? : 0 0 0
Condition0%0/12
$y >= 1 && ($self->{'proper'} ? $x >= 2 && $y <= int $x / 2 : $x >= 1 && $y <= $x) && $x % $y == 0
!ll&&!rl&&r
000
$y >= 1 and $self->{"proper"} ? $x >= 2 && $y <= int $x / 2 : $x >= 1 && $y <= $x
!ll&&!rl&&r
000
$x >= 2 and $y <= int $x / 2
!ll&&!rl&&r
000
$x >= 1 and $y <= $x
!ll&&!rl&&r
000
MC/DC0%0/3
$y >= 1 && ($self->{'proper'} ? $x >= 2 && $y <= int $x / 2 : $x >= 1 && $y <= $x) && $x % $y == 0
$y >= 1 $self->{"proper"} ? $x >= 2 && $y <= int $x / 2 : $x >= 1 && $y <= $x $x % $y == 0
MC/DC0%0/2
$x >= 2 and $y <= int $x / 2
$x >= 2 $y <= int $x / 2
MC/DC0%0/2
$x >= 1 and $y <= $x
$x >= 1 $y <= $x
Truth table0%0/4
A and B && C
A= $y >= 1B= $self->{"proper"} ? $x >= 2 && $y <= int $x / 2 : $x >= 1 && $y <= $xC= $x % $y == 0
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A and B
A= $x >= 2B= $y <= int $x / 2
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $x >= 1B= $y <= $x
ABresult
0X0
100
111
258 ? $x >= 2 && $y <= int($x/2)
259 : $x >= 1 && $y <= $x)
260 && ($x%$y) == 0);
261 }
262  
263 sub xy_to_n {
264 400 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=16 SCAR=38.0
Pod documented
265 ### DivisibleColumns xy_to_n(): "$x,$y"
266  
267 400 $x = round_nearest ($x);
268 400 $y = round_nearest ($y);
269 400 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 400 400
270 400 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 400 400
271  
272 400 my $proper = $self->{'proper'};
273 400 ▶ if ($proper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proper) { } 0 400 400
274 0 ▶ if ($x < 2
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 2 or $y < 1 or $y > int $x / 2 or $x % $y) 0 0 0
Condition0%0/9
$x < 2 or $y < 1 or $y > int $x / 2 or $x % $y
l!l&&r!l&&!r
000
$x < 2 or $y < 1 or $y > int $x / 2
l!l&&r!l&&!r
000
$x < 2 or $y < 1
l!l&&r!l&&!r
000
MC/DC0%0/4
$x < 2 or $y < 1 or $y > int $x / 2 or $x % $y
$x < 2 $y < 1 $y > int $x / 2 $x % $y
Truth table0%0/5
A or B or C or D
A= $x < 2B= $y < 1C= $y > int $x / 2D= $x % $y
ABCDresult
1XXX1
01XX1
001X1
00011
00000
275 || $y < 1
276 || $y > int($x/2)
277 || ($x%$y)) {
278 0 return undef;
279 }
280 } else {
281 400 ▶ if ($x < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 1 or $y > $x or $x % $y) 0 400 400
Condition33%3/9
$x < 1 or $y < 1 or $y > $x or $x % $y
l!l&&r!l&&!r
00400
$x < 1 or $y < 1 or $y > $x
l!l&&r!l&&!r
00400
$x < 1 or $y < 1
l!l&&r!l&&!r
00400
MC/DC0%0/4
$x < 1 or $y < 1 or $y > $x or $x % $y
$x < 1 $y < 1 $y > $x $x % $y
Truth table20%1/5
A or B or C or D
A= $x < 1B= $y < 1C= $y > $xD= $x % $y
ABCDresult
1XXX1
01XX1
001X1
00011
00000
282 || $y < 1
283 || $y > $x
284 || ($x%$y)) {
285 0 return undef;
286 }
287 }
288  
289 400 ▶ while ($#x_to_n < $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#x_to_n < $x) 198 400 598
290 198 _extend();
291 }
292 ### x_to_n: $x_to_n[$x]
293  
294 400 ▶ my $n = $x_to_n[$x] - ($proper ? $x-1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$proper ? : 0 400 400
295 ### base n: $n
296  
297 400 ▶ for (my $i = 1+$proper; $i <= $y; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $y) 20300 400 20700
298 20300 ▶ unless ($x % $i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x % $i) 1298 19002 20300
299 1298 $n += 1;
300 }
301 }
302 400 return $n + $self->{'n_start'};
303 }
304  
305 # not exact
306 sub rect_to_n_range {
307 200 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=16 SCAR=37.5
Pod documented
308 ### DivisibleColumns rect_to_n_range(): "$x1,$y1 $x2,$y2"
309  
310 200 $x1 = round_nearest($x1);
311 200 $y1 = round_nearest($y1);
312 200 $x2 = round_nearest($x2);
313 200 $y2 = round_nearest($y2);
314  
315 200 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 0 200 200
316 200 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 200 200
317  
318 ### rounded ...
319 ### $x2
320 ### $y2
321  
322 200 ▶ if ($self->{'proper'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'proper'}) { } 0 200 200
323 0 ▶ if ($x2 < 2 # rect all negative
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 2 or $y2 < 1 or 2 * $y1 > $x2) 0 0 0
Condition0%0/6
$x2 < 2 or $y2 < 1 or 2 * $y1 > $x2
l!l&&r!l&&!r
000
$x2 < 2 or $y2 < 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$x2 < 2 or $y2 < 1 or 2 * $y1 > $x2
$x2 < 2 $y2 < 1 2 * $y1 > $x2
Truth table0%0/4
A or B or C
A= $x2 < 2B= $y2 < 1C= 2 * $y1 > $x2
ABCresult
1XX1
01X1
0011
0000
324 || $y2 < 1 # rect all negative
325 || 2*$y1 > $x2) { # rect all above X=2Y octant
326 ### outside proper divisors ...
327 0 return (1, 0);
328 }
329 0 ▶ if ($x1 < 2) { $x1 = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 2) 0 0 0
330 } else {
331 200 ▶ if ($x2 < 1 # rect all negative
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 1 or $y2 < 1 or $y1 > $x2) 0 200 200
Condition33%2/6
$x2 < 1 or $y2 < 1 or $y1 > $x2
l!l&&r!l&&!r
00200
$x2 < 1 or $y2 < 1
l!l&&r!l&&!r
00200
MC/DC0%0/3
$x2 < 1 or $y2 < 1 or $y1 > $x2
$x2 < 1 $y2 < 1 $y1 > $x2
Truth table25%1/4
A or B or C
A= $x2 < 1B= $y2 < 1C= $y1 > $x2
ABCresult
1XX1
01X1
0011
0000
332 || $y2 < 1 # rect all negative
333 || $y1 > $x2) { # rect all above X=Y diagonal
334 ### outside all divisors ...
335 0 return (1, 0);
336 }
337 200 ▶ if ($x1 < 1) { $x1 = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 1) 0 200 200
338 }
339 200 ▶ if (is_infinite($x2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x2)) 0 200 200
340 0 return ($self->{'n_start'}, $x2);
341 }
342  
343 200 my ($n_lo, $n_hi);
344 200 ▶ if ($x1 <= $#x_to_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 <= $#x_to_n) { } 2 198 200
345 2 $n_lo = $x_to_n[$x1];
346 } else {
347 198 $n_lo = _count_divisors_cumulative($x1-1);
348 }
349 200 ▶ if ($x2 < $#x_to_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < $#x_to_n) { } 1 199 200
350 1 $n_hi = $x_to_n[$x2+1];
351 } else {
352 199 $n_hi = _count_divisors_cumulative($x2);
353 }
354 200 $n_hi -= 1;
355  
356 ### rect at: "x=".($x2+1)." x_to_n=".($x_to_n[$x2+1]||'none')
357  
358 200 ▶ if ($self->{'proper'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proper"}) 0 200 200
359 0 $n_lo -= $x1-1;
360 0 $n_hi -= $x2;
361 }
362 return ($n_lo + $self->{'n_start'},
363 200 $n_hi + $self->{'n_start'});
364 }
365  
366 # Return a total count of all the divisors of all the integers 1 to $x
367 # inclusive.
368 sub _count_divisors_cumulative {
369 904 ▶ my ($x) = @_;
Subroutine _count_divisors_cumulative: called CC=2 SCAR=6.9
370 904 my $total = 0;
371 904 my $limit = _sqrtint($x);
372 904 foreach my $i (1 .. $limit) {
373 10820 $total += int($x/$i);
374 }
375 904 return 2*$total - $limit*$limit;
376 }
377  
378 1 1;
379 __END__
« PlanePath/TerdragonCurve.pm ↑ Summary PlanePath/Staircase.pm »