blib/lib/Math/GrahamFunction.pm

filter: Statementstmt 100% 131 / 131 Branchbran 95.0% 38 / 40 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 26 / 26 Podpod 100% 1 / 1 totaltotal 98.1% 209 / 213 SCAR 33.0
CC
27
Cov
98%
_update_base
6.0
_get_i_vec
4.0
_get_min_id
4.0
CRAP 27.0
↑ Summary blib/lib/Math/GrahamFunction/SqFacts.pm »
1 package Math::GrahamFunction;
2 3 $Math::GrahamFunction::VERSION = '0.02002';
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 ▶use parent qw(Math::GrahamFunction::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Math::GrahamFunction::SqFacts;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Math::GrahamFunction::SqFacts::Dipole;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 __PACKAGE__->mk_accessors(qw(
15 _base
16 n
17 _n_vec
18 next_id
19 _n_sq_factors
20 primes_to_ids_map
21 ));
22  
23 sub _initialize
24 {
25 100 ▶ my $self = shift;
Subroutine _initialize: called CC=2 SCAR=7.0
26 100 my $args = shift;
27  
28 100 ▶ $self->n($args->{n}) or
Branch: 1 branch
BranchTrueFalseTotal
$self->n($args->{"n"}) or die "n was not specified" 0 100 100
29 die "n was not specified";
30  
31 100 $self->primes_to_ids_map({});
32  
33 100 return 0;
34 }
35  
36  
37 sub _get_num_facts
38 {
39 844 ▶ my ($self, $number) = @_;
Subroutine _get_num_facts: called CC=1 SCAR=0.0
40  
41 844 return Math::GrahamFunction::SqFacts->new({ 'n' => $number });
42 }
43  
44 sub _get_facts
45 {
46 745 ▶ my ($self, $factors) = @_;
Subroutine _get_facts: called CC=2 SCAR=7.2
47  
48 return
49 745 ▶ Math::GrahamFunction::SqFacts->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $factors eq 'ARRAY' ? : 0 745 745
50 { 'factors' =>
51 (ref($factors) eq "ARRAY" ? $factors : [$factors])
52 }
53 );
54 }
55  
56 sub _get_num_dipole
57 {
58 745 ▶ my ($self, $number) = @_;
Subroutine _get_num_dipole: called CC=1 SCAR=0.0
59  
60 745 return Math::GrahamFunction::SqFacts::Dipole->new(
61 {
62 'result' => $self->_get_num_facts($number),
63 'compose' => $self->_get_facts($number),
64 }
65 );
66  
67 }
68  
69 sub _calc_n_sq_factors
70 {
71 100 ▶ my $self = shift;
Subroutine _calc_n_sq_factors: called CC=1 SCAR=0.0
72  
73 100 $self->_n_sq_factors(
74 $self->_get_num_dipole($self->n)
75 );
76 }
77  
78 sub _check_largest_factor_in_between
79 {
80 90 ▶ my $self = shift;
Subroutine _check_largest_factor_in_between: called CC=3 SCAR=11.0
81  
82 90 my $n = $self->n();
83 # Cheating:
84 # Check if between n and n+largest_factor we can fit
85 # a square of SqFact{n*(n+largest_factor)}. If so, return
86 # n+largest_factor.
87 #
88 # So, for instance, if n = p than n+largest_factor = 2p
89 # and so SqFact{p*(2p)} = 2 and it is possible to see if
90 # there's a 2*i^2 between p and 2p. That way, p*2*i^2*2p is
91 # a square number.
92  
93 90 my $largest_factor = $self->_n_sq_factors()->last();
94  
95 90 my ($lower_bound, $lb_sq_factors);
96  
97 90 $lower_bound = $self->n() + $largest_factor;
98 90 while (1)
99 {
100 99 $lb_sq_factors = $self->_get_num_facts($lower_bound);
101 99 ▶ if ($lb_sq_factors->exists($largest_factor))
Branch: 1 branch
BranchTrueFalseTotal
if ($lb_sq_factors->exists($largest_factor)) 90 9 99
102 {
103 90 last;
104 }
105 9 $lower_bound += $largest_factor;
106 }
107  
108 90 my $n_times_lb = $self->_n_sq_factors->result->mult($lb_sq_factors);
109  
110 90 my $rest_of_factors_product = $n_times_lb->product();
111  
112 90 my $low_square_val = int(sqrt($n/$rest_of_factors_product));
113 90 my $high_square_val = int(sqrt($lower_bound/$rest_of_factors_product));
114  
115 90 ▶ if ($low_square_val != $high_square_val)
Branch: 1 branch
BranchTrueFalseTotal
if ($low_square_val != $high_square_val) { } 44 46 90
116 {
117 44 my @factors =
118 (
119 $n,
120 ($low_square_val+1)*($low_square_val+1)*$rest_of_factors_product,
121 $lower_bound
122 );
123 # TODO - possibly convert to Dipole
124 # return ($lower_bound, $self->_get_facts(\@factors));
125 44 return \@factors;
126 }
127 else
128 {
129 46 return;
130 }
131 }
132  
133 sub _get_next_id
134 {
135 416 ▶ my $self = shift;
Subroutine _get_next_id: called CC=1 SCAR=0.0
136 416 return $self->next_id($self->next_id()+1);
137 }
138  
139 sub _get_prime_id
140 {
141 2118 ▶ my $self = shift;
Subroutine _get_prime_id: called CC=1 SCAR=0.0
142 2118 my $p = shift;
143 2118 return $self->primes_to_ids_map()->{$p};
144 }
145  
146 sub _register_prime
147 {
148 416 ▶ my ($self, $p) = @_;
Subroutine _register_prime: called CC=1 SCAR=0.0
149 416 $self->primes_to_ids_map()->{$p} = $self->_get_next_id();
150 }
151  
152 sub _prime_exists
153 {
154 819 ▶ my ($self, $p) = @_;
Subroutine _prime_exists: called CC=1 SCAR=0.0
155 819 return exists($self->primes_to_ids_map->{$p});
156 }
157  
158 sub _get_min_id
159 {
160 1017 ▶ my ($self, $vec) = @_;
Subroutine _get_min_id: called CC=4 SCAR=13.9
161  
162 1017 my $min_id = -1;
163 1017 my $min_p = 0;
164  
165 1017 foreach my $p (@{$vec->result()->factors()})
166 {
167 1637 my $id = $self->_get_prime_id($p);
168 1637 ▶ if (($min_id < 0) || ($min_id > $id))
Branch: 1 branch
BranchTrueFalseTotal
if ($min_id < 0 or $min_id > $id) 1144 493 1637
Condition100%3/3
$min_id < 0 or $min_id > $id
l!l&&r!l&&!r
984160493
MC/DC100%2/2
$min_id < 0 or $min_id > $id
$min_id < 0 $min_id > $id
Truth table100%3/3
A or B
A= $min_id < 0B= $min_id > $id
ABresult
1X1
011
000
169 {
170 1144 $min_id = $id;
171 1144 $min_p = $p;
172 }
173 }
174  
175 1017 return ($min_id, $min_p);
176 }
177  
178 sub _try_to_form_n
179 {
180 444 ▶ my $self = shift;
Subroutine _try_to_form_n: called CC=3 SCAR=11.0
181  
182 444 ▶ while (! $self->_n_vec->is_square())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_n_vec->is_square) 573 46 619
183 {
184 # Calculating $id as the minimal ID of the squaring factors of $p
185 573 my ($id, undef) = $self->_get_min_id($self->_n_vec);
186  
187 # Multiply by the controlling vector of this ID if it exists
188 # or terminate if it doesn't.
189 573 ▶ return 0 if (!defined($self->_base->[$id]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_base->[$id] 398 175 573
190 175 $self->_n_vec->mult_by($self->_base->[$id]);
191 }
192  
193 46 return 1;
194 }
195  
196 sub _get_final_factors
197 {
198 100 ▶ my $self = shift;
Subroutine _get_final_factors: called CC=3 SCAR=11.0
199  
200 100 $self->_calc_n_sq_factors();
201  
202 # The graham number of a perfect square is itself.
203 100 ▶ if ($self->_n_sq_factors->is_square())
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_n_sq_factors->is_square) { } 10 90 100
elsif (defined(my $ret = $self->_check_largest_factor_in_between)) { } 44 46 90
204 {
205 10 return $self->_n_sq_factors->_get_ret();
206 }
207 elsif (defined(my $ret = $self->_check_largest_factor_in_between()))
208 {
209 44 return $ret;
210 }
211 else
212 {
213 46 return $self->_main_solve();
214 }
215 }
216  
217 sub solve
218 {
219 100 ▶ my $self = shift;
Subroutine solve: called CC=1 SCAR=0.0
Pod documented
220  
221 100 return { factors => $self->_get_final_factors() };
222 }
223  
224 sub _main_init
225 {
226 46 ▶ my $self = shift;
Subroutine _main_init: called CC=2 SCAR=6.9
227  
228 46 $self->next_id(0);
229  
230 46 $self->_base([]);
231  
232 # Register all the primes in the squaring factors of $n
233 46 foreach my $p (@{$self->_n_sq_factors->factors()})
234 {
235 78 $self->_register_prime($p);
236 }
237  
238 # $self->_n_vec is used to determine if $n can be composed out of the
239 # base's vectors.
240 46 $self->_n_vec($self->_n_sq_factors->clone());
241  
242 46 return;
243 }
244  
245  
246 sub _update_base
247 {
248 444 ▶ my ($self, $final_vec) = @_;
Subroutine _update_base: called CC=6 SCAR=17.9
249  
250 # Get the minimal ID and its corresponding prime number
251 # in $final_vec.
252 444 my ($min_id, $min_p) = $self->_get_min_id($final_vec);
253  
254 444 ▶ if ($min_id >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($min_id >= 0) 411 33 444
255 {
256 # Assign $final_vec as the controlling vector for this prime
257 # number
258 411 $self->_base->[$min_id] = $final_vec;
259 # Canonicalize the rest of the vectors with the new vector.
260 CANON_LOOP:
261 411 ▶ for(my $j=0;$j<scalar(@{$self->_base()});$j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$self->_base;}) 3161 411 3572
262 {
263 3161 ▶ if (($j == $min_id) || (! defined($self->_base->[$j])))
Branch: 1 branch
BranchTrueFalseTotal
if ($j == $min_id or not defined $self->_base->[$j]) 1311 1850 3161
Condition100%3/3
$j == $min_id or not defined $self->_base->[$j]
l!l&&r!l&&!r
4119001850
MC/DC100%2/2
$j == $min_id or not defined $self->_base->[$j]
$j == $min_id not defined $self->_base->[$j]
Truth table100%3/3
A or not(B)
A= $j == $min_idB= not defined $self->_base->[$j]
ABresult
1X1
011
000
264 {
265 1311 next CANON_LOOP;
266 }
267 1850 ▶ if ($self->_base->[$j]->exists($min_p))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_base->[$j]->exists($min_p)) 414 1436 1850
268 {
269 414 $self->_base->[$j]->mult_by($final_vec);
270 }
271 }
272 }
273 }
274  
275 sub _get_final_composition
276 {
277 444 ▶ my ($self, $i_vec) = @_;
Subroutine _get_final_composition: called CC=4 SCAR=13.9
278  
279 # $final_vec is the new vector to add after it was
280 # stair-shaped by all the controlling vectors in the base.
281  
282 444 my $final_vec = $i_vec;
283  
284 444 foreach my $p (@{$i_vec->factors()})
285 {
286 819 ▶ if (!$self->_prime_exists($p))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_prime_exists($p)) { } 338 481 819
287 {
288 338 $self->_register_prime($p);
289 }
290 else
291 {
292 481 my $id = $self->_get_prime_id($p);
293 481 ▶ if (defined($self->_base->[$id]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_base->[$id]) 387 94 481
294 {
295 387 $final_vec->mult_by($self->_base->[$id]);
296 }
297 }
298 }
299  
300 444 return $final_vec;
301 }
302  
303 sub _get_i_vec
304 {
305 645 ▶ my ($self, $i) = @_;
Subroutine _get_i_vec: called CC=4 SCAR=13.9
306  
307 645 my $i_vec = $self->_get_num_dipole($i);
308 # Skip perfect squares - they do not add to the solution
309 645 ▶ if ($i_vec->is_square())
Branch: 1 branch
BranchTrueFalseTotal
if ($i_vec->is_square) 45 600 645
310 {
311 45 return;
312 }
313  
314 # Check if $i is a prime number
315 # We need n > 2 because for n == 2 it does include a prime number.
316 #
317 # Prime numbers cannot be included because 2*n is an upper bound
318 # to G(n) and so if there is a prime p > n than its next multiple
319 # will be greater than G(n).
320 600 ▶ if (($self->n() > 2) && ($i_vec->first() == $i))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->n > 2 and $i_vec->first == $i) 156 444 600
Condition66%2/3
$self->n > 2 and $i_vec->first == $i
!ll&&!rl&&r
0444156
MC/DC50%1/2
$self->n > 2 and $i_vec->first == $i
$self->n > 2 $i_vec->first == $i
Truth table66%2/3
A and B
A= $self->n > 2B= $i_vec->first == $i
ABresult
0X0
100
111
321 {
322 156 return;
323 }
324  
325 444 return $i_vec;
326 }
327  
328 sub _solve_iteration
329 {
330 645 ▶ my ($self, $i) = @_;
Subroutine _solve_iteration: called CC=3 SCAR=11.0
331  
332 645 ▶ my $i_vec = $self->_get_i_vec($i)
Branch: 1 branch
BranchTrueFalseTotal
my $i_vec = $self->_get_i_vec($i) or return 201 444 645
333 or return;
334  
335 444 my $final_vec = $self->_get_final_composition($i_vec);
336  
337 444 $self->_update_base($final_vec);
338  
339 # Check if we can form $n
340 444 ▶ if ($self->_try_to_form_n())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_to_form_n) { } 46 398 444
341 {
342 46 return $self->_n_vec->_get_ret();
343 }
344 else
345 {
346 398 return;
347 }
348 }
349  
350 sub _main_solve
351 {
352 46 ▶ my $self = shift;
Subroutine _main_solve: called CC=2 SCAR=6.9
353  
354 46 $self->_main_init();
355  
356 46 for(my $i=$self->n()+1;;$i++)
357 {
358 645 ▶ if (defined(my $ret = $self->_solve_iteration($i)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ret = $self->_solve_iteration($i))) 46 599 645
359 {
360 46 return $ret;
361 }
362 }
363 }
364  
365  
366 3 1; # End of Math::GrahamFunction
367  
368 __END__
↑ Summary blib/lib/Math/GrahamFunction/SqFacts.pm »