GrahamFunction.pm

filter: Statementstmt 100% 131 / 131 Branchbran 94.7% 36 / 38 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 26 / 26 Podpod 100% 1 / 1 totaltotal 98.1% 207 / 211 CC 27 SCAR 10.6
CC
27
Cov
98%
_update_base
6.0
_get_i_vec
4.0
_get_min_id
4.0
CRAP 2.9
« GrahamFunction/SqFacts.pm ↑ Summary GrahamFunction/Object.pm »
1 package Math::GrahamFunction;
2 2 $Math::GrahamFunction::VERSION = '0.02004';
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 2 ▶use parent qw(Math::GrahamFunction::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Math::GrahamFunction::SqFacts;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Math::GrahamFunction::SqFacts::Dipole;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 __PACKAGE__->mk_accessors(
15 qw(
16 _base
17 n
18 _n_vec
19 next_id
20 _n_sq_factors
21 primes_to_ids_map
22 )
23 );
24  
25 sub _initialize
26 100 ▶{
Subroutine _initialize: called CC=2 SCAR=7.0
27 100 my $self = shift;
28 100 my $args = shift;
29  
30 $self->n( $args->{n} )
31 100 ▶ or die "n was not specified";
Branch: 1 branch
BranchTrueFalseTotal
$self->n($args->{"n"}) or die "n was not specified" 100 0 100
32  
33 100 $self->primes_to_ids_map( {} );
34  
35 100 return 0;
36 }
37  
38  
39 sub _get_num_facts
40 844 ▶{
Subroutine _get_num_facts: called CC=1 SCAR=0.0
41 844 my ( $self, $number ) = @_;
42  
43 844 return Math::GrahamFunction::SqFacts->new( { 'n' => $number } );
44 }
45  
46 sub _get_facts
47 745 ▶{
Subroutine _get_facts: called CC=2 SCAR=7.2
48 745 my ( $self, $factors ) = @_;
49  
50 745 ▶ return Math::GrahamFunction::SqFacts->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $factors eq 'ARRAY' ? : 0 745 745
51 { 'factors' => ( ref($factors) eq "ARRAY" ? $factors : [$factors] ) } );
52 }
53  
54 sub _get_num_dipole
55 745 ▶{
Subroutine _get_num_dipole: called CC=1 SCAR=0.0
56 745 my ( $self, $number ) = @_;
57  
58 745 return Math::GrahamFunction::SqFacts::Dipole->new(
59 {
60 'result' => $self->_get_num_facts($number),
61 'compose' => $self->_get_facts($number),
62 }
63 );
64  
65 }
66  
67 sub _calc_n_sq_factors
68 100 ▶{
Subroutine _calc_n_sq_factors: called CC=1 SCAR=0.0
69 100 my $self = shift;
70  
71 100 $self->_n_sq_factors( $self->_get_num_dipole( $self->n ) );
72 }
73  
74 sub _check_largest_factor_in_between
75 90 ▶{
Subroutine _check_largest_factor_in_between: called CC=3 SCAR=11.0
76 90 my $self = shift;
77  
78 90 my $n = $self->n();
79  
80 # Cheating:
81 # Check if between n and n+largest_factor we can fit
82 # a square of SqFact{n*(n+largest_factor)}. If so, return
83 # n+largest_factor.
84 #
85 # So, for instance, if n = p than n+largest_factor = 2p
86 # and so SqFact{p*(2p)} = 2 and it is possible to see if
87 # there's a 2*i^2 between p and 2p. That way, p*2*i^2*2p is
88 # a square number.
89  
90 90 my $largest_factor = $self->_n_sq_factors()->last();
91  
92 90 my ( $lower_bound, $lb_sq_factors );
93  
94 90 $lower_bound = $self->n() + $largest_factor;
95 90 while (1)
96 {
97 99 $lb_sq_factors = $self->_get_num_facts($lower_bound);
98 99 ▶ if ( $lb_sq_factors->exists($largest_factor) )
Branch: 1 branch
BranchTrueFalseTotal
if ($lb_sq_factors->exists($largest_factor)) 90 9 99
99 {
100 90 last;
101 }
102 9 $lower_bound += $largest_factor;
103 }
104  
105 90 my $n_times_lb = $self->_n_sq_factors->result->mult($lb_sq_factors);
106  
107 90 my $rest_of_factors_product = $n_times_lb->product();
108  
109 90 my $low_square_val = int( sqrt( $n / $rest_of_factors_product ) );
110 90 my $high_square_val =
111 int( sqrt( $lower_bound / $rest_of_factors_product ) );
112  
113 90 ▶ if ( $low_square_val != $high_square_val )
Branch: 1 branch
BranchTrueFalseTotal
if ($low_square_val != $high_square_val) { } 44 46 90
114 {
115 44 my @factors = (
116 $n,
117 ( $low_square_val + 1 ) *
118 ( $low_square_val + 1 ) *
119 $rest_of_factors_product,
120 $lower_bound
121 );
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 416 ▶{
Subroutine _get_next_id: called CC=1 SCAR=0.0
135 416 my $self = shift;
136 416 return $self->next_id( $self->next_id() + 1 );
137 }
138  
139 sub _get_prime_id
140 2118 ▶{
Subroutine _get_prime_id: called CC=1 SCAR=0.0
141 2118 my $self = shift;
142 2118 my $p = shift;
143 2118 return $self->primes_to_ids_map()->{$p};
144 }
145  
146 sub _register_prime
147 416 ▶{
Subroutine _register_prime: called CC=1 SCAR=0.0
148 416 my ( $self, $p ) = @_;
149 416 $self->primes_to_ids_map()->{$p} = $self->_get_next_id();
150 }
151  
152 sub _prime_exists
153 819 ▶{
Subroutine _prime_exists: called CC=1 SCAR=0.0
154 819 my ( $self, $p ) = @_;
155 819 return exists( $self->primes_to_ids_map->{$p} );
156 }
157  
158 sub _get_min_id
159 1017 ▶{
Subroutine _get_min_id: called CC=4 SCAR=13.9
160 1017 my ( $self, $vec ) = @_;
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 444 ▶{
Subroutine _try_to_form_n: called CC=3 SCAR=11.0
180 444 my $self = shift;
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 100 ▶{
Subroutine _get_final_factors: called CC=3 SCAR=11.0
198 100 my $self = shift;
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 100 ▶{
Subroutine solve: called CC=1 SCAR=0.0
Pod documented
219 100 my $self = shift;
220  
221 100 return { factors => $self->_get_final_factors() };
222 }
223  
224 sub _main_init
225 46 ▶{
Subroutine _main_init: called CC=2 SCAR=6.9
226 46 my $self = shift;
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 444 ▶{
Subroutine _update_base: called CC=6 SCAR=17.9
248 444 my ( $self, $final_vec ) = @_;
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  
260 # Canonicalize the rest of the vectors with the new vector.
261 CANON_LOOP:
262 411 for my $j ( keys @{ $self->_base } )
263 {
264 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
265 {
266 1311 next CANON_LOOP;
267 }
268 1850 ▶ if ( $self->_base->[$j]->exists($min_p) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_base->[$j]->exists($min_p)) 414 1436 1850
269 {
270 414 $self->_base->[$j]->mult_by($final_vec);
271 }
272 }
273 }
274 }
275  
276 sub _get_final_composition
277 444 ▶{
Subroutine _get_final_composition: called CC=4 SCAR=13.9
278 444 my ( $self, $i_vec ) = @_;
279  
280 # $final_vec is the new vector to add after it was
281 # stair-shaped by all the controlling vectors in the base.
282  
283 444 my $final_vec = $i_vec;
284  
285 444 foreach my $p ( @{ $i_vec->factors() } )
286 {
287 819 ▶ if ( !$self->_prime_exists($p) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_prime_exists($p)) { } 338 481 819
288 {
289 338 $self->_register_prime($p);
290 }
291 else
292 {
293 481 my $id = $self->_get_prime_id($p);
294 481 ▶ if ( defined( $self->_base->[$id] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_base->[$id]) 387 94 481
295 {
296 387 $final_vec->mult_by( $self->_base->[$id] );
297 }
298 }
299 }
300  
301 444 return $final_vec;
302 }
303  
304 sub _get_i_vec
305 645 ▶{
Subroutine _get_i_vec: called CC=4 SCAR=13.9
306 645 my ( $self, $i ) = @_;
307  
308 645 my $i_vec = $self->_get_num_dipole($i);
309  
310 # Skip perfect squares - they do not add to the solution
311 645 ▶ if ( $i_vec->is_square() )
Branch: 1 branch
BranchTrueFalseTotal
if ($i_vec->is_square) 45 600 645
312 {
313 45 return;
314 }
315  
316 # Check if $i is a prime number
317 # We need n > 2 because for n == 2 it does include a prime number.
318 #
319 # Prime numbers cannot be included because 2*n is an upper bound
320 # to G(n) and so if there is a prime p > n than its next multiple
321 # will be greater than G(n).
322 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
323 {
324 156 return;
325 }
326  
327 444 return $i_vec;
328 }
329  
330 sub _solve_iteration
331 645 ▶{
Subroutine _solve_iteration: called CC=3 SCAR=11.0
332 645 my ( $self, $i ) = @_;
333  
334 645 ▶ my $i_vec = $self->_get_i_vec($i)
Branch: 1 branch
BranchTrueFalseTotal
my $i_vec = $self->_get_i_vec($i) or return 444 201 645
335 or return;
336  
337 444 my $final_vec = $self->_get_final_composition($i_vec);
338  
339 444 $self->_update_base($final_vec);
340  
341 # Check if we can form $n
342 444 ▶ if ( $self->_try_to_form_n() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_to_form_n) { } 46 398 444
343 {
344 46 return $self->_n_vec->_get_ret();
345 }
346 else
347 {
348 398 return;
349 }
350 }
351  
352 sub _main_solve
353 46 ▶{
Subroutine _main_solve: called CC=2 SCAR=6.9
354 46 my $self = shift;
355  
356 46 $self->_main_init();
357  
358 46 for ( my $i = $self->n() + 1 ; ; ++$i )
359 {
360 645 ▶ if ( defined( my $ret = $self->_solve_iteration($i) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ret = $self->_solve_iteration($i))) 46 599 645
361 {
362 46 return $ret;
363 }
364 }
365 }
366  
367  
368 2 1; # End of Math::GrahamFunction
369  
370 __END__
« GrahamFunction/SqFacts.pm ↑ Summary GrahamFunction/Object.pm »