| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2017, 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::DigitCount; | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 22 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 1 | $VERSION = 75; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 1 | ▶ | use Math::NumSeq::Base::IterateIth; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 1 | @ISA = ('Math::NumSeq::Base::IterateIth', | |||||||||||||||||||
| 28 | 'Math::NumSeq'); | ||||||||||||||||||||
| 29 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | ▶ | use Math::NumSeq::Repdigits; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 1 | *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # use constant name => Math::NumSeq::__('Digit Count'); | ||||||||||||||||||||
| 36 | 1 | ▶ | 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.'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | ▶ | use constant values_min => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | ▶ | use constant default_i_start => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 1 | ▶ | use constant characteristic_count => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 1 | ▶ | use constant characteristic_smaller => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 1 | ▶ | use constant characteristic_increasing => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | ▶ | use Math::NumSeq::Base::Digits; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 1 | ▶ | use constant parameter_info_array => | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | [ | ||||||||||||||||||||
| 46 | Math::NumSeq::Base::Digits->parameter_info_list(), | ||||||||||||||||||||
| 47 | { | ||||||||||||||||||||
| 48 | name => 'digit', | ||||||||||||||||||||
| 49 | type => 'integer', | ||||||||||||||||||||
| 50 | share_key => 'digit-1', | ||||||||||||||||||||
| 51 | display => Math::NumSeq::__('Digit'), | ||||||||||||||||||||
| 52 | default => -1, | ||||||||||||||||||||
| 53 | minimum => -1, | ||||||||||||||||||||
| 54 | width => 2, | ||||||||||||||||||||
| 55 | description => Math::NumSeq::__('Digit to count, default -1 means radix-1.'), | ||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||
| 57 | 1 | ]; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # cf A008687 - count 1s in twos-complement -n | ||||||||||||||||||||
| 62 | # A000788 - cumulative count 1-bits in 0 to n | ||||||||||||||||||||
| 63 | # A033093 - total count 0s in all bases 2 to n+1 | ||||||||||||||||||||
| 64 | # A059015 - cumulative count 0-bits | ||||||||||||||||||||
| 65 | # A000788 - cumulative count 1-bits | ||||||||||||||||||||
| 66 | # A077266 - triangle of count 0s in bases 2 to n+1 | ||||||||||||||||||||
| 67 | # A077268 - num bases with at least one 0 | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | my @oeis_anum; # $oeis_anum[radix]->[digit] | |||||||||||||||||||
| 70 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 71 | # cf A023416 treating "0" as a single digit zero | ||||||||||||||||||||
| 72 | 1 | $oeis_anum[0]->[2]->[0] = 'A080791'; # base 2 count 0s, start i=0 | |||||||||||||||||||
| 73 | 1 | $oeis_anum[0]->[2]->[1] = 'A000120'; # base 2 count 1s, start i=0 | |||||||||||||||||||
| 74 | # OEIS-Catalogue: A080791 radix=2 digit=0 | ||||||||||||||||||||
| 75 | # OEIS-Catalogue: A000120 radix=2 digit=1 | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | $oeis_anum[1]->[3]->[0] = 'A077267'; # base 3 count 0s, start i=1 | |||||||||||||||||||
| 78 | 1 | $oeis_anum[0]->[3]->[1] = 'A062756'; # base 3 count 1s, start i=0 | |||||||||||||||||||
| 79 | 1 | $oeis_anum[0]->[3]->[2] = 'A081603'; # base 3 count 2s, start i=0 | |||||||||||||||||||
| 80 | # OEIS-Catalogue: A077267 radix=3 digit=0 i_start=1 | ||||||||||||||||||||
| 81 | # OEIS-Catalogue: A062756 radix=3 digit=1 | ||||||||||||||||||||
| 82 | # OEIS-Catalogue: A081603 radix=3 digit=2 | ||||||||||||||||||||
| 83 | # | ||||||||||||||||||||
| 84 | # cf A081602 start i=0 but considers i=0 to have 1 zero, unlike other counts | ||||||||||||||||||||
| 85 | # $oeis_anum[0]->[3]->[0] = 'A081602'; # base 3 count 0s, start i=0 | ||||||||||||||||||||
| 86 | # # OEIS-Catalogue: A081602 radix=3 digit=0 | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 1 | $oeis_anum[0]->[4]->[0] = 'A160380'; # base 4 count 0s, start i=0 | |||||||||||||||||||
| 89 | 1 | $oeis_anum[0]->[4]->[1] = 'A160381'; # base 4 count 1s, start i=0 | |||||||||||||||||||
| 90 | 1 | $oeis_anum[0]->[4]->[2] = 'A160382'; # base 4 count 2s, start i=0 | |||||||||||||||||||
| 91 | 1 | $oeis_anum[0]->[4]->[3] = 'A160383'; # base 4 count 3s, start i=0 | |||||||||||||||||||
| 92 | # OEIS-Catalogue: A160380 radix=4 digit=0 | ||||||||||||||||||||
| 93 | # OEIS-Catalogue: A160381 radix=4 digit=1 | ||||||||||||||||||||
| 94 | # OEIS-Catalogue: A160382 radix=4 digit=2 | ||||||||||||||||||||
| 95 | # OEIS-Catalogue: A160383 radix=4 digit=3 | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # almost A055641 decimal count 9s, but it starts i=0 has 1 zero | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 1 | $oeis_anum[0]->[10]->[9] = 'A102683'; # base 10 count 9s, start i=0 | |||||||||||||||||||
| 100 | # OEIS-Catalogue: A102683 radix=10 digit=9 | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 3 | ▶ | sub oeis_anum { | ||||||||||||||||||
|
Subroutine
oeis_anum: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||
| 103 | 3 | my ($self) = @_; | |||||||||||||||||||
| 104 | 3 | my $radix = $self->{'radix'}; | |||||||||||||||||||
| 105 | 3 | my $digit = $self->{'digit'}; | |||||||||||||||||||
| 106 | 3 | ▶ | if ($digit == -1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 107 | 1 | $digit = $radix-1; | |||||||||||||||||||
| 108 | } elsif ($digit >= $radix) { | ||||||||||||||||||||
| 109 | 0 | return 'A000004'; # all zeros, starting i=0 | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 3 | return $oeis_anum[$self->i_start]->[$radix]->[$digit]; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 640 | ▶ | sub ith { | ||||||||||||||||||
|
Subroutine
ith: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 115 | 640 | my ($self, $i) = @_; | |||||||||||||||||||
| 116 | 640 | $i = abs($i); | |||||||||||||||||||
| 117 | 640 | ▶ | if (_is_infinite ($i)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | return $i; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 640 | my $radix = $self->{'radix'}; | |||||||||||||||||||
| 122 | 640 | my $digit = $self->{'digit'}; | |||||||||||||||||||
| 123 | 640 | ▶ | if ($digit == -1) { $digit = $radix - 1; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 640 | my $count = 0; | |||||||||||||||||||
| 126 | 640 | foreach my $got_digit (_digit_split_lowtohigh($i, $radix)) { | |||||||||||||||||||
| 127 | 1576 | ▶ | if ($got_digit == $digit) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 665 | $count++; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 640 | return $count; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 46 | ▶ | sub pred { | ||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 135 | 46 | my ($self, $value) = @_; | |||||||||||||||||||
| 136 | 46 | ▶ | return ($value >= 0 && $value==int($value)); | ||||||||||||||||||
|
Condition33%1/3
$value >= 0 && $value == int $value
MC/DC0%0/2
$value >= 0 && $value == int $value
$value >= 0
$value == int $value
Truth table33%1/3
A && B
A= $value >= 0B= $value == int $value
| |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||