File Coverage

blib/lib/Math/NumSeq/TotientCumulative.pm
Criterion Covered Total %
statement 53 55 96.3
branch 6 8 75.0
condition n/a
subroutine 15 15 100.0
pod 4 4 100.0
total 78 82 95.1


line stmt bran cond sub pod time code
1             # Copyright 2011, 2012, 2013, 2014 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 .
17              
18              
19              
20             # Untouchables, not sum of proper divisors of any other integer
21             # p*q sum S=1+p+q
22             # so sums up to hi need factorize to (hi^2)/4
23             #
24              
25              
26             package Math::NumSeq::TotientCumulative;
27 2     2   6713 use 5.004;
  2         5  
28 2     2   7 use strict;
  2         2  
  2         37  
29              
30 2     2   6 use vars '$VERSION', '@ISA';
  2         1  
  2         90  
31             $VERSION = 72;
32 2     2   343 use Math::NumSeq;
  2         2  
  2         66  
33             @ISA = ('Math::NumSeq');
34             *_is_infinite = \&Math::NumSeq::_is_infinite;
35              
36 2     2   940 use Math::NumSeq::Totient;
  2         3  
  2         77  
37             *_totient = \&Math::NumSeq::Totient::_totient;
38              
39              
40             # uncomment this to run the ### lines
41             #use Devel::Comments;
42              
43             # use constant name => Math::NumSeq::__('Totient Cumulative');
44 2     2   9 use constant description => Math::NumSeq::__('Cumulative totient(1..n).');
  2         2  
  2         6  
45 2     2   8 use constant i_start => 0;
  2         2  
  2         62  
46 2     2   7 use constant values_min => 0;
  2         1  
  2         61  
47 2     2   6 use constant characteristic_increasing => 1;
  2         3  
  2         59  
48 2     2   5 use constant characteristic_integer => 1;
  2         3  
  2         61  
49              
50             # cf A015614 totient cumulative + 1
51 2     2   6 use constant oeis_anum => 'A002088';
  2         2  
  2         423  
52              
53             sub rewind {
54 4     4 1 373 my ($self) = @_;
55 4         19 $self->{'i'} = $self->i_start;
56 4         12 $self->{'sum'} = 0;
57             }
58             sub next {
59 24     24 1 392 my ($self) = @_;
60 24         22 my $i = $self->{'i'}++;
61 24         32 return ($i, $self->{'sum'} += _totient($i));
62             }
63              
64             sub ith {
65 39     39 1 55 my ($self, $i) = @_;
66             ### TotientCumulative ith(): $i
67              
68 39 50       46 if (_is_infinite($i)) {
69 0         0 return $i;
70             }
71 39         30 my $sum = 0;
72 39         43 foreach my $n (1 .. $i) {
73 287         348 $sum += _totient($n);
74             }
75 39         49 return $sum;
76             }
77              
78             sub pred {
79 97     97 1 289 my ($self, $value) = @_;
80             ### TotientCumulative pred(): $value
81              
82 97 50       129 if (_is_infinite($value)) {
83 0         0 return 0;
84             }
85 97         74 my $sum = 0;
86 97         73 for (my $n = 0; ; $n++) {
87 912 100       1034 if ($sum == $value) {
88 24         25 return 1;
89             }
90 888 100       922 if ($sum > $value) {
91 73         77 return 0;
92             }
93 815         939 $sum += _totient($n);
94             }
95             }
96              
97             1;
98             __END__