File Coverage

blib/lib/Math/NumSeq/DigitCount.pm
Criterion Covered Total %
statement 71 73 97.2
branch 8 10 80.0
condition 1 3 33.3
subroutine 18 18 100.0
pod 3 3 100.0
total 101 107 94.3


line stmt bran cond sub pod time code
1             # Copyright 2010, 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             package Math::NumSeq::DigitCount;
19 1     1   13 use 5.004;
  1         2  
20 1     1   5 use strict;
  1         1  
  1         29  
21              
22 1     1   4 use vars '$VERSION', '@ISA';
  1         2  
  1         63  
23             $VERSION = 72;
24              
25 1     1   4 use Math::NumSeq;
  1         1  
  1         15  
26 1     1   3 use Math::NumSeq::Base::IterateIth;
  1         1  
  1         32  
27             @ISA = ('Math::NumSeq::Base::IterateIth',
28             'Math::NumSeq');
29             *_is_infinite = \&Math::NumSeq::_is_infinite;
30              
31 1     1   4 use Math::NumSeq::Repdigits;
  1         1  
  1         31  
32             *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh;
33              
34             # uncomment this to run the ### lines
35             #use Smart::Comments;
36              
37              
38             # use constant name => Math::NumSeq::__('Digit Count');
39 1     1   2 use constant description => Math::NumSeq::__('How many of a given digit in each number, in a given radix, for example how many 1-bits in binary.');
  1         2  
  1         2  
40 1     1   4 use constant values_min => 0;
  1         2  
  1         35  
41 1     1   4 use constant default_i_start => 0;
  1         1  
  1         33  
42 1     1   3 use constant characteristic_count => 1;
  1         0  
  1         34  
43 1     1   3 use constant characteristic_smaller => 1;
  1         1  
  1         35  
44 1     1   4 use constant characteristic_increasing => 0;
  1         1  
  1         33  
45              
46 1     1   4 use Math::NumSeq::Base::Digits;
  1         1  
  1         65  
47 1         6 use constant parameter_info_array =>
48             [
49             Math::NumSeq::Base::Digits->parameter_info_list(),
50             {
51             name => 'digit',
52             type => 'integer',
53             share_key => 'digit-1',
54             display => Math::NumSeq::__('Digit'),
55             default => -1,
56             minimum => -1,
57             width => 2,
58             description => Math::NumSeq::__('Digit to count, default -1 means radix-1.'),
59             },
60 1     1   4 ];
  1         1  
61              
62             #------------------------------------------------------------------------------
63              
64             # cf A008687 - count 1s in twos-complement -n
65             # A000788 - cumulative count 1-bits in 0 to n
66             # A033093 - total count 0s in all bases 2 to n+1
67             # A059015 - cumulative count 0-bits
68             # A000788 - cumulative count 1-bits
69             # A077266 - triangle of count 0s in bases 2 to n+1
70             # A077268 - num bases with at least one 0
71              
72             my @oeis_anum; # $oeis_anum[radix]->[digit]
73             BEGIN {
74             # cf A023416 treating "0" as a single digit zero
75 1     1   5 $oeis_anum[0]->[2]->[0] = 'A080791'; # base 2 count 0s, start i=0
76 1         1 $oeis_anum[0]->[2]->[1] = 'A000120'; # base 2 count 1s, start i=0
77             # OEIS-Catalogue: A080791 radix=2 digit=0
78             # OEIS-Catalogue: A000120 radix=2 digit=1
79              
80 1         2 $oeis_anum[1]->[3]->[0] = 'A077267'; # base 3 count 0s, start i=1
81 1         1 $oeis_anum[0]->[3]->[1] = 'A062756'; # base 3 count 1s, start i=0
82 1         1 $oeis_anum[0]->[3]->[2] = 'A081603'; # base 3 count 2s, start i=0
83             # OEIS-Catalogue: A077267 radix=3 digit=0 i_start=1
84             # OEIS-Catalogue: A062756 radix=3 digit=1
85             # OEIS-Catalogue: A081603 radix=3 digit=2
86             #
87             # cf A081602 start i=0 but considers i=0 to have 1 zero, unlike other counts
88             # $oeis_anum[0]->[3]->[0] = 'A081602'; # base 3 count 0s, start i=0
89             # # OEIS-Catalogue: A081602 radix=3 digit=0
90              
91 1         2 $oeis_anum[0]->[4]->[0] = 'A160380'; # base 4 count 0s, start i=0
92 1         1 $oeis_anum[0]->[4]->[1] = 'A160381'; # base 4 count 1s, start i=0
93 1         2 $oeis_anum[0]->[4]->[2] = 'A160382'; # base 4 count 2s, start i=0
94 1         1 $oeis_anum[0]->[4]->[3] = 'A160383'; # base 4 count 3s, start i=0
95             # OEIS-Catalogue: A160380 radix=4 digit=0
96             # OEIS-Catalogue: A160381 radix=4 digit=1
97             # OEIS-Catalogue: A160382 radix=4 digit=2
98             # OEIS-Catalogue: A160383 radix=4 digit=3
99              
100             # almost A055641 decimal count 9s, but it starts i=0 has 1 zero
101              
102 1         200 $oeis_anum[0]->[10]->[9] = 'A102683'; # base 10 count 9s, start i=0
103             # OEIS-Catalogue: A102683 radix=10 digit=9
104             }
105             sub oeis_anum {
106 3     3 1 10 my ($self) = @_;
107 3         6 my $radix = $self->{'radix'};
108 3         4 my $digit = $self->{'digit'};
109 3 100       10 if ($digit == -1) {
    50          
110 1         2 $digit = $radix-1;
111             } elsif ($digit >= $radix) {
112 0         0 return 'A000004'; # all zeros, starting i=0
113             }
114 3         6 return $oeis_anum[$self->i_start]->[$radix]->[$digit];
115             }
116              
117             sub ith {
118 640     640 1 510 my ($self, $i) = @_;
119 640         399 $i = abs($i);
120 640 50       794 if (_is_infinite ($i)) {
121 0         0 return $i;
122             }
123              
124 640         541 my $radix = $self->{'radix'};
125 640         444 my $digit = $self->{'digit'};
126 640 100       718 if ($digit == -1) { $digit = $radix - 1; }
  219         139  
127              
128 640         392 my $count = 0;
129 640         798 foreach my $got_digit (_digit_split_lowtohigh($i, $radix)) {
130 1576 100       1742 if ($got_digit == $digit) {
131 665         473 $count++;
132             }
133             }
134 640         1040 return $count;
135             }
136              
137             sub pred {
138 46     46 1 119 my ($self, $value) = @_;
139 46   33     115 return ($value >= 0 && $value==int($value));
140             }
141              
142             1;
143             __END__