File Coverage

blib/lib/Math/NumSeq/DigitCount.pm
Criterion Covered Total %
statement 72 74 97.3
branch 8 10 80.0
condition 1 3 33.3
subroutine 18 18 100.0
pod 3 3 100.0
total 102 108 94.4


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   45 use 5.004;
  1         7  
  1         110  
20 1     1   14 use strict;
  1         5  
  1         182  
21              
22 1     1   9 use vars '$VERSION', '@ISA';
  1         4  
  1         147  
23             $VERSION = 71;
24              
25 1     1   10 use Math::NumSeq;
  1         3  
  1         28  
26 1     1   8 use Math::NumSeq::Base::IterateIth;
  1         2  
  1         84  
27             @ISA = ('Math::NumSeq::Base::IterateIth',
28             'Math::NumSeq');
29             *_is_infinite = \&Math::NumSeq::_is_infinite;
30              
31 1     1   10 use Math::NumSeq::Repdigits;
  1         6  
  1         143  
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   11 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         4  
  1         12  
40 1     1   10 use constant values_min => 0;
  1         4  
  1         81  
41 1     1   8 use constant default_i_start => 0;
  1         4  
  1         67  
42 1     1   9 use constant characteristic_count => 1;
  1         2  
  1         96  
43 1     1   8 use constant characteristic_smaller => 1;
  1         4  
  1         157  
44 1     1   19 use constant characteristic_increasing => 0;
  1         2  
  1         79  
45              
46 1     1   9 use Math::NumSeq::Base::Digits;
  1         4  
  1         144  
47 1         9 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   8 ];
  1         4  
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   7 $oeis_anum[0]->[2]->[0] = 'A080791'; # base 2 count 0s, start i=0
76 1         3 $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         4 $oeis_anum[1]->[3]->[0] = 'A077267'; # base 3 count 0s, start i=1
81 1         4 $oeis_anum[0]->[3]->[1] = 'A062756'; # base 3 count 1s, start i=0
82 1         3 $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         4 $oeis_anum[0]->[4]->[0] = 'A160380'; # base 4 count 0s, start i=0
92 1         3 $oeis_anum[0]->[4]->[1] = 'A160381'; # base 4 count 1s, start i=0
93 1         4 $oeis_anum[0]->[4]->[2] = 'A160382'; # base 4 count 2s, start i=0
94 1         3 $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         584 $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 19 my ($self) = @_;
107 3         9 my $radix = $self->{'radix'};
108 3         11 my $digit = $self->{'digit'};
109 3 100       19 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         13 return $oeis_anum[$self->i_start]->[$radix]->[$digit];
115             }
116              
117             sub ith {
118 640     640 1 1231 my ($self, $i) = @_;
119 640         773 $i = abs($i);
120 640 50       1518 if (_is_infinite ($i)) {
121 0         0 return $i;
122             }
123              
124 640         1464 my $radix = $self->{'radix'};
125 640         934 my $digit = $self->{'digit'};
126 640 100       1345 if ($digit == -1) { $digit = $radix - 1; }
  219         282  
127              
128 640         705 my $count = 0;
129 640         1747 foreach my $got_digit (_digit_split_lowtohigh($i, $radix)) {
130 1576 100       3412 if ($got_digit == $digit) {
131 665         1301 $count++;
132             }
133             }
134 640         2560 return $count;
135             }
136              
137             sub pred {
138 46     46 1 490 my ($self, $value) = @_;
139 46   33     218 return ($value >= 0 && $value==int($value));
140             }
141              
142             1;
143             __END__