| 1 | # Copyright 2012, 2013, 2014, 2016, 2019, 2020 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 <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | package Math::NumSeq::MaxDigitCount; | ||||||||||
| 19 | 1 | ▶ | use 5.004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use List::Util 'max'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | $VERSION = 75; | |||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | use Math::NumSeq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use Math::NumSeq::Base::IterateIth; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | @ISA = ('Math::NumSeq::Base::IterateIth', | |||||||||
| 29 | 'Math::NumSeq'); | ||||||||||
| 30 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||
| 31 | |||||||||||
| 32 | # uncomment this to run the ### lines | ||||||||||
| 33 | #use Smart::Comments; | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | # use constant name => Math::NumSeq::__('...'); | ||||||||||
| 37 | 1 | ▶ | use constant description => Math::NumSeq::__('Maximum count of a given digit in any radix.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ▶ | use constant default_i_start => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 1 | ▶ | use constant parameter_info_array => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | [ | ||||||||||
| 41 | { | ||||||||||
| 42 | name => 'digit', | ||||||||||
| 43 | share_key => 'digit_0', | ||||||||||
| 44 | type => 'integer', | ||||||||||
| 45 | display => Math::NumSeq::__('Digit'), | ||||||||||
| 46 | default => 0, | ||||||||||
| 47 | minimum => 0, | ||||||||||
| 48 | width => 2, | ||||||||||
| 49 | description => Math::NumSeq::__('Digit to count.'), | ||||||||||
| 50 | }, | ||||||||||
| 51 | { | ||||||||||
| 52 | name => 'values_type', | ||||||||||
| 53 | share_key => 'values_type_cr', | ||||||||||
| 54 | type => 'enum', | ||||||||||
| 55 | default => 'count', | ||||||||||
| 56 | choices => ['count','radix'], | ||||||||||
| 57 | choices_display => [Math::NumSeq::__('Count'), | ||||||||||
| 58 | Math::NumSeq::__('Radix')], | ||||||||||
| 59 | description => Math::NumSeq::__('Whether to give the digit count or the radix the count occurs in.'), | ||||||||||
| 60 | }, | ||||||||||
| 61 | 1 | ]; | |||||||||
| 62 | |||||||||||
| 63 | 4 | ▶ | sub characteristic_count { | ||||||||
|
Subroutine
characteristic_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | 4 | my ($self) = @_; | |||||||||
| 65 | 4 | return $self->{'values_type'} eq 'count'; | |||||||||
| 66 | } | ||||||||||
| 67 | 0 | ▶ | sub characteristic_value_is_radix { | ||||||||
|
Subroutine
characteristic_value_is_radix: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||
| 69 | 0 | return $self->{'values_type'} eq 'radix'; | |||||||||
| 70 | } | ||||||||||
| 71 | 1 | ▶ | use constant characteristic_smaller => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | ▶ | use constant characteristic_integer => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 4 | ▶ | sub values_min { | ||||||||
|
Subroutine
values_min: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 75 | 4 | my ($self) = @_; | |||||||||
| 76 | 4 | ▶ | if ($self->{'values_type'} eq 'count') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 2 | ▶ | if ($self->{'digit'} == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 1 | return 1; | |||||||||
| 79 | } | ||||||||||
| 80 | } else { # values_type=='radix' | ||||||||||
| 81 | 2 | return 2; | |||||||||
| 82 | } | ||||||||||
| 83 | 1 | return 0; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | #------------------------------------------------------------------------------ | ||||||||||
| 87 | # cf A033093 number of zeros in triangle of base 2 to n+1 | ||||||||||
| 88 | |||||||||||
| 89 | 1 | my %oeis_anum; | |||||||||
| 90 | 1 | $oeis_anum{'count'}->[0] = 'A062842'; # max 0s count | |||||||||
| 91 | 1 | $oeis_anum{'count'}->[1] = 'A062843'; # max 1s count | |||||||||
| 92 | # OEIS-Catalogue: A062842 | ||||||||||
| 93 | # OEIS-Catalogue: A062843 digit=1 | ||||||||||
| 94 | 4 | ▶ | sub oeis_anum { | ||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 4 | my ($self) = @_; | |||||||||
| 96 | 4 | return $oeis_anum{$self->{'values_type'}}->[$self->{'digit'}]; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | #------------------------------------------------------------------------------ | ||||||||||
| 100 | |||||||||||
| 101 | 432 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 102 | 432 | my ($self, $i) = @_; | |||||||||
| 103 | ### MaxDigitCount ith(): $i | ||||||||||
| 104 | |||||||||||
| 105 | 432 | ▶ | if (_is_infinite($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | return $i; | |||||||||
| 107 | } | ||||||||||
| 108 | 432 | $i = abs($i); | |||||||||
| 109 | |||||||||||
| 110 | 432 | my $digit = $self->{'digit'}; | |||||||||
| 111 | 432 | my $found_count = 0; | |||||||||
| 112 | 432 | my $found_radix = 2; | |||||||||
| 113 | 432 | foreach my $radix (2 .. max($i,2)) { | |||||||||
| 114 | 1900 | my $digits = _digit_split($i,$radix); # low to high | |||||||||
| 115 | |||||||||||
| 116 | ### $radix | ||||||||||
| 117 | ### $digits | ||||||||||
| 118 | |||||||||||
| 119 | 1900 | ▶ | if (@$digits < $found_count) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 8 | last; # fewer digits now than already found | |||||||||
| 121 | } | ||||||||||
| 122 | 1892 | my $count = grep {$_ == $digit} @$digits; | |||||||||
| 123 | 1892 | ▶ | if ($count > $found_count) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 460 | $found_count = $count; | |||||||||
| 125 | 460 | $found_radix = $radix; | |||||||||
| 126 | |||||||||||
| 127 | # all "ddddd" digits, is the maximum possible | ||||||||||
| 128 | # or "X0000" when digit=0 is the maximum possible | ||||||||||
| 129 | 460 | ▶ | if ($count == scalar(@$digits) - ($digit==0)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 194 | last; | |||||||||
| 131 | } | ||||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | 432 | ▶ | return ($self->{'values_type'} eq 'radix' ? $found_radix : $found_count); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 1900 | ▶ | sub _digit_split { | ||||||||
|
Subroutine
_digit_split: called CC=2 SCAR=6.9
| |||||||||||
| 138 | 1900 | my ($n, $radix) = @_; | |||||||||
| 139 | # ### _digit_split(): $n | ||||||||||
| 140 | 1900 | my @ret; | |||||||||
| 141 | 1900 | ▶ | while ($n) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 4484 | push @ret, $n % $radix; | |||||||||
| 143 | 4484 | $n = int($n/$radix); | |||||||||
| 144 | } | ||||||||||
| 145 | 1900 | return \@ret; # array[0] low digit | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 40 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||
| 149 | 40 | my ($self, $value) = @_; | |||||||||
| 150 | 40 | ▶ | unless ($value == int($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | return 0; | |||||||||
| 152 | } | ||||||||||
| 153 | 40 | ▶ | if ($self->{'values_type'} eq 'count') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 20 | ▶ | if ($self->{'digit'} == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 10 | return ($value >= 1); | |||||||||
| 156 | } | ||||||||||
| 157 | } else { # values_type=='radix' | ||||||||||
| 158 | 20 | ▶ | if ($value == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 0 | return 0; | |||||||||
| 160 | } | ||||||||||
| 161 | } | ||||||||||
| 162 | 30 | return ($value >= 0); | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 1 | 1; | |||||||||
| 166 | __END__ | ||||||||||