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::DigitLengthCumulative; |
19
|
1
|
|
|
1
|
|
3015
|
use 5.004; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
57
|
|
20
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
8
|
use vars '$VERSION', '@ISA'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
90
|
|
23
|
|
|
|
|
|
|
$VERSION = 71; |
24
|
1
|
|
|
1
|
|
6
|
use Math::NumSeq; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
25
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq'); |
26
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
1
|
|
8
|
use Math::NumSeq::Fibonacci; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
44
|
|
29
|
|
|
|
|
|
|
*_blog2_estimate = \&Math::NumSeq::Fibonacci::_blog2_estimate; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Math::NumSeq::Base::Digits |
32
|
1
|
|
|
1
|
|
6
|
'parameter_info_array'; # radix parameter |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
81
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
35
|
|
|
|
|
|
|
#use Smart::Comments; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
7
|
use vars '$VERSION'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
77
|
|
39
|
|
|
|
|
|
|
$VERSION = 71; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# use constant name => Math::NumSeq::__('Digit Length Cumulative'); |
42
|
1
|
|
|
1
|
|
7
|
use constant description => Math::NumSeq::__('Cumulative length of numbers 0,1,2,3,etc written out in the given radix. For example binary 1,2,4,6,9,12,15,18,22,etc, 2 steps by 2, then 4 steps by 3, then 8 steps by 4, then 16 steps by 5, etc.'); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
43
|
1
|
|
|
1
|
|
6
|
use constant i_start => 0; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
44
|
1
|
|
|
1
|
|
6
|
use constant values_min => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
45
|
1
|
|
|
1
|
|
6
|
use constant characteristic_increasing => 1; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
57
|
|
46
|
1
|
|
|
1
|
|
5
|
use constant characteristic_integer => 1; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1259
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
49
|
|
|
|
|
|
|
# cf A117804 - natural position of n in 012345678910111213 |
50
|
|
|
|
|
|
|
# A061168 |
51
|
|
|
|
|
|
|
# |
52
|
|
|
|
|
|
|
my @oeis_anum; |
53
|
|
|
|
|
|
|
$oeis_anum[2] = 'A083652'; # 2 binary |
54
|
|
|
|
|
|
|
# OEIS-Catalogue: A083652 radix=2 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub oeis_anum { |
57
|
1
|
|
|
1
|
1
|
6
|
my ($self) = @_; |
58
|
1
|
|
|
|
|
4
|
return $oeis_anum[$self->{'radix'}]; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub rewind { |
64
|
3
|
|
|
3
|
1
|
1220
|
my ($self) = @_; |
65
|
|
|
|
|
|
|
### DigitLengthCumulative rewind(): $self |
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
21
|
$self->{'i'} = $self->i_start; |
68
|
3
|
|
|
|
|
8
|
$self->{'length'} = 1; |
69
|
3
|
|
|
|
|
8
|
$self->{'limit'} = $self->{'radix'}; |
70
|
3
|
|
|
|
|
5
|
$self->{'total'} = 0; |
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
22
|
$self->{'logR'} = log($self->{'radix'}); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub _UNTESTED__seek_to_i { |
75
|
0
|
|
|
0
|
|
0
|
my ($self, $i) = @_; |
76
|
0
|
|
|
|
|
0
|
$self->{'i'} = $i; |
77
|
0
|
|
|
|
|
0
|
my $length = $self->{'length'} = $self->Math::NumSeq::DigitLength::ith($i); |
78
|
0
|
|
|
|
|
0
|
$self->{'limit'} = $self->{'radix'} ** ($length+1); |
79
|
0
|
|
|
|
|
0
|
$self->{'total'} = $self->ith($i); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
sub next { |
82
|
118
|
|
|
118
|
1
|
1043
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
### DigitLengthCumulative next(): $self |
84
|
|
|
|
|
|
|
### count: $self->{'count'} |
85
|
|
|
|
|
|
|
### bits: $self->{'bits'} |
86
|
|
|
|
|
|
|
|
87
|
118
|
|
|
|
|
169
|
my $i = $self->{'i'}++; |
88
|
118
|
100
|
|
|
|
244
|
if ($i >= $self->{'limit'}) { |
89
|
10
|
|
|
|
|
17
|
$self->{'limit'} *= $self->{'radix'}; |
90
|
10
|
|
|
|
|
12
|
$self->{'length'}++; |
91
|
|
|
|
|
|
|
### step to |
92
|
|
|
|
|
|
|
### length: $self->{'length'} |
93
|
|
|
|
|
|
|
### remaining: $self->{'limit'} |
94
|
|
|
|
|
|
|
} |
95
|
118
|
|
|
|
|
387
|
return ($i, ($self->{'total'} += $self->{'length'})); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# 0 to 9 is 10 of |
99
|
|
|
|
|
|
|
sub ith { |
100
|
475
|
|
|
475
|
1
|
842
|
my ($self, $i) = @_; |
101
|
|
|
|
|
|
|
### DigitLengthCumulative ith(): $i |
102
|
|
|
|
|
|
|
|
103
|
475
|
50
|
|
|
|
1136
|
if (_is_infinite($i)) { |
104
|
0
|
|
|
|
|
0
|
return $i; # don't loop forever if $i is +infinity |
105
|
|
|
|
|
|
|
} |
106
|
475
|
|
|
|
|
16940
|
my $ret = 1; |
107
|
475
|
|
|
|
|
496
|
my $length = 1; |
108
|
475
|
|
|
|
|
804
|
my $radix = $self->{'radix'}; |
109
|
475
|
|
|
|
|
683
|
my $power = ($i*0)+1; # inherit bignum 1 |
110
|
475
|
|
|
|
|
16122
|
for (;;) { |
111
|
|
|
|
|
|
|
### $ret |
112
|
|
|
|
|
|
|
### $length |
113
|
|
|
|
|
|
|
### $power |
114
|
2329
|
|
|
|
|
2754
|
my $next_power = $power * $radix; |
115
|
2329
|
100
|
|
|
|
36448
|
if ($i < $next_power) { |
116
|
|
|
|
|
|
|
### final extra: $length * ($i - $power + 1) |
117
|
475
|
|
|
|
|
2669
|
return $ret + $length * ($i - $power + 1); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
### add: $length * $next_power |
120
|
1854
|
|
|
|
|
7425
|
$ret += $length++ * ($next_power - $power); |
121
|
1854
|
|
|
|
|
62522
|
$power = $next_power; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub pred { |
126
|
642
|
|
|
642
|
1
|
3264
|
my ($self, $value) = @_; |
127
|
|
|
|
|
|
|
|
128
|
642
|
50
|
|
|
|
1326
|
if (_is_infinite($value)) { |
129
|
0
|
|
|
|
|
0
|
return undef; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
{ |
132
|
642
|
|
|
|
|
858
|
my $int = int($value); |
|
642
|
|
|
|
|
781
|
|
133
|
642
|
100
|
|
|
|
1343
|
if ($value != $int) { |
134
|
291
|
|
|
|
|
586
|
return 0; |
135
|
|
|
|
|
|
|
} |
136
|
351
|
|
|
|
|
488
|
$value = $int; |
137
|
|
|
|
|
|
|
} |
138
|
351
|
50
|
|
|
|
621
|
if ($value == 0) { |
139
|
0
|
|
|
|
|
0
|
return 0; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
351
|
|
|
|
|
537
|
my $radix = $self->{'radix'}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# length=1 |
145
|
|
|
|
|
|
|
# values 0,1,2,...,9 |
146
|
|
|
|
|
|
|
# cumulative 1,2,3,...,10 |
147
|
351
|
100
|
|
|
|
685
|
if ($value <= $radix) { |
148
|
4
|
|
|
|
|
9
|
return 1; |
149
|
|
|
|
|
|
|
} |
150
|
347
|
|
|
|
|
692
|
$value -= $radix; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# initial 10 to 99 = 90 values R*(R-1) |
153
|
|
|
|
|
|
|
# later 1000 to 9999 = 9000 values R*R*R*(R-1) |
154
|
|
|
|
|
|
|
# eg length=3 |
155
|
|
|
|
|
|
|
# values 1000 to 9999 |
156
|
|
|
|
|
|
|
# cumulative 3,6,..., |
157
|
347
|
|
|
|
|
411
|
my $length = 2; |
158
|
347
|
|
|
|
|
541
|
my $count = ($value*0) # inherit bignum $value |
159
|
|
|
|
|
|
|
+ $radix*($radix-1); |
160
|
|
|
|
|
|
|
|
161
|
347
|
|
|
|
|
371
|
for (;;) { |
162
|
1487
|
|
|
|
|
1780
|
my $limit = $count*$length; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
### $length |
165
|
|
|
|
|
|
|
### $count |
166
|
|
|
|
|
|
|
### remainder: $value |
167
|
|
|
|
|
|
|
### $limit |
168
|
|
|
|
|
|
|
|
169
|
1487
|
100
|
|
|
|
3116
|
if ($value <= $limit) { |
170
|
347
|
|
|
|
|
921
|
return ($value % $length) == 0; |
171
|
|
|
|
|
|
|
} |
172
|
1140
|
|
|
|
|
1534
|
$value -= $limit; |
173
|
1140
|
|
|
|
|
1230
|
$length++; |
174
|
1140
|
|
|
|
|
1297
|
$count *= $radix; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub value_to_i { |
179
|
295
|
|
|
295
|
1
|
1199
|
my ($self, $value) = @_; |
180
|
295
|
|
|
|
|
717
|
my $i = $self->value_to_i_floor($value); |
181
|
295
|
100
|
|
|
|
14437
|
if ($value == $self->ith($i)) { |
182
|
118
|
|
|
|
|
23095
|
return $i; |
183
|
|
|
|
|
|
|
} |
184
|
177
|
|
|
|
|
399
|
return undef; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
sub value_to_i_floor { |
187
|
648
|
|
|
648
|
1
|
3903
|
my ($self, $value) = @_; |
188
|
|
|
|
|
|
|
|
189
|
648
|
50
|
|
|
|
1416
|
if (_is_infinite($value)) { |
190
|
0
|
|
|
|
|
0
|
return $value; |
191
|
|
|
|
|
|
|
} |
192
|
648
|
|
|
|
|
44503
|
$value = int($value); |
193
|
|
|
|
|
|
|
|
194
|
648
|
100
|
|
|
|
2861
|
if ($value < 1) { |
195
|
11
|
|
|
|
|
27
|
return 0; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# length=1 |
199
|
|
|
|
|
|
|
# values 0,1,2,...,9 |
200
|
|
|
|
|
|
|
# cumulative 1,2,3,...,10 |
201
|
|
|
|
|
|
|
# |
202
|
637
|
|
|
|
|
9445
|
my $radix = $self->{'radix'}; |
203
|
637
|
100
|
|
|
|
1242
|
if ($value <= $radix) { |
204
|
10
|
|
|
|
|
303
|
return $value-1; |
205
|
|
|
|
|
|
|
} |
206
|
627
|
|
|
|
|
8694
|
$value -= $radix; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# initial 10 to 99 = 90 values R*(R-1) |
209
|
|
|
|
|
|
|
# later 1000 to 9999 = 9000 values R*R*R*(R-1) |
210
|
|
|
|
|
|
|
# eg length=3 |
211
|
|
|
|
|
|
|
# values 1000 to 9999 |
212
|
|
|
|
|
|
|
# cumulative 3,6,..., |
213
|
627
|
|
|
|
|
15506
|
my $length = 2; |
214
|
627
|
|
|
|
|
1057
|
my $count = ($value*0) # inherit bignum $value |
215
|
|
|
|
|
|
|
+ $radix*($radix-1); |
216
|
627
|
|
|
|
|
51667
|
my $i = $radix-1; |
217
|
|
|
|
|
|
|
|
218
|
627
|
|
|
|
|
731
|
for (;;) { |
219
|
2561
|
|
|
|
|
38362
|
my $limit = $count*$length; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
### $length |
222
|
|
|
|
|
|
|
### $count |
223
|
|
|
|
|
|
|
### remainder: $value |
224
|
|
|
|
|
|
|
### $limit |
225
|
|
|
|
|
|
|
|
226
|
2561
|
100
|
|
|
|
57181
|
if ($value <= $limit) { |
227
|
627
|
|
|
|
|
4156
|
return $i + int($value/$length); |
228
|
|
|
|
|
|
|
} |
229
|
1934
|
|
|
|
|
10712
|
$value -= $limit; |
230
|
1934
|
|
|
|
|
38093
|
$i += $count; |
231
|
1934
|
|
|
|
|
26304
|
$length++; |
232
|
1934
|
|
|
|
|
2625
|
$count *= $radix; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# OR: estimate |
237
|
|
|
|
|
|
|
# value = 1 + (R-1) + 2*R*(R-1) + 3*R*R*(R-1) + ... + k*R^(k-1)*(R-1) |
238
|
|
|
|
|
|
|
# = 1+ (R-1) * [ 1 + R + R^2 + R^3 + ... + R^(k-1) |
239
|
|
|
|
|
|
|
# + R + R^2 + R^3 + ... + R^(k-1) |
240
|
|
|
|
|
|
|
# + R^2 + R^3 + ... + R^(k-1) |
241
|
|
|
|
|
|
|
# ... + R^(k-1) ] |
242
|
|
|
|
|
|
|
# = 1+ (R-1) * [ (R^k - 1) / (R-1) |
243
|
|
|
|
|
|
|
# + R *(R^(k-1) - 1) / (R-1) |
244
|
|
|
|
|
|
|
# + R^2 *(R^(k-2) - 1) / (R-1) |
245
|
|
|
|
|
|
|
# ... + R^(k-1) *(R - 1) / (R-1) ] |
246
|
|
|
|
|
|
|
# = 1+ [ (R^k - 1) |
247
|
|
|
|
|
|
|
# + R *(R^(k-1) - 1) |
248
|
|
|
|
|
|
|
# + R^2 *(R^(k-2) - 1) |
249
|
|
|
|
|
|
|
# ... + R^(k-1) *(R - 1) ] |
250
|
|
|
|
|
|
|
# ~= (R^k) |
251
|
|
|
|
|
|
|
# + R *(R^(k-1)) |
252
|
|
|
|
|
|
|
# + R^2 *(R^(k-2)) |
253
|
|
|
|
|
|
|
# ... + R^(k-1) *(R) |
254
|
|
|
|
|
|
|
# = k*R^k at i=R^k |
255
|
|
|
|
|
|
|
# |
256
|
|
|
|
|
|
|
# log(k*R^k) = log(k) + k*log(R) |
257
|
|
|
|
|
|
|
# target t=log(value) |
258
|
|
|
|
|
|
|
# f(x) = x*log(R) + log(x) - t |
259
|
|
|
|
|
|
|
# f'(x) = log(R) + log(x) |
260
|
|
|
|
|
|
|
# next_x = x - f(x)/f'(x) |
261
|
|
|
|
|
|
|
# = x - (x*log(R) + log(x) - t)/(log(R) + log(x)) |
262
|
|
|
|
|
|
|
# = (x*(log(R) + log(x)) - (x*log(R) + log(x) - t)) |
263
|
|
|
|
|
|
|
# / (log(R) + log(x)) |
264
|
|
|
|
|
|
|
# = (x*log(R) + x*log(x) - x*log(R) - log(x) + t) |
265
|
|
|
|
|
|
|
# / (log(R) + log(x)) |
266
|
|
|
|
|
|
|
# = (x*log(x) - log(x) + t) / (log(R) + log(x)) |
267
|
|
|
|
|
|
|
# = ((x-1)*log(x) + t) / (log(R) + log(x)) |
268
|
|
|
|
|
|
|
# |
269
|
|
|
|
|
|
|
# For i=R^k value=k*R^k estimate k as kest=logR(value), which is only a bit |
270
|
|
|
|
|
|
|
# bigger than it should be, and divide that out value/kest~=R^k=i |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub value_to_i_estimate { |
273
|
68
|
|
|
68
|
1
|
722
|
my ($self, $value) = @_; |
274
|
|
|
|
|
|
|
|
275
|
68
|
100
|
|
|
|
139
|
if ($value <= 1) { |
276
|
9
|
|
|
|
|
19
|
return 0; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
59
|
|
|
|
|
140
|
my $t; |
280
|
59
|
100
|
|
|
|
164
|
if (defined (my $blog2 = _blog2_estimate($value))) { |
281
|
1
|
|
|
|
|
351
|
$t = $blog2 * log(2); |
282
|
|
|
|
|
|
|
} else { |
283
|
58
|
|
|
|
|
93
|
$t = log($value); |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# integer divisor to help Math::BigInt $value |
287
|
59
|
|
|
|
|
117
|
my $div = int($t/$self->{'logR'}); |
288
|
59
|
100
|
|
|
|
122
|
if ($div > 1) { |
289
|
58
|
|
|
|
|
80
|
$value /= $div; |
290
|
|
|
|
|
|
|
} |
291
|
59
|
|
|
|
|
290
|
return int($value); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
1; |
295
|
|
|
|
|
|
|
__END__ |