NumSeq/TotientStepsSum.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 126 CC 9 SCAR 25.3
CC
9
Cov
0%
ith
42.0
values_min
6.0
oeis_anum
6.0
CRAP 12.6
1 # Copyright 2011, 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::TotientStepsSum;
19 use 5.004;
20 use strict;
21  
22 use vars '$VERSION', '@ISA';
23 $VERSION = 75;
24  
25 use Math::NumSeq;
26 use Math::NumSeq::Base::IterateIth;
27 @ISA = ('Math::NumSeq::Base::IterateIth',
28 'Math::NumSeq');
29 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 use Math::NumSeq::Totient 13;
32 *_totient = \&Math::NumSeq::Totient::_totient;
33  
34 use Math::NumSeq::PrimeFactorCount;;
35 *_prime_factors = \&Math::NumSeq::PrimeFactorCount::_prime_factors;
36  
37 # uncomment this to run the ### lines
38 #use Devel::Comments;
39  
40  
41 # use constant name => Math::NumSeq::__('Totient Steps Sum');
42 use constant description => Math::NumSeq::__('Sum of totients repeatedly applying until reach 1.');
43 use constant characteristic_increasing => 0;
44 use constant characteristic_integer => 1;
45 use constant i_start => 1;
46 use constant parameter_info_array =>
47 [ { name => 'including_self',
48 type => 'boolean',
49 display => Math::NumSeq::__('Incl Self'),
50 default => 1,
51 description => Math::NumSeq::__('Whether to include N itself in the sum.'),
52 },
53 ];
54 sub values_min {
55 my ($self) = @_;
56 return ($self->{'including_self'} ? 1 : 0);
57 }
58  
59  
60 # OEIS-Catalogue: A053478 including_self=1
61 # OEIS-Catalogue: A092693 including_self=0
62 sub oeis_anum {
63 my ($self) = @_;
64 return ($self->{'including_self'} ? 'A053478' : 'A092693');
65 }
66  
67 sub rewind {
68 my ($self) = @_;
69 $self->{'i'} = $self->i_start;
70 }
71 sub next {
72 my ($self) = @_;
73  
74 my $i = $self->{'i'}++;
75 my $sum = ($self->{'including_self'} ? $i : 0);
76 my $v = $i;
77 while ($v > 1) {
78 $sum += ($v = _totient($v));
79 }
80 return ($i, $sum);
81 }
82  
83 sub ith {
84 my ($self, $i) = @_;
85  
86 if (_is_infinite($i)) {
87 return $i;
88 }
89  
90 my ($good, @primes) = _prime_factors($i);
91 return undef unless $good;
92  
93 my %primes;
94 foreach my $p (@primes) {
95 $primes{$p}++;
96 }
97  
98 my %factors;
99 my $sum = ($self->{'including_self'} ? $i : $i*0);
100 while (%primes) {
101 ### %primes
102  
103 my %next;
104 while (my ($p, $e) = each %primes) {
105 if (--$e) {
106 $next{$p} += $e;
107 }
108 ($good, @primes) = _prime_factors($p-1);
109 return undef unless $good;
110  
111 foreach my $f (@{ $factors{$p} ||= [ @primes ] }) {
112 $next{$f}++;
113 }
114 }
115  
116 my $next_value = 1;
117 while (my ($p, $e) = each %next) {
118 $next_value *= $p ** $e;
119 }
120 $sum += $next_value;
121  
122 %primes = %next;
123 }
124 ### final sum: $sum
125  
126 return $sum;
127 }
128  
129 1;
130 __END__