File Coverage

blib/lib/Math/NumSeq/DigitCountHigh.pm
Criterion Covered Total %
statement 57 60 95.0
branch 7 10 70.0
condition 3 6 50.0
subroutine 15 15 100.0
pod 3 3 100.0
total 85 94 90.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::DigitCountHigh;
19 1     1   4400 use 5.004;
  1         4  
  1         54  
20 1     1   6 use strict;
  1         2  
  1         44  
21              
22 1     1   6 use vars '$VERSION', '@ISA';
  1         3  
  1         82  
23             $VERSION = 71;
24              
25 1     1   5 use Math::NumSeq;
  1         3  
  1         21  
26 1     1   6 use Math::NumSeq::Base::IterateIth;
  1         2  
  1         80  
27             @ISA = ('Math::NumSeq::Base::IterateIth',
28             'Math::NumSeq');
29             *_is_infinite = \&Math::NumSeq::_is_infinite;
30              
31             # uncomment this to run the ### lines
32             #use Devel::Comments;
33              
34              
35             # use constant name => Math::NumSeq::__('Digit Count High');
36 1     1   6 use constant description => Math::NumSeq::__('Count of how many of a given digit at the high end of a number, in a given radix.');
  1         2  
  1         4  
37 1     1   6 use constant values_min => 0;
  1         2  
  1         124  
38 1     1   7 use constant i_start => 0;
  1         2  
  1         43  
39 1     1   4 use constant characteristic_count => 1;
  1         2  
  1         38  
40 1     1   6 use constant characteristic_smaller => 1;
  1         2  
  1         46  
41 1     1   6 use constant characteristic_increasing => 0;
  1         2  
  1         34  
42              
43 1     1   12240 use Math::NumSeq::DigitCount 4;
  1         36  
  1         618  
44             *parameter_info_array = \&Math::NumSeq::DigitCount::parameter_info_array;
45              
46             my @oeis_anum;
47              
48             $oeis_anum[2]->[1] = 'A090996'; # leading 1 bits
49             # OEIS-Catalogue: A090996 radix=2
50              
51             # OEIS-Other: A000004 radix=2 digit=0
52             # OEIS-Other: A000004 radix=3 digit=0
53             # OEIS-Other: A000004 radix=10 digit=0
54              
55             sub oeis_anum {
56 3     3 1 18 my ($self) = @_;
57 3         8 my $radix = $self->{'radix'};
58 3         8 my $digit = $self->{'digit'};
59 3 50 66     29 if ($digit == -1) {
    100          
60 0         0 $digit = $radix-1;
61             } elsif ($digit >= $radix || $digit == 0) {
62 1         3 return 'A000004'; # all zeros,
63             }
64 2         8 return $oeis_anum[$radix]->[$digit];
65             }
66              
67             sub ith {
68 712     712 1 1154 my ($self, $i) = @_;
69 712         785 $i = abs($i);
70 712 50       1594 if (_is_infinite($i)) {
71 0         0 return $i; # don't loop forever if $i is +infinity
72             }
73              
74 712         1379 my $radix = $self->{'radix'};
75 712         1060 my $digit = $self->{'digit'};
76 712 50       1383 if ($digit == -1) { $digit = $radix - 1; }
  0         0  
77              
78 712         842 my $count = 0;
79 712         1330 while ($i) {
80 1768 100       3040 if (($i % $radix) == $digit) {
81 718         770 $count++;
82             } else {
83 1050         1229 $count = 0;
84             }
85 1768         3593 $i = int($i/$radix);
86             }
87 712         2215 return $count;
88             }
89              
90             sub pred {
91 47     47 1 235 my ($self, $value) = @_;
92 47   33     204 return ($value >= 0 && $value == int($value));
93             }
94              
95             1;
96             __END__