| 1 | # Copyright 2010, 2011, 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::DigitLength; | ||||||||||||||||||||
| 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 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 1 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||
| 26 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # uncomment this to run the ### lines | ||||||||||||||||||||
| 30 | #use Smart::Comments; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # use constant name => Math::NumSeq::__('Digit Length'); | ||||||||||||||||||||
| 33 | 1 | ▶ | use constant description => Math::NumSeq::__('How many digits the number requires in the given radix. For example binary 1,1,2,2,3,3,3,3,4, etc.'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 1 | ▶ | use constant values_min => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 1 | ▶ | use constant i_start => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1 | ▶ | use constant characteristic_count => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | ▶ | use constant characteristic_smaller => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | use Math::NumSeq::Base::Digits | ||||||||||||||||||||
| 41 | 1 | ▶ | 'parameter_info_array'; # radix parameter | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 44 | # cf A000523 - floor(log2(n)), is bitlength-1 | ||||||||||||||||||||
| 45 | # A036786 - roman numeral length < decimal length | ||||||||||||||||||||
| 46 | # A036787 - roman numeral length == decimal length | ||||||||||||||||||||
| 47 | # A036788 - roman numeral length <= decimal length | ||||||||||||||||||||
| 48 | # | ||||||||||||||||||||
| 49 | 1 | my @oeis_anum = ( | |||||||||||||||||||
| 50 | # OEIS-Catalogue array begin | ||||||||||||||||||||
| 51 | undef, # 0 | ||||||||||||||||||||
| 52 | undef, # 1 | ||||||||||||||||||||
| 53 | 'A070939', # radix=2 | ||||||||||||||||||||
| 54 | 'A081604', # radix=3 # ternary | ||||||||||||||||||||
| 55 | 'A110591', # radix=4 | ||||||||||||||||||||
| 56 | 'A110592', # radix=5 | ||||||||||||||||||||
| 57 | undef, # 6 | ||||||||||||||||||||
| 58 | undef, # 7 | ||||||||||||||||||||
| 59 | undef, # 8 | ||||||||||||||||||||
| 60 | undef, # 9 | ||||||||||||||||||||
| 61 | 'A055642', # radix=10 | ||||||||||||||||||||
| 62 | # OEIS-Catalogue array end | ||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||
| 64 | 3 | ▶ | sub oeis_anum { | ||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 65 | 3 | my ($self) = @_; | |||||||||||||||||||
| 66 | 3 | return $oeis_anum[$self->{'radix'}]; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 9 | ▶ | sub rewind { | ||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 73 | 9 | my ($self) = @_; | |||||||||||||||||||
| 74 | 9 | $self->{'i'} = $self->i_start; | |||||||||||||||||||
| 75 | 9 | $self->{'length'} = 1; | |||||||||||||||||||
| 76 | 9 | $self->{'limit'} = $self->{'radix'}; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 0 | ▶ | sub _UNTESTED__seek_to_i { | ||||||||||||||||||
|
Subroutine
_UNTESTED__seek_to_i: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 79 | 0 | my ($self, $i) = @_; | |||||||||||||||||||
| 80 | 0 | $self->{'i'} = $i; | |||||||||||||||||||
| 81 | 0 | my $length = $self->{'length'} = $self->ith($i); | |||||||||||||||||||
| 82 | 0 | $self->{'limit'} = $self->{'radix'} ** ($length+1); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 158 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 85 | 158 | my ($self) = @_; | |||||||||||||||||||
| 86 | ### DigitLength next(): $self | ||||||||||||||||||||
| 87 | ### count: $self->{'count'} | ||||||||||||||||||||
| 88 | ### bits: $self->{'bits'} | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 158 | my $i = $self->{'i'}++; | |||||||||||||||||||
| 91 | 158 | ▶ | if ($i >= $self->{'limit'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 18 | $self->{'limit'} *= $self->{'radix'}; | |||||||||||||||||||
| 93 | 18 | $self->{'length'}++; | |||||||||||||||||||
| 94 | ### step to | ||||||||||||||||||||
| 95 | ### length: $self->{'length'} | ||||||||||||||||||||
| 96 | ### remaining: $self->{'limit'} | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 158 | return ($i, $self->{'length'}); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 246 | ▶ | sub ith { | ||||||||||||||||||
|
Subroutine
ith: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 102 | 246 | my ($self, $i) = @_; | |||||||||||||||||||
| 103 | 246 | ▶ | if (_is_infinite($i)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | return $i; # don't loop forever if $i is +infinity | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | 246 | my $length = 1; | |||||||||||||||||||
| 107 | 246 | my $radix = $self->{'radix'}; | |||||||||||||||||||
| 108 | 246 | my $power = $i*0 + $radix; # inherit possible $i bignum | |||||||||||||||||||
| 109 | 246 | ▶ | while ($i >= $power) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 489 | $length++; | |||||||||||||||||||
| 111 | 489 | $power *= $radix; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | 246 | return $length; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 79 | ▶ | sub pred { | ||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 117 | 79 | my ($self, $value) = @_; | |||||||||||||||||||
| 118 | 79 | ▶ | return ($value >= 1 && $value == int($value)); | ||||||||||||||||||
|
Condition33%1/3
$value >= 1 && $value == int $value
MC/DC0%0/2
$value >= 1 && $value == int $value
$value >= 1
$value == int $value
Truth table33%1/3
A && B
A= $value >= 1B= $value == int $value
| |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # not actually documented yet ... | ||||||||||||||||||||
| 122 | 285 | ▶ | sub value_to_i_floor { | ||||||||||||||||||
|
Subroutine
value_to_i_floor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | 285 | my ($self, $value) = @_; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # radix**(value-1) is the first of length $value, except 0 is the first | ||||||||||||||||||||
| 126 | # length 1 | ||||||||||||||||||||
| 127 | 285 | $value = int($value)-1; | |||||||||||||||||||
| 128 | 285 | ▶ | if ($value <= 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 97 | return 0; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 188 | return $self->{'radix'} ** $value; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 1 | *value_to_i_estimate = \&value_to_i_floor; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 1 | 1; | |||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||