NumSeq/Powerful.pm

filter: Statementstmt 81.8% 54 / 66 Branchbran 57.1% 16 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 10 / 12 Podpod 100% 4 / 4 totaltotal 69.6% 87 / 125 CC 18 SCAR 25.4
CC
18
Cov
71%
description
20.0
pred
19.6
values_min
2.0
CRAP 12.7
« NumSeq/Kolakoski.pm ↑ Summary NumSeq/TotientStepsSum.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 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::Powerful;
19 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 $VERSION = 75;
25 2 ▶use Math::NumSeq 7; # v.7 for _is_infinite()
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Math::NumSeq::Base::IteratePred;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 @ISA = ('Math::NumSeq::Base::IteratePred',
28 'Math::NumSeq');
29 2 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments;
33  
34  
35 # use constant name => Math::NumSeq::__('Powerful Numbers');
36 2 ▶use constant i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 0 ▶sub description {
Subroutine description: not called CC=4 SCAR=30.0
Pod documented
39 0 my ($self) = @_;
40 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
41 return ("Integers with $self->{'powerful_type'} prime factors "
42 . ($self->{'power'} == 2 ? "squared"
43 0 ▶ : $self->{'power'} == 3 ? "cubed"
Branch: 2 branches
BranchTrueFalseTotal
$self->{'power'} == 2 ? : 0 0 0
$self->{'power'} == 3 ? : 0 0 0
44 : "$self->{'power'}th power")
45 . " or higher.");
46 } else {
47 0 return Math::NumSeq::__('Integers which some prime factors squared or higher.');
48 }
49 }
50  
51 2 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
52 [
53 { name => 'powerful_type',
54 type => 'enum',
55 default => 'some',
56 choices => ['some','all'],
57 choices_display => [Math::NumSeq::__('Some'),
58 Math::NumSeq::__('All'),
59 ],
60 # description => Math::NumSeq::__(''),
61 },
62 { name => 'power',
63 type => 'integer',
64 default => '2',
65 minimum => 2,
66 width => 2,
67 # description => Math::NumSeq::__(''),
68 },
69 2 ];
70  
71 3 ▶sub values_min {
Subroutine values_min: called CC=2 SCAR=6.9
Pod documented
72 3 my ($self) = @_;
73 return ($self->{'powerful_type'} eq 'some'
74 3 ▶ ? 2 ** $self->{'power'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'powerful_type'} eq 'some' ? : 1 2 3
75 : 1);
76 }
77  
78 # cf A168363 squares and cubes of primes, the primitives of "all" power=2
79 # A112526 0/1 charactistic of A001694 all k>=2
80 # A005934 highly powerful - new highest product of exponents
81 # A005117 the squarefrees, all k<2, complement of some k>=2
82 # A001597 perfect powers m^k for k>=2
83 # A052485 weak, non-powerful, some k<2
84 #
85 2 my %oeis_anum = (some => [undef,
86 undef,
87 'A013929', # 2 non squarefree, divisible by some p^2
88 'A046099', # 3 non cubefree, divisible by some p^3
89 'A046101', # 4 non 4th-free
90 # OEIS-Catalogue: A013929
91 # OEIS-Catalogue: A046099 power=3
92 # OEIS-Catalogue: A046101 power=4
93 ],
94 all => [undef,
95 undef,
96 'A001694', # 2 all p^k has k >= 2
97 'A036966', # 3 all p^k has k >= 3
98 'A036967', # 4 all p^k has k >= 4
99 'A069492', # 5 all p^k has k >= 5
100 'A069493', # 6
101 # OEIS-Catalogue: A001694 powerful_type=all
102 # OEIS-Catalogue: A036966 powerful_type=all power=3
103 # OEIS-Catalogue: A036967 powerful_type=all power=4
104 # OEIS-Catalogue: A069492 powerful_type=all power=5
105 # OEIS-Catalogue: A069493 powerful_type=all power=6
106 ],
107 );
108 0 ▶sub oeis_anum {
Subroutine oeis_anum: not called CC=1 SCAR=6.9
Pod documented
109 0 my ($self) = @_;
110 0 return $oeis_anum{$self->{'powerful_type'}}->[$self->{'power'}];
111 }
112  
113 1002 ▶sub pred {
Subroutine pred: called CC=14 SCAR=29.7
Pod documented
114 1002 my ($self, $value) = @_;
115 ### SquareFree pred(): $value
116  
117 1002 $value = abs($value);
118 1002 ▶ unless ($value >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value >= 0) 0 1002 1002
119 0 return 0;
120 }
121 1002 ▶ if ($value <= 0xFFFF_FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 4294967295) 1000 2 1002
122 1000 $value = "$value"; # numize Math::BigInt for speed
123 }
124  
125 1002 ▶ if ($value < 1 || $value != int($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 1 or $value != int $value) 0 1002 1002
Condition33%1/3
$value < 1 or $value != int $value
l!l&&r!l&&!r
001002
MC/DC0%0/2
$value < 1 or $value != int $value
$value < 1 $value != int $value
Truth table33%1/3
A or B
A= $value < 1B= $value != int $value
ABresult
1X1
011
000
126 0 return 0;
127 }
128  
129 1002 my $power = $self->{'power'};
130 1002 my $limit = "$value" ** (1/$power) + 3;
131 1002 my $reduced_limit = min($limit,65535);
132  
133 1002 my $p = 2;
134 1002 ▶ for ( ; $p <= $reduced_limit; $p += 2-($p==2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $reduced_limit) 2031 1002 3033
135 2031 ▶ next if ($value % $p);
Branch: 1 branch
BranchTrueFalseTotal
if $value % $p 1607 424 2031
136 ### prime factor: $p
137  
138 424 $value /= $p;
139 424 my $count = 1;
140 424 ▶ while (($value % $p) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value % $p == 0) 1306 424 1730
141 1306 ++$count;
142 1306 ▶ if ($count >= $power && $self->{'powerful_type'} eq 'some') {
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $power and $self->{"powerful_type"} eq "some") 0 1306 1306
Condition33%1/3
$count >= $power and $self->{"powerful_type"} eq "some"
!ll&&!rl&&r
013060
MC/DC0%0/2
$count >= $power and $self->{"powerful_type"} eq "some"
$count >= $power $self->{"powerful_type"} eq "some"
Truth table33%1/3
A and B
A= $count >= $powerB= $self->{"powerful_type"} eq "some"
ABresult
0X0
100
111
143 ### found some factor of desired power ...
144 0 return 1;
145 }
146 1306 $value /= $p;
147 }
148 424 ▶ if ($count < $power && $self->{'powerful_type'} ne 'some') {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $power and $self->{"powerful_type"} ne "some") 0 424 424
Condition33%1/3
$count < $power and $self->{"powerful_type"} ne "some"
!ll&&!rl&&r
42400
MC/DC0%0/2
$count < $power and $self->{"powerful_type"} ne "some"
$count < $power $self->{"powerful_type"} ne "some"
Truth table33%1/3
A and B
A= $count < $powerB= $self->{"powerful_type"} ne "some"
ABresult
0X0
100
111
149 ### all/all_prim prime without desired power ...
150 0 return 0;
151 }
152  
153 424 $limit = "$value" ** (1/$power) + 3;
154 424 $reduced_limit = min($limit,65535);
155 ### divided out: "$p, now value=$value, new limit $limit reduced $reduced_limit"
156 }
157 ### final value: $value
158  
159 1002 ▶ if ($p < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $limit) 0 1002 1002
160 ### value too big to check ...
161 0 return undef;
162 }
163 1002 ▶ if ($self->{'powerful_type'} eq 'some') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'powerful_type'} eq 'some') { } 0 1002 1002
164 ### some, no suitable power found ...
165 0 return 0;
166 } else {
167 ### all, ok if reduced to value==1 ...
168 1002 return ($value == 1);
169 }
170 }
171  
172 2 1;
173 __END__
« NumSeq/Kolakoski.pm ↑ Summary NumSeq/TotientStepsSum.pm »