| 1 | # Copyright 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::SelfLengthCumulative; | ||||||||||||||||||||||||||||
| 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 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||
| 27 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Math::NumSeq::Fibonacci; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | *_blog2_estimate = \&Math::NumSeq::Fibonacci::_blog2_estimate; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use constant name => Math::NumSeq::__('Self Length Cumulative'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use constant description => Math::NumSeq::__('Cumulative digit length of own values.'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use constant default_i_start => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use constant values_min => 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 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my @oeis_anum; | |||||||||||||||||||||||||||
| 47 | 1 | $oeis_anum[10] = 'A064223'; | |||||||||||||||||||||||||||
| 48 | # OEIS-Catalogue: A064223 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | sub oeis_anum { | ||||||||||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 52 | 4 | return $oeis_anum[$self->{'radix'}]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 12 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 60 | 12 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||
| 61 | 12 | $self->{'value'} = 1; | |||||||||||||||||||||||||||
| 62 | 12 | $self->{'power'} = $self->{'radix'}; | |||||||||||||||||||||||||||
| 63 | 12 | $self->{'length'} = 1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 112 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 112 | my ($self) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 112 | my $value = $self->{'value'}; | |||||||||||||||||||||||||||
| 69 | 112 | ▶ | if ($value >= $self->{'power'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 24 | $self->{'power'} *= $self->{'radix'}; | |||||||||||||||||||||||||||
| 71 | 24 | $self->{'length'}++; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 112 | $self->{'value'} += $self->{'length'}; | |||||||||||||||||||||||||||
| 74 | 112 | return ($self->{'i'}++, $value); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 328 | ▶ | sub pred { | ||||||||||||||||||||||||||
|
Subroutine
pred: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 328 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 328 | ▶ | if ($value < 1 || $value != int($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value < 1 or $value != int $value
MC/DC50%1/2
$value < 1 or $value != int $value
$value < 1
$value != int $value
Truth table66%2/3
A or B
A= $value < 1B= $value != int $value
| |||||||||||||||||||||||||||||
| 81 | 134 | return 0; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 194 | ▶ | if (_is_infinite($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | return undef; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 194 | my $length = 1; | |||||||||||||||||||||||||||
| 88 | 194 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 89 | 194 | my $power = ($value * 0) + $radix; # inherit bignum $value | |||||||||||||||||||||||||||
| 90 | 194 | my $upto = 1; | |||||||||||||||||||||||||||
| 91 | 194 | for (;;) { | |||||||||||||||||||||||||||
| 92 | 678 | ▶ | if ($value < $power) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 194 | return (($value - $upto) % $length) == 0; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 484 | $upto = $power + ($upto-$power)%$length; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 484 | $power *= $radix; | |||||||||||||||||||||||||||
| 98 | 484 | $length++; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # value = 10 i=10 | ||||||||||||||||||||||||||||
| 103 | # value = 100 i=10+90/2 | ||||||||||||||||||||||||||||
| 104 | # value = 1000 i=10+90/2+900/3 | ||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||
| 106 | # value = R^k | ||||||||||||||||||||||||||||
| 107 | # i = R + R^2/2 + R^3/3 + ... + R^k/k | ||||||||||||||||||||||||||||
| 108 | # i = R + R^2/2 + R^3/3 + ... + R^k/k | ||||||||||||||||||||||||||||
| 109 | # <= (R + R^2 + R^3 + ... + R^k)/k | ||||||||||||||||||||||||||||
| 110 | # = (R^(k+1)-1)/(R-1) /k | ||||||||||||||||||||||||||||
| 111 | # ~= R^k/k | ||||||||||||||||||||||||||||
| 112 | # i ~= value/logR(value) | ||||||||||||||||||||||||||||
| 113 | # logR(value) = log(value)/log(R) | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | # est(R^k) = R^k/k | ||||||||||||||||||||||||||||
| 116 | # log(est(R^k)) = log(R^k/k) | ||||||||||||||||||||||||||||
| 117 | # = log(value) - log(k) | ||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||
| 119 | 92 | ▶ | sub value_to_i_estimate { | ||||||||||||||||||||||||||
|
Subroutine
value_to_i_estimate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 92 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 92 | ▶ | if ($value <= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 36 | return 0; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 56 | my $t; | |||||||||||||||||||||||||||
| 127 | 56 | ▶ | if (defined (my $blog2 = _blog2_estimate($value))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 4 | $t = $blog2 * log(2); | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 52 | $t = log($value); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 56 | $self->{'logR'} = log($self->{'radix'}); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # integer divisor to help Math::BigInt | ||||||||||||||||||||||||||||
| 136 | 56 | my $div = int($t/$self->{'logR'}); | |||||||||||||||||||||||||||
| 137 | 56 | ▶ | if ($div > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 27 | $value /= $div; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 56 | return int($value); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||