| 1 | package Math::GrahamFunction::SqFacts; | ||||||||||||||||||||||||||||
| 2 | 5 | $Math::GrahamFunction::SqFacts::VERSION = '0.02002'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use parent qw(Math::GrahamFunction::Object); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use List::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | __PACKAGE__->mk_accessors(qw(n factors)); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub _initialize | ||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 1771 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 15 | 1771 | my $args = shift; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1771 | ▶ | if ($args->{n}) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | 844 | $self->n($args->{n}); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 844 | $self->_calc_sq_factors(); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | elsif ($args->{factors}) | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | 927 | $self->factors($args->{factors}); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | else | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 0 | die "factors or n must be supplied."; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1771 | return 0; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub clone | ||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 182 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 182 | return __PACKAGE__->new({'factors' => [@{$self->factors()}]}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub _calc_sq_factors | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 844 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_calc_sq_factors: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 844 | $self->factors($self->_get_sq_facts($self->n())); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 844 | return 0; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | my %gsf_cache = (1 => []); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub _get_sq_facts | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 973 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_sq_facts: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 56 | 973 | my $n = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 973 | ▶ | if (exists($gsf_cache{$n})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 844 | return $gsf_cache{$n}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 129 | ▶ | my $start_from = shift || 2; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 2
MC/DC100%1/1
shift() || 2
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 129 | for(my $p=$start_from; ;$p++) | |||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 1720 | ▶ | if ($n % $p == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | # This function is recursive to make better use of the Memoization | ||||||||||||||||||||||||||||
| 70 | # feature. | ||||||||||||||||||||||||||||
| 71 | 129 | my $division_factors = $self->_get_sq_facts(($n / $p), $p); | |||||||||||||||||||||||||||
| 72 | 129 | ▶ | if (@$division_factors && ($division_factors->[0] == $p)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$division_factors and $division_factors->[0] == $p
MC/DC100%2/2
@$division_factors and $division_factors->[0] == $p
@$division_factors
$division_factors->[0] == $p
Truth table100%3/3
A and B
A= @$division_factorsB= $division_factors->[0] == $p
| |||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 29 | return ($gsf_cache{$n} = [ @{$division_factors}[1 .. $#$division_factors] ]); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 100 | return ($gsf_cache{$n} = [ $p, @$division_factors ]); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Removed because it is too slow - we now use our own custom memoization ( | ||||||||||||||||||||||||||||
| 85 | # or perhaps it is just called caching) | ||||||||||||||||||||||||||||
| 86 | # memoize('get_squaring_factors', 'NORMALIZER' => sub { return $_[0]; }); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # This function multiplies the squaring factors of $n and $m to receive | ||||||||||||||||||||||||||||
| 89 | # the squaring factors of ($n*$m) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # OOP-Wise, it should be a multi-method, but since we don't inherit this | ||||||||||||||||||||||||||||
| 92 | # object it's all-right. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub mult_by | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 2042 | ▶ | my $n_ref = shift; | ||||||||||||||||||||||||||
|
Subroutine
mult_by: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 2042 | my $m_ref = shift; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2042 | my @n = @{$n_ref->factors()}; | |||||||||||||||||||||||||||
| 101 | my @m = | ||||||||||||||||||||||||||||
| 102 | 2042 | eval { | |||||||||||||||||||||||||||
| 103 | 2042 | @{$m_ref->factors()}; | |||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | 2042 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | print "Hello\n"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2042 | my @ret = (); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2042 | ▶ | while (scalar(@n) && scalar(@m)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
scalar @n and scalar @m
MC/DC100%1/1
scalar @n and scalar @m
scalar @n
Truth table100%2/2
A
A= scalar @n
| |||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 4255 | ▶ | if ($n[0] == $m[0]) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 1836 | shift(@n); | |||||||||||||||||||||||||||
| 117 | 1836 | shift(@m); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | elsif ($n[0] < $m[0]) | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 1315 | push @ret, shift(@n); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else | ||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 1104 | push @ret, shift(@m); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 2042 | push @ret, @n, @m; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2042 | $n_ref->factors(\@ret); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # 0 for success | ||||||||||||||||||||||||||||
| 133 | 2042 | return 0; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub mult | ||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 90 | ▶ | my $n = shift; | ||||||||||||||||||||||||||
|
Subroutine
mult: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 90 | my $m = shift; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 90 | my $result = $n->clone(); | |||||||||||||||||||||||||||
| 143 | 90 | $result->mult_by($m); | |||||||||||||||||||||||||||
| 144 | 90 | return $result; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub is_square | ||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 1364 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_square: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 1364 | return (scalar(@{$self->factors()}) == 0); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub exists | ||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 1949 | ▶ | my ($self, $factor) = @_; | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1949 | ▶ | return defined(List::Util::first { $_ == $factor } @{$self->factors()}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub last | ||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||
| 165 | 90 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 90 | return $self->factors()->[-1]; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 5 | ▶ | use vars qw($a $b); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub product | ||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 90 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
product: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 90 | ▶ | return (List::Util::reduce { $a * $b } @{$self->factors()}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub first | ||||||||||||||||||||||||||||
| 182 | { | ||||||||||||||||||||||||||||
| 183 | 600 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 600 | return $self->factors()->[0]; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 5 | 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||