NumSeq/AllPrimeFactors.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 5 / 5 totaltotal 91.4% 107 / 117 CC 9 SCAR 11.1
CC
9
Cov
91%
next
7.2
pred
3.3
BEGIN
1.0
CRAP 3.0
« NumSeq/PrimeIndexPrimes.pm ↑ Summary NumSeq/DigitProduct.pm »
1 # Copyright 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq 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-NumSeq 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-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Math::NumSeq::AllPrimeFactors;
19 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Math::Factor::XS 0.39 'prime_factors'; # version 0.39 for prime_factors()
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $VERSION = 75;
26 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 @ISA = ('Math::NumSeq');
28  
29 1 ▶use Math::NumSeq 7; # v.7 for _is_infinite()
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 *_is_infinite = \&Math::NumSeq::_is_infinite;
31  
32 1 ▶use Math::NumSeq::Primes;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 # use constant name => Math::NumSeq::__('All Prime Factors');
39 1 ▶use constant description => Math::NumSeq::__('Prime factors of the integers.');
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant default_i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
44 [
45 {
46 name => 'order',
47 display => Math::NumSeq::__('Order'),
48 share_key => 'order_as',
49 type => 'enum',
50 default => 'ascending',
51 choices => ['ascending','descending'],
52 choices_display => [Math::NumSeq::__('Ascending'),
53 Math::NumSeq::__('Descending'),
54 ],
55 description => Math::NumSeq::__('Order for the digits within each integer.'),
56 },
57 { name => 'multiplicity',
58 display => Math::NumSeq::__('Multiplicity'),
59 type => 'enum',
60 choices => ['repeated','distinct'],
61 choices_display => [Math::NumSeq::__('Repeated'),
62 Math::NumSeq::__('Distinct'),
63 ],
64 default => 'repeated',
65 description => Math::NumSeq::__('Whether to include repeated prime factors, or only distinct prime factors.'),
66 },
67 {
68 name => 'on_values',
69 display => Math::NumSeq::__('On Values'),
70 type => 'enum',
71 default => 'all',
72 choices => ['all','composites','odd','even'],
73 choices_display => [Math::NumSeq::__('All'),
74 Math::NumSeq::__('Composites'),
75 Math::NumSeq::__('Odd'),
76 Math::NumSeq::__('Even')],
77 description => Math::NumSeq::__('The values to take prime factors from, either all integers or just composites or odds or evens.'),
78 },
79 1 ];
80  
81 1 my %values_min = (all => 2,
82 composites => 2,
83 odd => 3,
84 even => 2);
85 6 ▶sub values_min {
Subroutine values_min: called CC=1 SCAR=0.0
Pod documented
86 6 my ($self) = @_;
87 6 return $values_min{$self->{'on_values'}};
88 }
89  
90 #------------------------------------------------------------------------------
91 # cf A033308 - concatenate primes digits
92 # A056538 - list of all divisors
93  
94 1 my %oeis_anum;
95 1 $oeis_anum{'all'} ->{'repeated'}->{'ascending'} = 'A027746';
96 1 $oeis_anum{'composites'}->{'repeated'}->{'ascending'} = 'A109709';
97 1 $oeis_anum{'all'} ->{'distinct'}->{'ascending'} = 'A027748';
98  
99 6 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
100 6 my ($self) = @_;
101 return $oeis_anum{$self->{'on_values'}}
102 ->{$self->{'multiplicity'}}
103 6 ->{$self->{'order'}};
104 }
105  
106 #------------------------------------------------------------------------------
107 # ENHANCE-ME: find prime factors by sieve
108  
109 1 my %rewind = (all => [ n => 2-1, n_step => 1 ],
110 composites => [ n => 2-1, n_step => 1 ],
111 odd => [ n => 2-1, n_step => 2 ],
112 even => [ n => 2-2, n_step => 2 ]);
113 18 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
114 18 my ($self) = @_;
115 %$self = (%$self,
116 18 @{$rewind{$self->{'on_values'}}});
117 18 $self->{'pending'} = [ ];
118 18 $self->{'i'} = $self->i_start;
119 }
120  
121 198 ▶sub next {
Subroutine next: called CC=7 SCAR=19.8
Pod documented
122 198 my ($self) = @_;
123 ### AllDigits next(): $self->{'i'}
124  
125 198 my $value;
126 198 my $pending = $self->{'pending'};
127 198 ▶ unless (defined ($value = shift @$pending)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($value = shift @$pending)) 120 78 198
128 120 my $n = ($self->{'n'} += $self->{'n_step'});
129  
130 120 ▶ if ($self->{'on_values'} eq 'composites') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_values"} eq "composites") 0 120 120
131 0 ▶ while (is_prime($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_prime($n)) 0 0 0
132 0 $n = ++$self->{'n'};
133 }
134 }
135  
136 120 @$pending = prime_factors($n);
137 120 ▶ if ($self->{'multiplicity'} eq 'distinct') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"multiplicity"} eq "distinct") 42 78 120
138 42 my $prev = 0;
139 42 ▶ @$pending = grep { $_ == $prev ? () : ($prev=$_) } @$pending;
Branch: 1 branch
BranchTrueFalseTotal
$_ == $prev ? : 26 60 86
140 }
141 120 my $order = $self->{'order'};
142 120 ▶ if ($order eq 'descending') {
Branch: 1 branch
BranchTrueFalseTotal
if ($order eq "descending") 20 100 120
143 20 @$pending = reverse @$pending;
144 }
145 120 $value = shift @$pending;
146 }
147 198 return ($self->{'i'}++, $value);
148 }
149  
150 99 ▶sub pred {
Subroutine pred: called CC=3 SCAR=12.0
Pod documented
151 99 my ($self, $value) = @_;
152 99 ▶ if ($self->{'on_values'} eq 'odd' && $value < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_values"} eq "odd" and $value < 3) 0 99 99
Condition66%2/3
$self->{"on_values"} eq "odd" and $value < 3
!ll&&!rl&&r
89100
MC/DC0%0/2
$self->{"on_values"} eq "odd" and $value < 3
$self->{"on_values"} eq "odd" $value < 3
Truth table66%2/3
A and B
A= $self->{"on_values"} eq "odd"B= $value < 3
ABresult
0X0
100
111
153 0 return 0;
154 }
155 99 return Math::NumSeq::Primes->pred($value);
156 }
157  
158 1 1;
159 __END__
« NumSeq/PrimeIndexPrimes.pm ↑ Summary NumSeq/DigitProduct.pm »