File Coverage

blib/lib/Math/NumSeq/DigitCountHigh.pm
Criterion Covered Total %
statement 56 59 94.9
branch 7 10 70.0
condition 3 6 50.0
subroutine 15 15 100.0
pod 3 3 100.0
total 84 93 90.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::DigitCountHigh;
19 1     1   1002 use 5.004;
  1         3  
20 1     1   5 use strict;
  1         2  
  1         22  
21              
22 1     1   3 use vars '$VERSION', '@ISA';
  1         1  
  1         54  
23             $VERSION = 72;
24              
25 1     1   4 use Math::NumSeq;
  1         1  
  1         18  
26 1     1   3 use Math::NumSeq::Base::IterateIth;
  1         1  
  1         49  
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   3 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         3  
37 1     1   4 use constant values_min => 0;
  1         1  
  1         32  
38 1     1   7 use constant i_start => 0;
  1         2  
  1         35  
39 1     1   3 use constant characteristic_count => 1;
  1         2  
  1         32  
40 1     1   3 use constant characteristic_smaller => 1;
  1         1  
  1         35  
41 1     1   3 use constant characteristic_increasing => 0;
  1         2  
  1         42  
42              
43 1     1   618 use Math::NumSeq::DigitCount 4;
  1         11  
  1         208  
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 8 my ($self) = @_;
57 3         4 my $radix = $self->{'radix'};
58 3         3 my $digit = $self->{'digit'};
59 3 50 66     20 if ($digit == -1) {
    100          
60 0         0 $digit = $radix-1;
61             } elsif ($digit >= $radix || $digit == 0) {
62 1         2 return 'A000004'; # all zeros,
63             }
64 2         6 return $oeis_anum[$radix]->[$digit];
65             }
66              
67             sub ith {
68 712     712 1 600 my ($self, $i) = @_;
69 712         411 $i = abs($i);
70 712 50       833 if (_is_infinite($i)) {
71 0         0 return $i; # don't loop forever if $i is +infinity
72             }
73              
74 712         626 my $radix = $self->{'radix'};
75 712         473 my $digit = $self->{'digit'};
76 712 50       802 if ($digit == -1) { $digit = $radix - 1; }
  0         0  
77              
78 712         446 my $count = 0;
79 712         794 while ($i) {
80 1768 100       1618 if (($i % $radix) == $digit) {
81 718         428 $count++;
82             } else {
83 1050         666 $count = 0;
84             }
85 1768         2107 $i = int($i/$radix);
86             }
87 712         1090 return $count;
88             }
89              
90             sub pred {
91 47     47 1 175 my ($self, $value) = @_;
92 47   33     130 return ($value >= 0 && $value == int($value));
93             }
94              
95             1;
96             __END__