line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 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::WoodallNumbers; |
19
|
2
|
|
|
2
|
|
14647
|
use 5.004; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
80
|
|
20
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
78
|
|
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
2
|
|
10
|
use vars '$VERSION', '@ISA'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
218
|
|
23
|
|
|
|
|
|
|
$VERSION = 71; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
|
608
|
use Math::NumSeq; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
103
|
|
26
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq'); |
27
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
|
820
|
use Math::NumSeq::Fibonacci; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
134
|
|
30
|
|
|
|
|
|
|
*_blog2_estimate = \&Math::NumSeq::Fibonacci::_blog2_estimate; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
33
|
|
|
|
|
|
|
#use Smart::Comments; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# use constant name => Math::NumSeq::__('Woodall Numbers'); |
37
|
2
|
|
|
2
|
|
11
|
use constant description => Math::NumSeq::__('Woodall numbers i*2^i-1.'); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
21
|
|
38
|
2
|
|
|
2
|
|
12
|
use constant characteristic_increasing => 1; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
111
|
|
39
|
2
|
|
|
2
|
|
11
|
use constant characteristic_integer => 1; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
102
|
|
40
|
2
|
|
|
2
|
|
10
|
use constant values_min => 1; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
92
|
|
41
|
2
|
|
|
2
|
|
9
|
use constant i_start => 1; # from 1*2^1-1==1 |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
100
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# cf A002234 - n of Woodall primes |
44
|
|
|
|
|
|
|
# A050918 - Woodall primes values |
45
|
|
|
|
|
|
|
# A056821 - totient(woodall) |
46
|
|
|
|
|
|
|
# |
47
|
2
|
|
|
2
|
|
8
|
use constant oeis_anum => 'A003261'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1770
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# pow*i+1 |
50
|
|
|
|
|
|
|
my $uv_i_limit = do { |
51
|
|
|
|
|
|
|
my $max = ~0; |
52
|
|
|
|
|
|
|
my $limit = 1; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
for (my $i = 1; $i < 1000; $i++) { |
55
|
|
|
|
|
|
|
if ($i <= (($max>>1)+1) >> ($i-1)) { |
56
|
|
|
|
|
|
|
$limit = $i; |
57
|
|
|
|
|
|
|
} else { |
58
|
|
|
|
|
|
|
last; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
### max : $max |
63
|
|
|
|
|
|
|
### woodall: (1<<$limit)*$limit+1 |
64
|
|
|
|
|
|
|
### assert: $limit*(1<<$limit)+1 <= $max |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$limit |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
### $uv_i_limit |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub rewind { |
71
|
4
|
|
|
4
|
1
|
6
|
my ($self) = @_; |
72
|
4
|
|
|
|
|
21
|
my $i = $self->{'i'} = $self->i_start; |
73
|
4
|
|
|
|
|
15
|
$self->{'power'} = 2 ** $i; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
sub seek_to_i { |
76
|
14
|
|
|
14
|
1
|
995
|
my ($self, $i) = @_; |
77
|
|
|
|
|
|
|
### seek_to_i(): $i |
78
|
14
|
|
|
|
|
23
|
$self->{'i'} = $i; |
79
|
14
|
100
|
|
|
|
34
|
if ($i >= $uv_i_limit) { |
80
|
2
|
|
|
|
|
7
|
$i = Math::NumSeq::_to_bigint($i); |
81
|
|
|
|
|
|
|
} |
82
|
14
|
|
|
|
|
331
|
$self->{'power'} = 2 ** $i; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
sub _UNTESTED__seek_to_value { |
85
|
0
|
|
|
0
|
|
0
|
my ($self, $value) = @_; |
86
|
0
|
|
|
|
|
0
|
$self->seek_to_i($self->value_to_i_ceil($value)); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# diff = (i+1)*2^(i+1)-1 - (i*2^i-1) |
90
|
|
|
|
|
|
|
# = i*2^(i+1) + 2^(i+1) - i*2^i |
91
|
|
|
|
|
|
|
# = i*(2^(i+1) - 2^i) + 2^(i+1) |
92
|
|
|
|
|
|
|
# = i*2^i + 2^(i+1) |
93
|
|
|
|
|
|
|
# 2*(i*2^i-1) + 2*2^i + 1 |
94
|
|
|
|
|
|
|
# = 2*i*2^i-2 + 2*2^i + 1 |
95
|
|
|
|
|
|
|
# = (2*i+2)*2^i - 1 |
96
|
|
|
|
|
|
|
# = (i+1)*2^(i+1) - 2 |
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
sub next { |
99
|
25
|
|
|
25
|
1
|
640
|
my ($self) = @_; |
100
|
25
|
|
|
|
|
61
|
my $i = $self->{'i'}++; |
101
|
25
|
50
|
|
|
|
64
|
if ($i == $uv_i_limit) { |
102
|
0
|
|
|
|
|
0
|
$self->{'power'} = Math::NumSeq::_to_bigint($self->{'power'}); |
103
|
|
|
|
|
|
|
} |
104
|
25
|
|
|
|
|
67
|
my $value = $self->{'power'}*$i - 1; |
105
|
25
|
|
|
|
|
725
|
$self->{'power'} *= 2; |
106
|
25
|
|
|
|
|
322
|
return ($i, $value); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub ith { |
110
|
14
|
|
|
14
|
1
|
662
|
my ($self, $i) = @_; |
111
|
14
|
|
|
|
|
18
|
my $power; |
112
|
14
|
100
|
66
|
|
|
69
|
if (! ref $i && $i >= $uv_i_limit) { |
113
|
2
|
|
|
|
|
8
|
$power = Math::NumSeq::_to_bigint(1) << $i; |
114
|
|
|
|
|
|
|
} else { |
115
|
12
|
|
|
|
|
20
|
$power = 2**$i; |
116
|
|
|
|
|
|
|
} |
117
|
14
|
|
|
|
|
658
|
return $i * $power - 1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub pred { |
121
|
9
|
|
|
9
|
1
|
20
|
my ($self, $value) = @_; |
122
|
|
|
|
|
|
|
### WoodallNumbers pred(): $value |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
{ |
125
|
9
|
|
|
|
|
11
|
my $int = int($value); |
|
9
|
|
|
|
|
11
|
|
126
|
9
|
50
|
|
|
|
21
|
if ($value != $int) { return 0; } |
|
0
|
|
|
|
|
0
|
|
127
|
9
|
|
|
|
|
12
|
$value = $int; |
128
|
|
|
|
|
|
|
} |
129
|
9
|
100
|
100
|
|
|
42
|
unless ($value >= 1 |
130
|
|
|
|
|
|
|
&& ($value % 2) == 1) { |
131
|
5
|
|
|
|
|
20
|
return 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
4
|
|
|
|
|
5
|
$value += 1; # now seeking $value == $exp * 2**$exp |
135
|
4
|
50
|
|
|
|
14
|
if (_is_infinite($value)) { |
136
|
0
|
|
|
|
|
0
|
return 0; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
4
|
|
|
|
|
7
|
my $exp = 0; |
140
|
4
|
|
|
|
|
7
|
for (;;) { |
141
|
12
|
100
|
66
|
|
|
45
|
if ($value <= $exp || $value % 2) { |
142
|
4
|
|
|
|
|
133
|
return ($value == $exp); |
143
|
|
|
|
|
|
|
} |
144
|
8
|
|
|
|
|
9
|
$value >>= 1; |
145
|
8
|
|
|
|
|
15
|
$exp++; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# ENHANCE-ME: round_down_pow(value,2) then exp-=log2(exp) is maybe only |
150
|
|
|
|
|
|
|
# 0,+1,+2 away from being correct |
151
|
|
|
|
|
|
|
# |
152
|
|
|
|
|
|
|
sub value_to_i_floor { |
153
|
22526
|
|
|
22526
|
1
|
112246
|
my ($self, $value) = @_; |
154
|
|
|
|
|
|
|
### WoodallNumbers value_to_i_floor(): $value |
155
|
|
|
|
|
|
|
|
156
|
22526
|
|
|
|
|
30703
|
$value = int($value) + 1; # now seeking $value == $exp * 2**$exp |
157
|
22526
|
100
|
|
|
|
42567
|
if ($value < 8) { |
158
|
6
|
|
|
|
|
14
|
return 1; |
159
|
|
|
|
|
|
|
} |
160
|
22520
|
50
|
|
|
|
48026
|
if (_is_infinite($value)) { |
161
|
0
|
|
|
|
|
0
|
return $value; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
22520
|
|
|
|
|
31140
|
my $i = 1; |
165
|
22520
|
|
|
|
|
27919
|
my $power = ($value*0) + 2; # 2^1=2, inherit bignum 2 |
166
|
|
|
|
|
|
|
|
167
|
22520
|
|
|
|
|
22310
|
for (;;) { |
168
|
229351
|
|
|
|
|
240517
|
my $try_value = $i*$power; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
### $i |
171
|
|
|
|
|
|
|
### try_value: "$try_value" |
172
|
|
|
|
|
|
|
|
173
|
229351
|
100
|
|
|
|
378641
|
if ($try_value >= $value) { |
174
|
22520
|
|
|
|
|
60134
|
return $i - 1 + ($try_value == $value); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
206831
|
50
|
|
|
|
326012
|
if ($i == $uv_i_limit) { |
178
|
0
|
|
|
|
|
0
|
$power = Math::NumSeq::_to_bigint($power); |
179
|
|
|
|
|
|
|
} |
180
|
206831
|
|
|
|
|
202938
|
$power *= 2; |
181
|
206831
|
|
|
|
|
203701
|
$i++; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# shared by Math::NumSeq::CullenNumbers |
186
|
|
|
|
|
|
|
sub value_to_i_estimate { |
187
|
19
|
|
|
19
|
1
|
374
|
my ($self, $value) = @_; |
188
|
|
|
|
|
|
|
|
189
|
19
|
100
|
|
|
|
45
|
if ($value < 1) { |
190
|
8
|
|
|
|
|
18
|
return 0; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
11
|
|
|
|
|
136
|
my $i = _blog2_estimate($value); |
194
|
11
|
100
|
|
|
|
417
|
if (! defined $i) { |
195
|
10
|
|
|
|
|
22
|
$i = log($value) * (1/log(2)); |
196
|
|
|
|
|
|
|
} |
197
|
11
|
|
|
|
|
25
|
$i -= log($i) * (1/log(2)); |
198
|
11
|
|
|
|
|
25
|
return int($i); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
__END__ |