NumSeq/DigitProductSteps.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 83.3% 5 / 6 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 90.4% 104 / 115 CC 7 SCAR 7.7
CC
7
Cov
92%
pred
5.5
ith
4.0
BEGIN
1.0
CRAP 2.2
« NumSeq/MephistoWaltz.pm ↑ Summary NumSeq/DigitCount.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  
19 # http://www.inwap.com/pdp10/hbaker/hakmem/number.html#item56
20 # /so/hakmem/number.html
21  
22  
23 package Math::NumSeq::DigitProductSteps;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use List::Util 'reduce';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $VERSION = 75;
30  
31 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 @ISA = ('Math::NumSeq::Base::IterateIth',
34 'Math::NumSeq');
35 1 *_is_infinite = \&Math::NumSeq::_is_infinite;
36  
37 1 ▶use Math::NumSeq::Repdigits;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh;
39  
40 1 ▶use Math::NumSeq::DigitProduct;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # uncomment this to run the ### lines
43 #use Smart::Comments;
44  
45  
46 # use constant name => Math::NumSeq::__('...');
47 1 ▶use constant description => Math::NumSeq::__('Number of steps of digit product until reaching a single digit.');
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant characteristic_count => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Math::NumSeq::Base::Digits; # radix
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
55 [
56 Math::NumSeq::Base::Digits::parameter_common_radix(),
57 {
58 name => 'values_type',
59 type => 'enum',
60 share_key => 'values_type_countroot',
61 default => 'count',
62 choices => ['count',
63 'root',
64 ],
65 choices_display => [Math::NumSeq::__('Count'),
66 Math::NumSeq::__('Root'),
67 ],
68 description => Math::NumSeq::__('The values, either steps count or the final value after the steps.'),
69 1 } ];
70 1 ▶use constant values_min => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 #------------------------------------------------------------------------------
73 # cf A046511 - numbers with persistence 2
74 #
75 # A031348 - iterate product of squares of digits until 0,1
76 # A031349 - iterate product of cubes of digits
77 # A031350 -
78 # A031351
79 # A031352
80 # A031353
81 # A031354
82 # A031355 -
83 # A031356 - 10th powers of digits
84 #
85 # A087471 - iterate product of alternate digits, final digit
86 # A087472 - num steps
87 # A087473 - first of n iterations
88 # A087474 - triangle of values of those first taking n iterations
89 #
90 # A031286 - additive persistence to single digit
91 # A010888 - additive root single digit
92  
93 1 my %oeis_anum;
94  
95 1 $oeis_anum{'count'}->[10] = 'A031346';
96 1 $oeis_anum{'root'}->[10] = 'A031347';
97 # OEIS-Catalogue: A031346
98 # OEIS-Catalogue: A031347 values_type=root
99  
100 2 ▶sub oeis_anum {
Subroutine oeis_anum: called CC=1 SCAR=0.0
Pod documented
101 2 my ($self) = @_;
102 2 return $oeis_anum{$self->{'values_type'}}->[$self->{'radix'}];
103 }
104  
105 #------------------------------------------------------------------------------
106  
107 1146 ▶sub ith {
Subroutine ith: called CC=4 SCAR=13.9
Pod documented
108 1146 my ($self, $i) = @_;
109 ### ith(): $i
110  
111 1146 ▶ if (_is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 1146 1146
112 0 return $i; # don't loop forever if $i is +infinity
113 }
114  
115 1146 my $radix = $self->{'radix'};
116 1146 my $count = 0;
117 1146 for (;;) {
118 2336 my @digits = _digit_split_lowtohigh($i, $radix);
119 2336 ▶ if (@digits <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@digits <= 1) 1146 1190 2336
120 1146 ▶ if ($self->{'values_type'} eq 'count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'values_type'} eq 'count') { } 573 573 1146
121 573 return $count;
122 } else {
123 573 return $i; # final root
124 }
125 }
126 1190 ▶ $i = reduce {$a*$b} @digits;
Subroutine __ANON__: called CC=1 SCAR=0.0
127 1190 $count++;
128 }
129 }
130  
131 60 ▶sub pred {
Subroutine pred: called CC=4 SCAR=17.1
Pod documented
132 60 my ($self, $value) = @_;
133 return ($value == int($value)
134 && $value >= 0
135 && ($self->{'values_type'} eq 'count' # anything for count
136 60 ▶ || $value < $self->{'radix'})); # 0 to R-1 for root
Condition44%4/9
$value == int $value && $value >= 0 && ($self->{'values_type'} eq 'count' || $value < $self->{'radix'})
!ll&&!rl&&r
0060
$value == int $value and $value >= 0
!ll&&!rl&&r
0060
$self->{"values_type"} eq "count" or $value < $self->{"radix"}
l!l&&r!l&&!r
30300
MC/DC0%0/4
$value == int $value && $value >= 0 && ($self->{'values_type'} eq 'count' || $value < $self->{'radix'})
$value == int $value $value >= 0 $self->{"values_type"} eq "count" $value < $self->{"radix"}
Truth table40%2/5
A and B && C or D
A= $value == int $valueB= $value >= 0C= $self->{"values_type"} eq "count"D= $value < $self->{"radix"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
137 }
138  
139 1 1;
140 __END__
« NumSeq/MephistoWaltz.pm ↑ Summary NumSeq/DigitCount.pm »