PlanePath/FactorRationals.pm

filter: Statementstmt 65.8% 139 / 211 Branchbran 48.7% 39 / 80 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 82.1% 23 / 28 Podpod 100% 4 / 4 totaltotal 62.1% 212 / 341 CC 45 SCAR 37.2
CC
45
Cov
61%
xy_to_n
113.6
_pos_to_pn__revbinary
12.0
_pn_to_pos__revbinary
12.0
CRAP 41.4
« PlanePath/R5DragonCurve.pm ↑ Summary PlanePath/WunderlichSerpentine.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 # Multiples of prime make grid.
20  
21 # [13] L. S. Johnston, Denumerability of the rational number system, Amer. Math. Monthly, 55 (Feb.
22 # 1948), no. 2, 65-70.
23 # www.jstor.org/stable/2305738
24  
25  
26 # prime factors q1,..qk of n
27 # f(m/n) = m^2*n^2/ (q1q2...qk)
28  
29 # Kevin McCrimmon, 1960
30 #
31 # integer prod p[i]^a[i] -> rational prod p[i]^b[i]
32 # b[i] = a[2i-1] if a[2i-1]!=0
33 # b[1]=a[1], b[2]=a[3], b[3]=a[5]
34 # b[i] = -a[2k] if a[2i-1]=0 and is kth such
35 #
36 # b[i] = f(a[i]) where f(n) = (-1)^(n+1) * floor((n+1)/2)
37 # f(0) = 0
38 # f(1) = 1
39 # f(2) = -1
40 # f(3) = 2
41 # f(4) = -2
42  
43 # Gerald Freilich, 1965
44 #
45 # f(n) = n/2 if n even n>=0
46 # = -(n+1)/2 if n odd n>0
47 # f(0)=0/2 = 0
48 # f(1)=-(1+1)/2 = -1
49 # f(2)=2/2 = 1
50 # f(3)=-(3+1)/2 = -2
51 # f(4)=4/2 = 2
52  
53 # Yoram Sagher, "Counting the rationals", American Math Monthly, Nov 1989,
54 # page 823. http://www.jstor.org/stable/2324846
55 #
56 # m = p1^e1.p2^e2...
57 # n = q1^f1.q2^f2...
58 # f(m/n) = p1^2e1.p2^2e2... . q1^(2f1-1).q2^(2f2-1)...
59 # so 0 -> 0 0 -> 0
60 # num 1 -> 2 1 -> -1
61 # 2 -> 4 2 -> 1
62 # den -1 1 -> 2*1-1 = 1 3 -> -2
63 # -2 2 -> 2*2-1 = 3 4 -> 2
64  
65 # Umberto Cerruti, "Ordinare i razionali Gli alberi di Keplero e di
66 # Calkin-Wilf", following T.J. Heard
67 # B(2k)=-k even=negative and zero
68 # B(2k-1)=k odd=positive
69 # which is Y/X invert
70 # B(0 =2*0) = 0
71 # B(1 =2*1-1) = 1
72 # B(2 =2*1) = -1
73 # B(3 =2*2-1) = 2
74 # B(4 =2*2) = -2
75  
76  
77 package Math::PlanePath::FactorRationals;
78 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 #use List::Util 'max';
83 1 *max = \&Math::PlanePath::_max;
84  
85 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 $VERSION = 128;
87 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 @ISA = ('Math::PlanePath');
89  
90 use Math::PlanePath::Base::Generic
91 1 'is_infinite',
92 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 use Math::PlanePath::Base::Digits
94 1 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 1 ▶use Math::PlanePath::CoprimeColumns;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 *_coprime = \&Math::PlanePath::CoprimeColumns::_coprime;
98  
99 # uncomment this to run the ### lines
100 # use Smart::Comments;
101  
102  
103 # Not yet.
104 1 use constant parameter_info_array =>
105 [ { name => 'factor_coding',
106 display => 'Sign Encoding',
107 type => 'enum',
108 default => 'even/odd',
109 choices => ['even/odd','odd/even',
110 'negabinary','revbinary',
111 ],
112 choices_display => ['Even/Odd','Odd/Even',
113 'Negabinary','Revbinary',
114 ],
115 },
116 1 ▶ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 1 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 1 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 1 ▶use constant x_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 1 ▶use constant y_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 1 ▶use constant gcdxy_maximum => 1; # no common factor
Subroutine BEGIN: called CC=1 SCAR=0.0
123 1 ▶use constant absdy_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 # factor_coding=even/odd
126 # factor_coding=odd/even
127 # dir_minimum_dxdy() suspect dir approaches 0.
128 # Eg. N=5324 = 2^2.11^3 dx=3,dy=92 0.97925
129 # N=642735 = 3^5.23^2 dX=45 dY=4 Dir4=0.05644
130 # 642736 = 2^4.17^2.139
131 # dir_maximum_dxdy() suspect approaches 360 degrees
132 # use constant dir_maximum_dxdy => (0,0); # the default
133 #
134 # factor_coding=negabinary
135 # dir_minimum_dxdy() = East 1,0 at N=1
136 # dir_maximum_dxdy() believe approaches 360 degrees
137 # Eg. N=40=2^3.5 X=5, Y=2
138 # N=41=41 X=41, Y=1
139 # N=multiple 8 and solitary primes, followed by N+1=prime is dX=big, dY=-1
140 #
141 # factor_coding=revbinary
142 # dir_maximum_dxdy() approaches 360 degrees dY=-1, dX=big
143 # Eg. N=7208=2^3*17*53 X=17*53 Y=2
144 # N=7209=3^4*89 X=3^4*89 Y=1
145 # dX=6308 dY=-1
146  
147  
148 #------------------------------------------------------------------------------
149 # even/odd
150  
151 # $n>=0, return a positive if even or negative if odd
152 # $n==0 return 0
153 # $n==1 return -1
154 # $n==2 return +1
155 # $n==3 return -2
156 # $n==4 return +2
157 sub _pos_to_pn__even_odd {
158 81 ▶ my ($n) = @_;
Subroutine _pos_to_pn__even_odd: called CC=2 SCAR=6.9
159 81 ▶ return ($n % 2 ? -1-$n : $n) / 2;
Branch: 1 branch
BranchTrueFalseTotal
$n % 2 ? : 64 17 81
160 }
161  
162 # # $n is positive or negative, return even for positive or odd for negative.
163 # # $n==0 return 0
164 # # $n==-1 return 1
165 # # $n==+1 return 2
166 # # $n==-2 return 3
167 # # $n==+2 return 4
168 # sub _pn_to_pos__even_odd {
169 # my ($n) = @_;
170 # return ($n >= 0 ? 2*$n : -1-2*$n);
171 # }
172  
173 #------------------------------------------------------------------------------
174 # odd/even
175  
176 # $n>=0, return a positive if even or negative if odd
177 # $n==0 return 0
178 # $n==1 return +1
179 # $n==2 return -1
180 # $n==3 return +2
181 # $n==4 return -2
182 sub _pos_to_pn__odd_even {
183 0 ▶ my ($n) = @_;
Subroutine _pos_to_pn__odd_even: not called CC=2 SCAR=17.9
184 0 ▶ return ($n % 2 ? $n+1 : -$n) / 2;
Branch: 1 branch
BranchTrueFalseTotal
$n % 2 ? : 0 0 0
185 }
186  
187 # # $n is positive or negative, return odd for positive or even for negative.
188 # # $n==0 return 0
189 # # $n==+1 return 1
190 # # $n==-1 return 2
191 # # $n==+2 return 3
192 # # $n==-2 return 4
193 # sub _pn_to_pos__odd_even {
194 # my ($n) = @_;
195 # return ($n <= 0 ? -2*$n : 2*$n-1);
196 # }
197  
198 #------------------------------------------------------------------------------
199 # negabinary
200  
201 sub _pn_to_pos__negabinary {
202 0 ▶ my ($n) = @_;
Subroutine _pn_to_pos__negabinary: not called CC=2 SCAR=17.9
203 0 my @bits;
204 0 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
205 0 my $bit = ($n % 2);
206 0 push @bits, $bit;
207 0 $n -= $bit;
208 0 $n /= 2;
209 0 $n = -$n;
210 }
211 0 return digit_join_lowtohigh(\@bits, 2,
212 $n); # zero
213 }
214 sub _pos_to_pn__negabinary {
215 0 ▶ my ($n) = @_;
Subroutine _pos_to_pn__negabinary: not called CC=1 SCAR=6.9
216 0 return (($n & 0x55555555) - ($n & 0xAAAAAAAA));
217 }
218  
219 #------------------------------------------------------------------------------
220 # revbinary
221 # A065620 pos -> pn
222 # A065621 pn(+ve) -> pos
223 # A048724 pn(-ve) -> pos n XOR 2n
224 # A048725 A048724 twice
225 # cf
226 # A073122 minimizing by taking +/- powers cf A072219 A072339
227  
228 # rev = 2^e0 - 2^e1 + 2^e2 - 2^e3 + ... + (-1)^t*2^et
229 # 0 <= e0 < e1 < e2 ...
230 sub _pos_to_pn__revbinary {
231 0 ▶ my ($n) = @_;
Subroutine _pos_to_pn__revbinary: not called CC=3 SCAR=24.8
232 0 my $sign = 1;
233 0 my $ret = 0;
234 0 ▶ for (my $bit = 1; $bit <= $n; $bit *= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit <= $n) 0 0 0
235 0 ▶ if ($n & $bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n & $bit) 0 0 0
236 0 $ret += $bit*$sign;
237 0 $sign = -$sign;
238 }
239 }
240 0 return $ret;
241 }
242 sub _pn_to_pos__revbinary {
243 0 ▶ my ($n) = @_;
Subroutine _pn_to_pos__revbinary: not called CC=3 SCAR=24.8
244 0 my @bits;
245 0 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
246 0 my $bit = ($n % 2);
247 0 push @bits, $bit;
248 0 $n -= $bit;
249 0 $n /= 2;
250 0 ▶ if ($bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit) 0 0 0
251 0 $n = -$n;
252 }
253 }
254 0 return digit_join_lowtohigh(\@bits, 2,
255 $n); # zero
256 }
257  
258 #------------------------------------------------------------------------------
259  
260 1 my %factor_coding__pos_to_pn = ('even/odd' => \&_pos_to_pn__even_odd,
261 'odd/even' => \&_pos_to_pn__odd_even,
262 negabinary => \&_pos_to_pn__negabinary,
263 revbinary => \&_pos_to_pn__revbinary,
264 );
265 1 my %factor_coding__pn_to_pos = (# 'even/odd' => \&_pn_to_pos__even_odd,
266 # 'odd/even' => \&_pn_to_pos__odd_even,
267 negabinary => \&_pn_to_pos__negabinary,
268 revbinary => \&_pn_to_pos__revbinary,
269 );
270  
271 sub new {
272 1 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=3 SCAR=11.4
Pod documented
273  
274 1 ▶ my $factor_coding = ($self->{'factor_coding'} ||= 'even/odd');
Condition50%1/2
$self->{"factor_coding"} ||= "even/odd"
l!l
01
MC/DC0%0/1
$self->{"factor_coding"} ||= "even/odd"
$self->{"factor_coding"}
Truth table50%1/2
A
A= $self->{"factor_coding"}
Aresult
11
00
275 1 ▶ $factor_coding__pos_to_pn{$factor_coding}
Branch: 1 branch
BranchTrueFalseTotal
$factor_coding__pos_to_pn{$factor_coding} or croak("Unrecognised factor_coding: ", $factor_coding) 0 1 1
276 or croak "Unrecognised factor_coding: ",$factor_coding;
277  
278 1 return $self;
279 }
280  
281 sub n_to_xy {
282 45 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=10 SCAR=24.3
Pod documented
283 ### FactorRationals n_to_xy(): "$n"
284  
285 45 ▶ if ($n < 1) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 45 45
286 45 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 45 45
287  
288 # what to do for fractional $n?
289 {
290 45 my $int = int($n);
291 45 ▶ if ($n != $int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $int) 0 45 45
292 ### frac ...
293 0 my $frac = $n - $int; # inherit possible BigFloat/BigRat
294 0 my ($x1,$y1) = $self->n_to_xy($int);
295 0 my ($x2,$y2) = $self->n_to_xy($int+1);
296 0 my $dx = $x2-$x1;
297 0 my $dy = $y2-$y1;
298 0 return ($frac*$dx + $x1, $frac*$dy + $y1);
299 }
300 45 $n = $int;
301 }
302  
303 45 my $zero = $n * 0;
304  
305 45 my $pos_to_pn = $factor_coding__pos_to_pn{$self->{'factor_coding'}};
306 45 my $x = my $y = ($n * 0) + 1; # inherit bignum 1
307 45 my ($limit,$overflow) = _limit($n);
308 ### $limit
309 45 my $divisor = 2;
310 45 my $dstep = 1;
311 45 ▶ while ($divisor <= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor <= $limit) 69 45 114
312 69 ▶ if (($n % $divisor) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $divisor == 0) 36 33 69
313 36 my $count = 0;
314 36 for (;;) {
315 309 $count++;
316 309 $n /= $divisor;
317 309 ▶ if ($n % $divisor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $divisor) 36 273 309
318 36 my $pn = &$pos_to_pn($count);
319 ### $count
320 ### $pn
321 36 my $pow = ($divisor+$zero) ** abs($pn);
322 36 ▶ if ($pn >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pn >= 0) { } 17 19 36
323 17 $x *= $pow;
324 } else {
325 19 $y *= $pow;
326 }
327 36 last;
328 }
329 }
330 36 ($limit,$overflow) = _limit($n);
331 ### $limit
332 }
333 69 $divisor += $dstep;
334 69 $dstep = 2;
335 }
336 45 ▶ if ($overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow) 0 45 45
337 ### n too big ...
338 0 return;
339 }
340  
341 ### remaining $n is prime, count=1: "n=$n"
342 45 my $pn = &$pos_to_pn(1);
343 ### $pn
344 45 my $pow = $n ** abs($pn);
345 45 ▶ if ($pn >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pn >= 0) { } 0 45 45
346 0 $x *= $pow;
347 } else {
348 45 $y *= $pow;
349 }
350  
351 ### result: "$x, $y"
352 45 return ($x, $y);
353 }
354  
355 sub xy_to_n {
356 5670 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=24 SCAR=47.3
Pod documented
357  
358 5670 $x = round_nearest ($x);
359 5670 $y = round_nearest ($y);
360 ### FactorRationals xy_to_n(): "x=$x y=$y"
361  
362 5670 ▶ if ($x < 1 || $y < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $y < 1) 625 5045 5670
Condition100%3/3
$x < 1 or $y < 1
l!l&&r!l&&!r
3223035045
MC/DC100%2/2
$x < 1 or $y < 1
$x < 1 $y < 1
Truth table100%3/3
A or B
A= $x < 1B= $y < 1
ABresult
1X1
011
000
363 625 return undef; # negatives and -infinity
364 }
365 5045 ▶ if (is_infinite($x)) { return $x; } # +infinity or nan
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 5045 5045
366 5045 ▶ if (is_infinite($y)) { return $y; } # +infinity or nan
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 5045 5045
367  
368 5045 ▶ if ($self->{'factor_coding'} eq 'negabinary'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'factor_coding'} eq 'negabinary' or $self->{'factor_coding'} eq 'revbinary') { } 0 5045 5045
Condition33%1/3
$self->{"factor_coding"} eq "negabinary" or $self->{"factor_coding"} eq "revbinary"
l!l&&r!l&&!r
005045
MC/DC0%0/2
$self->{"factor_coding"} eq "negabinary" or $self->{"factor_coding"} eq "revbinary"
$self->{"factor_coding"} eq "negabinary" $self->{"factor_coding"} eq "revbinary"
Truth table33%1/3
A or B
A= $self->{"factor_coding"} eq "negabinary"B= $self->{"factor_coding"} eq "revbinary"
ABresult
1X1
011
000
369 || $self->{'factor_coding'} eq 'revbinary') {
370 ### negabinary or revbinary ...
371 0 my $pn_to_pos = $factor_coding__pn_to_pos{$self->{'factor_coding'}};
372 0 my $n = 1;
373 0 my $zero = $x * 0 * $y;
374  
375 # Factorize both $x and $y and apply their pn_to_pos encoded powers to
376 # make $n. A common factor between $x and $y is noticed if $divisor
377 # divides both.
378  
379 0 my ($limit,$overflow) = _limit(max($x,$y));
380 0 my $dstep = 1;
381 0 ▶ for (my $divisor = 2; $divisor <= $limit; $divisor += $dstep, $dstep=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor <= $limit) 0 0 0
382 0 my $count = 0;
383 0 ▶ if ($x % $divisor == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x % $divisor == 0) { } 0 0 0
elsif ($y % $divisor == 0) { } 0 0 0
384 0 ▶ if ($y % $divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % $divisor == 0) 0 0 0
385 0 return undef; # common factor
386 }
387 0 ▶ while ($x % $divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % $divisor == 0) 0 0 0
388 0 $count++;
389 0 $x /= $divisor; # mutate loop variable
390 }
391 } elsif ($y % $divisor == 0) {
392 0 ▶ while ($y % $divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y % $divisor == 0) 0 0 0
393 0 $count--;
394 0 $y /= $divisor; # mutate loop variable
395 }
396 } else {
397 0 next;
398 }
399  
400 # Here $count > 0 if from $x or $count < 0 if from $y.
401 ### $count
402 ### pn: &$pn_to_pos($count)
403  
404 0 $count = &$pn_to_pos($count);
405 0 $n *= ($divisor+$zero) ** $count;
406  
407 # new search limit, perhaps smaller than before
408 0 ($limit,$overflow) = _limit(max($x,$y));
409 }
410  
411 0 ▶ if ($overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow) 0 0 0
412 ### x,y too big to find all primes ...
413 0 return undef;
414 }
415  
416 # Here $x and $y are primes.
417 0 ▶ if ($x > 1 && $x == $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 1 and $x == $y) 0 0 0
Condition0%0/3
$x > 1 and $x == $y
!ll&&!rl&&r
000
MC/DC0%0/2
$x > 1 and $x == $y
$x > 1 $x == $y
Truth table0%0/3
A and B
A= $x > 1B= $x == $y
ABresult
0X0
100
111
418 ### common factor final remaining prime x,y ...
419 0 return undef;
420 }
421  
422 # $x is power p^1 which is negabinary=1 or revbinary=1 so multiply into
423 # $n. $y is power p^-1 and -1 is negabinary=3 so cube and multiply into
424 # $n.
425 0 $n *= $x;
426 0 $n *= $y*$y*$y;
427  
428 0 return $n;
429  
430 } else {
431 ### assert: $self->{'factor_coding'} eq 'even/odd' || $self->{'factor_coding'} eq 'odd/even'
432 5045 ▶ if ($self->{'factor_coding'} eq 'odd/even') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"factor_coding"} eq "odd/even") 0 5045 5045
433 0 ($x,$y) = ($y,$x);
434 }
435  
436 # Factorize $y so as to make an odd power of its primes. Only need to
437 # divide out one copy of each prime, but by dividing out them all the
438 # $limit to search up to is reduced, usually by a lot.
439 #
440 # $ymult is $y with one copy of each prime factor divided out.
441 # $ychop is $y with all primes divided out as they're found.
442 # $y itself is unchanged.
443 #
444 5045 my $ychop = my $ymult = $y;
445  
446 5045 my ($limit,$overflow) = _limit($ychop);
447 5045 my $dstep = 1;
448 5045 ▶ for (my $divisor = 2; $divisor <= $limit; $divisor += $dstep, $dstep=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor <= $limit) 9349 2235 11584
449 9349 ▶ next if $ychop % $divisor;
Branch: 1 branch
BranchTrueFalseTotal
if $ychop % $divisor 5598 3751 9349
450  
451 3751 ▶ if ($x % $divisor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % $divisor == 0) 2810 941 3751
452 ### common factor with X ...
453 2810 return undef;
454 }
455 941 $ymult /= $divisor; # one of $divisor divided out
456 941 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$ychop % $divisor or do { do { $ychop /= $divisor } } 429 941 1370
457 1370 $ychop /= $divisor; # all of $divisor divided out
458 } until ($ychop % $divisor);
459 941 ($limit,$overflow) = _limit($ychop); # new lower $limit, perhaps
460 }
461  
462 2235 ▶ if ($overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow) 0 2235 2235
463 0 return undef; # Y too big to find all primes
464 }
465  
466 # remaining $ychop is a prime, or $ychop==1
467 2235 ▶ if ($ychop > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ychop > 1) 1958 277 2235
468 1958 ▶ if ($x % $ychop == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % $ychop == 0) 183 1775 1958
469 ### common factor with X ...
470 183 return undef;
471 }
472 1775 $ymult /= $ychop;
473 }
474  
475 2052 return $x*$x * $y*$ymult;
476 }
477 }
478  
479 #------------------------------------------------------------------------------
480  
481 # all rationals X,Y >= 1 with no common factor
482 1 ▶use Math::PlanePath::DiagonalRationals;
Subroutine BEGIN: called CC=1 SCAR=0.0
483 1 *xy_is_visited = Math::PlanePath::DiagonalRationals->can('xy_is_visited');
484  
485 #------------------------------------------------------------------------------
486  
487 # even/odd
488 # X=2^10 -> N=2^20 is X^2
489 # Y=3 -> N=3
490 # Y=3^2 -> N=3^3
491 # Y=3^3 -> N=3^5
492 # Y=3^4 -> N=3^7
493 # Y*Y / distinct prime factors
494 #
495 # negabinary
496 # X=prime^2 -> N=prime^6 is X^3
497 # X=prime^6 -> N=prime^26 is X^4.33
498 # maximum 101010...10110 -> 1101010...10 approaches factor 5
499 # same for negatives
500 #
501 # revbinary
502 # X=prime^k -> N=prime^(3k) ix X^3
503  
504 # not exact
505 sub rect_to_n_range {
506 47 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=3 SCAR=11.1
Pod documented
507 ### rect_to_n_range()
508  
509 47 $x1 = round_nearest ($x1);
510 47 $y1 = round_nearest ($y1);
511 47 $x2 = round_nearest ($x2);
512 47 $y2 = round_nearest ($y2);
513  
514 47 my $n = max($x1,$x2) * max($y1,$y2);
515 47 my $n_squared = $n * $n;
516 return (1,
517 ($self->{'factor_coding'} eq 'negabinary'
518 ? ($n_squared*$n_squared) * $n # X^5*Y^5
519 47 ▶ : $self->{'factor_coding'} eq 'revbinary'
Branch: 2 branches
BranchTrueFalseTotal
$self->{'factor_coding'} eq 'negabinary' ? : 0 47 47
$self->{'factor_coding'} eq 'revbinary' ? : 0 47 47
520 ? $n_squared * $n # X^3*Y^3
521 # even/odd, odd/even
522 : $n_squared)); # X^2*Y^2
523 }
524  
525  
526 #------------------------------------------------------------------------------
527  
528 # _limit() returns ($limit,$overflow).
529 #
530 # $limit is the biggest divisor to attempt trial division of $n. If $n <
531 # 2^32 then $limit=sqrt($n) and that will find all primes. If $n >= 2^32
532 # then $limit is smaller than sqrt($n), being calculated from the length of
533 # $n so as to make a roughly constant amount of time doing divisions. But
534 # $limit is always at least 50 so as to divide by primes up to 50.
535 #
536 # $overflow is a boolean, true if $n is too big to search for all primes and
537 # $limit is something smaller than sqrt($n). $overflow is false if $limit
538 # has not been capped and is enough to find all primes.
539 #
540 sub _limit {
541 6067 ▶ my ($n) = @_;
Subroutine _limit: called CC=2 SCAR=6.9
542 6067 my $limit = int(sqrt($n));
543 6067 my $cap = max (int(65536 * 10 / length($n)),
544 50);
545 6067 ▶ if ($limit > $cap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $cap) { } 1 6066 6067
546 1 return ($cap, 1);
547 } else {
548 6066 return ($limit, 0);
549 }
550 }
551  
552 1 1;
553 __END__
« PlanePath/R5DragonCurve.pm ↑ Summary PlanePath/WunderlichSerpentine.pm »