PlanePath/CoprimeColumns.pm

filter: Statementstmt 89.8% 133 / 148 Branchbran 80.3% 45 / 56 Conditioncond 68.2% 28 / 41 MC/DCmcdc 68.1% 15 / 22 Subroutinesub 96.0% 24 / 25 Podpod 100% 5 / 5 totaltotal 84.1% 250 / 297 CC 44 SCAR 22.0
CC
44
Cov
84%
xy_is_visited
30.0
xy_to_n
13.4
n_to_xy
11.2
CRAP 9.0
« PlanePath/HIndexing.pm ↑ Summary PlanePath/AlternatePaperMidpoint.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 # math-image --path=CoprimeColumns --all --scale=10
20 # math-image --path=CoprimeColumns --output=numbers --all
21  
22 package Math::PlanePath::CoprimeColumns;
23 10 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 ▶use vars '$VERSION', '@ISA', '@_x_to_n';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 $VERSION = 128;
28 10 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 @ISA = ('Math::PlanePath');
30  
31 use Math::PlanePath::Base::Generic
32 10 'is_infinite',
33 10 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38  
39 10 ▶use constant default_n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 10 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 10 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 10 ▶use constant n_frac_discontinuity => .5;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 10 ▶use constant x_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 10 ▶use constant y_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 10 ▶ use constant diffxy_minimum => 0; # octant Y<=X so X-Y>=0
Subroutine BEGIN: called CC=1 SCAR=0.0
47 10 ▶use constant gcdxy_maximum => 1; # no common factor
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 10 ▶use constant dx_minimum => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 10 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 10 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 10 use constant parameter_info_array =>
54 [
55 { name => 'direction',
56 share_key => 'direction_updown',
57 display => 'Direction',
58 type => 'enum',
59 default => 'up',
60 choices => ['up','down'],
61 choices_display => ['Down','Up'],
62 description => 'Number points upwards or downwards in the columns.',
63 },
64 Math::PlanePath::Base::Generic::parameter_info_nstart0(),
65 10 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 #------------------------------------------------------------------------------
68  
69 sub new {
70 8 ▶ my $self = shift->SUPER::new (@_);
Subroutine new: called CC=3 SCAR=11.0
Pod documented
71 8 ▶ $self->{'direction'} ||= 'up';
Condition100%2/2
$self->{"direction"} ||= "up"
l!l
17
MC/DC100%1/1
$self->{"direction"} ||= "up"
$self->{"direction"}
Truth table100%2/2
A
A= $self->{"direction"}
Aresult
11
00
72 8 ▶ if (! defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n_start"}) 7 1 8
73 7 $self->{'n_start'} = $self->default_n_start;
74 }
75 8 return $self;
76 }
77  
78  
79 # shared with DiagonalRationals
80 10 @_x_to_n = (0,0,1);
81 sub _extend {
82 ### _extend(): $#_x_to_n
83 298 ▶ my $x = $#_x_to_n;
Subroutine _extend: called CC=1 SCAR=0.0
84 298 push @_x_to_n, $_x_to_n[$x] + _totient($x);
85  
86 # if ($x > 2) {
87 # if (($x & 3) == 2) {
88 # $x >>= 1;
89 # $next_n += $_x_to_n[$x] - $_x_to_n[$x-1];
90 # } else {
91 # $next_n +=
92 # }
93 # }
94 ### last x: $#_x_to_n
95 ### second last: $_x_to_n[$#_x_to_n-2]
96 ### last: $_x_to_n[$#_x_to_n-1]
97 ### diff: $_x_to_n[$#_x_to_n-1] - $_x_to_n[$#_x_to_n-2]
98 ### totient of: $#_x_to_n - 2
99 ### totient: _totient($#_x_to_n-2)
100 ### assert: $_x_to_n[$#_x_to_n-1] - $_x_to_n[$#_x_to_n-2] == _totient($#_x_to_n-2)
101 }
102  
103 sub n_to_xy {
104 46 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=11 SCAR=24.2
Pod documented
105 ### CoprimeColumns n_to_xy(): $n
106  
107 46 $n = $n - $self->{'n_start'}; # to N=0 basis, and warn on undef
108  
109 # $n<-0.5 is ok for Math::BigInt circa Perl 5.12, it seems
110 46 ▶ if (2*$n < -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n < -1) 2 44 46
111 2 return;
112 }
113 44 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 44 44
114 0 return ($n,$n);
115 }
116  
117 44 my $frac;
118 {
119 44 my $int = int($n);
120 44 $frac = $n - $int; # -.5 <= $frac < 1
121 44 $n = $int; # BigFloat int() gives BigInt, use that
122  
123 44 ▶ if (2*$frac >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $frac >= 1) 0 44 44
124 0 $frac--;
125 0 $n += 1;
126 # now -.5 <= $frac < .5
127 }
128 ### $n
129 ### $frac
130 ### assert: 2*$frac >= -1
131 ### assert: 2*$frac < 1
132 }
133  
134 44 my $x = 1;
135 44 for (;;) {
136 252 ▶ while ($x > $#_x_to_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $#Math::PlanePath::CoprimeColumns::_x_to_n) 13 252 265
137 13 _extend();
138 }
139 252 ▶ if ($_x_to_n[$x] > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::PlanePath::CoprimeColumns::_x_to_n[$x] > $n) 44 208 252
140 44 $x--;
141 44 last;
142 }
143 208 $x++;
144 }
145 44 $n -= $_x_to_n[$x];
146 ### $x
147 ### n base: $_x_to_n[$x]
148 ### n next: $_x_to_n[$x+1]
149 ### remainder: $n
150  
151 44 my $y = 1;
152 44 for (;;) {
153 104 ▶ if (_coprime($x,$y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_coprime($x, $y)) 90 14 104
154 90 ▶ if (--$n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$n < 0) 44 46 90
155 44 last;
156 }
157 }
158 60 ▶ if (++$y >= $x) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$y >= $x) 0 60 60
159 ### oops, not enough in this column ...
160 0 return;
161 }
162 }
163  
164 44 $y += $frac;
165 44 ▶ if ($x >= 2 && $self->{'direction'} eq 'down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 2 and $self->{"direction"} eq "down") 27 17 44
Condition100%3/3
$x >= 2 and $self->{"direction"} eq "down"
!ll&&!rl&&r
51227
MC/DC100%2/2
$x >= 2 and $self->{"direction"} eq "down"
$x >= 2 $self->{"direction"} eq "down"
Truth table100%3/3
A and B
A= $x >= 2B= $self->{"direction"} eq "down"
ABresult
0X0
100
111
166 27 $y = $x - $y;
167 }
168 44 return ($x, $y);
169 }
170  
171 sub xy_is_visited {
172 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_is_visited: not called CC=5 SCAR=34.0
Pod documented
173 0 $x = round_nearest ($x);
174 0 $y = round_nearest ($y);
175 0 ▶ if ($x < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)) 0 0 0
Condition0%0/9
$x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)
l!l&&r!l&&!r
000
$x < 1 or $y < 1 or $y >= $x + ($x == 1)
l!l&&r!l&&!r
000
$x < 1 or $y < 1
l!l&&r!l&&!r
000
MC/DC0%0/4
$x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)
$x < 1 $y < 1 $y >= $x + ($x == 1) not _coprime($x, $y)
Truth table0%0/5
A or B or C or not(D)
A= $x < 1B= $y < 1C= $y >= $x + ($x == 1)D= not _coprime($x, $y)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
176 || $y < 1
177 || $y >= $x+($x==1) # Y<X except X=Y=1 included
178 || ! _coprime($x,$y)) {
179 0 return 0;
180 }
181 0 return 1;
182 }
183  
184 sub xy_to_n {
185 11283 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=13 SCAR=26.0
Pod documented
186 ### CoprimeColumns xy_to_n(): "$x,$y"
187 11283 $x = round_nearest ($x);
188 11283 $y = round_nearest ($y);
189 11283 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 11283 11283
190 11283 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 11283 11283
191 11283 ▶ if ($x < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)) 7282 3052 10334
Condition100%9/9
$x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)
l!l&&r!l&&!r
379144403052
$x < 1 or $y < 1 or $y >= $x + ($x == 1)
l!l&&r!l&&!r
94928427492
$x < 1 or $y < 1
l!l&&r!l&&!r
35559410334
MC/DC100%4/4
$x < 1 or $y < 1 or $y >= $x + ($x == 1) or not _coprime($x, $y)
$x < 1 $y < 1 $y >= $x + ($x == 1) not _coprime($x, $y)
Truth table100%5/5
A or B or C or not(D)
A= $x < 1B= $y < 1C= $y >= $x + ($x == 1)D= not _coprime($x, $y)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
192 || $y < 1
193 || $y >= $x+($x==1) # Y<X except X=Y=1 included
194 || ! _coprime($x,$y)) {
195 8231 return undef;
196 }
197  
198 3052 ▶ if ($x >= 2 && $self->{'direction'} eq 'down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 2 and $self->{"direction"} eq "down") 2034 1018 3052
Condition100%3/3
$x >= 2 and $self->{"direction"} eq "down"
!ll&&!rl&&r
310152034
MC/DC100%2/2
$x >= 2 and $self->{"direction"} eq "down"
$x >= 2 $self->{"direction"} eq "down"
Truth table100%3/3
A and B
A= $x >= 2B= $self->{"direction"} eq "down"
ABresult
0X0
100
111
199 2034 $y = $x - $y;
200 }
201  
202 3052 ▶ while ($#_x_to_n < $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#Math::PlanePath::CoprimeColumns::_x_to_n < $x) 0 3052 3052
203 0 _extend();
204 }
205 3052 my $n = $_x_to_n[$x];
206 ### base n: $n
207 3052 ▶ if ($y != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y != 1) 2935 117 3052
208 2935 foreach my $i (1 .. $y-1) {
209 130132 ▶ if (_coprime($x,$i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_coprime($x, $i)) 68320 61812 130132
210 68320 $n += 1;
211 }
212 }
213 }
214 3052 return $n + $self->{'n_start'};
215 }
216  
217 # Asymptotically
218 # phisum(x) ~ 1/(2*zeta(2)) * x^2 + O(x ln x)
219 # = 3/pi^2 * x^2 + O(x ln x)
220 # or by Walfisz
221 # phisum(x) ~ 3/pi^2 * x^2 + O(x * (ln x)^(2/3) * (ln ln x)^4/3)
222 #
223 # but want an upper bound, so that for a given X at least enough N is
224 # covered ...
225 #
226 # Note: DiagonalRationals depends on this working only to column resolution.
227 # not exact
228 sub rect_to_n_range {
229 24 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=9 SCAR=23.8
Pod documented
230 ### CoprimeColumns rect_to_n_range(): "$x1,$y1 $x2,$y2"
231  
232 24 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 1 23 24
233 24 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 0 24 24
234 24 $x2 = round_nearest($x2);
235 24 $y2 = round_nearest($y2);
236 ### rounded ...
237 ### $x2
238 ### $y2
239  
240 24 ▶ if ($x2 < 1 || $y2 < 1
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 1 or $y2 < 1 or $y1 >= $x2 + ($x2 == 1)) 0 24 24
Condition33%2/6
$x2 < 1 or $y2 < 1 or $y1 >= $x2 + ($x2 == 1)
l!l&&r!l&&!r
0024
$x2 < 1 or $y2 < 1
l!l&&r!l&&!r
0024
MC/DC0%0/3
$x2 < 1 or $y2 < 1 or $y1 >= $x2 + ($x2 == 1)
$x2 < 1 $y2 < 1 $y1 >= $x2 + ($x2 == 1)
Truth table25%1/4
A or B or C
A= $x2 < 1B= $y2 < 1C= $y1 >= $x2 + ($x2 == 1)
ABCresult
1XX1
01X1
0011
0000
241 # bottom right corner above X=Y diagonal, except X=1,Y=1 included
242 || ($y1 >= $x2 + ($x2 == 1))) {
243 ### outside ...
244 0 return (1, 0);
245 }
246 24 ▶ if (is_infinite($x2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x2)) 0 24 24
247 0 return ($self->{'n_start'}, $x2);
248 }
249  
250 24 ▶ while ($#_x_to_n <= $x2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#Math::PlanePath::CoprimeColumns::_x_to_n <= $x2) 93 24 117
251 93 _extend();
252 }
253  
254 ### rect use xy_to_n at: "x=".($x2+1)." y=1"
255 24 ▶ if ($x1 < 0) { $x1 = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < 0) 2 22 24
256 return ($_x_to_n[$x1] + $self->{'n_start'},
257 24 $_x_to_n[$x2+1] - 1 + $self->{'n_start'});
258  
259 # asympototically ?
260 # return ($self->{'n_start'}, $self->{'n_start'} + .304*$x2*$x2 + 20);
261 }
262  
263 # A000010
264 sub _totient {
265 404 ▶ my ($x) = @_;
Subroutine _totient: called CC=5 SCAR=16.1
266 404 ▶ my $count = (1 # y=1 always
Condition100%9/9
$x > 2 && $x & 1
!ll&&!rl&&r
6199199
$x > 3 && $x % 3 != 0
!ll&&!rl&&r
10130264
$x > 4 && $x & 1
!ll&&!rl&&r
14195195
MC/DC100%2/2
$x > 2 && $x & 1
$x > 2 $x & 1
MC/DC100%2/2
$x > 3 && $x % 3 != 0
$x > 3 $x % 3 != 0
MC/DC100%2/2
$x > 4 && $x & 1
$x > 4 $x & 1
Truth table100%3/3
A && B
A= $x > 2B= $x & 1
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= $x > 3B= $x % 3 != 0
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= $x > 4B= $x & 1
ABresult
0X0
100
111
267 + ($x > 2 && ($x&1)) # y=2 if $x odd
268 + ($x > 3 && ($x % 3) != 0) # y=3
269 + ($x > 4 && ($x&1)) # y=4 if $x odd
270 );
271 404 ▶ for (my $y = 5; $y < $x; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $x) 28231 404 28635
272 28231 $count += _coprime($x,$y);
273 }
274 404 return $count;
275 }
276  
277 # code here only uses X>=Y but allow for any X,Y>=0 for elsewhere
278 sub _coprime {
279 171850 ▶ my ($x, $y) = @_;
Subroutine _coprime: called CC=4 SCAR=13.9
280 #### _coprime(): "$x,$y"
281  
282 171850 ▶ if ($y > $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $x) 448 171402 171850
283 448 ▶ if ($x <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= 1) 68 380 448
284 ### result yes ...
285 68 return 1;
286 }
287 380 $y %= $x;
288 }
289  
290 171782 for (;;) {
291 696899 ▶ if ($y <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= 1) 171782 525117 696899
292 ### result: ($y == 1)
293 171782 return ($y == 1);
294 }
295 525117 ($x,$y) = ($y, $x % $y);
296 }
297 }
298  
299 10 1;
300 __END__
« PlanePath/HIndexing.pm ↑ Summary PlanePath/AlternatePaperMidpoint.pm »