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::ProthNumbers; |
19
|
2
|
|
|
2
|
|
29053
|
use 5.004; |
|
2
|
|
|
|
|
82
|
|
|
2
|
|
|
|
|
105
|
|
20
|
2
|
|
|
2
|
|
15
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
95
|
|
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
2
|
|
10
|
use vars '$VERSION', '@ISA'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
153
|
|
23
|
|
|
|
|
|
|
$VERSION = 71; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
|
832
|
use Math::NumSeq; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
117
|
|
26
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq'); |
27
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
|
790
|
use Math::NumSeq::NumAronson; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
112
|
|
30
|
|
|
|
|
|
|
*_round_down_pow = \&Math::NumSeq::NumAronson::_round_down_pow; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
33
|
|
|
|
|
|
|
# use Smart::Comments; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# use constant name => Math::NumSeq::__('Proth Numbers'); |
37
|
2
|
|
|
2
|
|
13
|
use constant description => Math::NumSeq::__('Proth numbers k*2^n+1 for odd k and k < 2^n.'); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
9
|
|
38
|
2
|
|
|
2
|
|
10
|
use constant i_start => 1; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
88
|
|
39
|
2
|
|
|
2
|
|
10
|
use constant values_min => 3; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
138
|
|
40
|
2
|
|
|
2
|
|
12
|
use constant characteristic_increasing => 1; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
104
|
|
41
|
2
|
|
|
2
|
|
24
|
use constant characteristic_integer => 1; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
102
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# cf A157892 - value of k |
44
|
|
|
|
|
|
|
# A157893 - value of n |
45
|
|
|
|
|
|
|
# A080076 - Proth primes |
46
|
|
|
|
|
|
|
# A134876 - how many Proth primes for given n |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
# A002253 3*2^n+1 is prime |
49
|
|
|
|
|
|
|
# A002254 5*2^n+1 |
50
|
|
|
|
|
|
|
# A032353 7*2^n+1 |
51
|
|
|
|
|
|
|
# A002256 9*2^n+1 |
52
|
|
|
|
|
|
|
# |
53
|
|
|
|
|
|
|
# A086341 - 2^x+/-1 is sqrt of Proth squares |
54
|
|
|
|
|
|
|
# |
55
|
2
|
|
|
2
|
|
9
|
use constant oeis_anum => 'A080075'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1714
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# $uv_limit is the biggest power 4**k which fits in a UV |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
my $uv_limit = do { |
61
|
|
|
|
|
|
|
my $max = ~0; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# limit <= floor(max/4) means 4*limit <= max is still good |
64
|
|
|
|
|
|
|
my $limit = 4; |
65
|
|
|
|
|
|
|
while ($limit <= ($max >> 2)) { |
66
|
|
|
|
|
|
|
$limit <<= 2; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
### ProthNumbers UV limit ... |
70
|
|
|
|
|
|
|
### $limit |
71
|
|
|
|
|
|
|
### limit: sprintf('%#X',$limit) |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$limit |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub rewind { |
77
|
7
|
|
|
7
|
1
|
1179
|
my ($self) = @_; |
78
|
7
|
|
|
|
|
32
|
$self->{'value'} = 1; |
79
|
7
|
|
|
|
|
22
|
$self->{'inc'} = 2; |
80
|
7
|
|
|
|
|
12
|
$self->{'limit'} = 4; |
81
|
7
|
|
|
|
|
51
|
$self->{'i'} = $self->i_start; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
sub next { |
84
|
1531
|
|
|
1531
|
1
|
373776
|
my ($self) = @_; |
85
|
|
|
|
|
|
|
### ProthNumbers next(): sprintf('value=%b inc=%b limit=%b', $self->{'value'}, $self->{'inc'}, $self->{'limit'}) |
86
|
|
|
|
|
|
|
|
87
|
1531
|
|
|
|
|
2662
|
my $value = ($self->{'value'} += $self->{'inc'}); |
88
|
1531
|
100
|
|
|
|
3863
|
if ($value >= $self->{'limit'}) { |
89
|
|
|
|
|
|
|
### bigger 2^n now ... |
90
|
78
|
50
|
|
|
|
157
|
if ($self->{'limit'} == $uv_limit) { |
91
|
|
|
|
|
|
|
### go to BigInt |
92
|
0
|
|
|
|
|
0
|
$self->{'value'} = Math::NumSeq::_to_bigint($self->{'value'}); |
93
|
0
|
|
|
|
|
0
|
$self->{'inc'} = Math::NumSeq::_to_bigint($self->{'inc'}); |
94
|
0
|
|
|
|
|
0
|
$self->{'limit'} = Math::NumSeq::_to_bigint($self->{'limit'}); |
95
|
|
|
|
|
|
|
} |
96
|
78
|
|
|
|
|
111
|
$self->{'inc'} *= 2; |
97
|
78
|
|
|
|
|
100
|
$self->{'limit'} *= 4; |
98
|
|
|
|
|
|
|
} |
99
|
1531
|
|
|
|
|
3750
|
return ($self->{'i'}++, $value); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# seek value at i-1, inc and limit ready to make next value |
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# i i value inc limit next value |
105
|
|
|
|
|
|
|
# 1 1 1 10 100 11 |
106
|
|
|
|
|
|
|
# 2 10 11 10 100 101 |
107
|
|
|
|
|
|
|
# 3 11 101 100 10000 1001 |
108
|
|
|
|
|
|
|
# 4 100 1001 100 10000 1101 |
109
|
|
|
|
|
|
|
# 5 101 1101 100 10000 10001 |
110
|
|
|
|
|
|
|
# 6 110 10001 1000 1000000 11001 |
111
|
|
|
|
|
|
|
# 7 111 11001 1000 1000000 100001 |
112
|
|
|
|
|
|
|
# 8 1000 100001 1000 1000000 101001 |
113
|
|
|
|
|
|
|
# 9 1001 101001 1000 1000000 110001 |
114
|
|
|
|
|
|
|
# 10 1010 110001 1000 1000000 111001 |
115
|
|
|
|
|
|
|
# 11 1011 111001 1000 1000000 1000001 |
116
|
|
|
|
|
|
|
# 12 1100 1000001 10000 100000000 1010001 |
117
|
|
|
|
|
|
|
# 13 1101 1010001 10000 100000000 1100001 |
118
|
|
|
|
|
|
|
# 14 1110 1100001 10000 100000000 1110001 |
119
|
|
|
|
|
|
|
# 15 1111 1110001 10000 100000000 10000001 |
120
|
|
|
|
|
|
|
# 16 10000 10000001 10000 100000000 10010001 |
121
|
|
|
|
|
|
|
# |
122
|
|
|
|
|
|
|
sub seek_to_i { |
123
|
575
|
|
|
575
|
1
|
3117
|
my ($self, $i) = @_; |
124
|
575
|
|
|
|
|
776
|
$self->{'i'} = $i; |
125
|
|
|
|
|
|
|
|
126
|
575
|
|
|
|
|
1485
|
my ($pow) = _round_down_pow($i, 2); # i = 1zxxx |
127
|
575
|
|
|
|
|
1172
|
$i -= $pow; # without high bit, so i=zxxx |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# $zpow is the z bit position, or 1 if $i==1 |
130
|
575
|
100
|
|
|
|
1208
|
my $zpow = ($pow >= 2 ? $pow/2 : 0); |
131
|
|
|
|
|
|
|
|
132
|
575
|
100
|
|
|
|
1107
|
if ($i >= $zpow) { |
133
|
|
|
|
|
|
|
# z=1 so return value=1xxx 0 0001, extra low 0-bit |
134
|
278
|
|
|
|
|
349
|
$pow *= 2; |
135
|
|
|
|
|
|
|
} else { |
136
|
|
|
|
|
|
|
# z=0 so return value=1xxx 0001, turn on 1-bit in i |
137
|
297
|
|
|
|
|
371
|
$i += $zpow; # i=0xxx becomes i=1xxx |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
575
|
50
|
|
|
|
1676
|
if ($pow*$pow >= $uv_limit) { |
141
|
0
|
|
|
|
|
0
|
$pow = Math::NumSeq::_to_bigint($pow); |
142
|
|
|
|
|
|
|
} |
143
|
575
|
|
|
|
|
1305
|
$self->{'value'} = $pow*$i + 1; |
144
|
575
|
|
|
|
|
990
|
$self->{'inc'} = $pow; |
145
|
575
|
|
|
|
|
1216
|
$self->{'limit'} = $pow*$pow; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub ith { |
149
|
162
|
|
|
162
|
1
|
402
|
my ($self, $i) = @_; |
150
|
|
|
|
|
|
|
### ProthNumbers ith(): $i |
151
|
|
|
|
|
|
|
|
152
|
162
|
|
|
|
|
201
|
$i += 1; |
153
|
162
|
|
|
|
|
413
|
my ($pow) = _round_down_pow($i, 2); # i = 1zxxx |
154
|
162
|
|
|
|
|
222
|
$i -= $pow; # without high bit, so i=zxxx |
155
|
|
|
|
|
|
|
|
156
|
162
|
|
|
|
|
185
|
my $zpow = $pow/2; |
157
|
162
|
100
|
|
|
|
271
|
if ($i >= $zpow) { # test z bit |
158
|
|
|
|
|
|
|
# z=1 so return value=1xxx 0 0001, extra low 0-bit |
159
|
67
|
|
|
|
|
93
|
$pow *= 2; |
160
|
|
|
|
|
|
|
} else { |
161
|
|
|
|
|
|
|
# z=1 so return value=1xxx 0001, turn on 1-bit in i |
162
|
95
|
|
|
|
|
135
|
$i += $zpow; # i=0xxx becomes i=1xxx |
163
|
|
|
|
|
|
|
} |
164
|
162
|
|
|
|
|
450
|
return $i*$pow + 1; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub pred { |
168
|
2043
|
|
|
2043
|
1
|
9040
|
my ($self, $value) = @_; |
169
|
|
|
|
|
|
|
### ProthNumbers pred(): $value |
170
|
|
|
|
|
|
|
{ |
171
|
2043
|
|
|
|
|
2085
|
my $int = int($value); |
|
2043
|
|
|
|
|
2196
|
|
172
|
2043
|
100
|
|
|
|
3400
|
if ($value != $int) { return 0; } |
|
990
|
|
|
|
|
1703
|
|
173
|
1053
|
|
|
|
|
1262
|
$value = $int; |
174
|
|
|
|
|
|
|
} |
175
|
1053
|
100
|
66
|
|
|
4321
|
unless ($value >= 3 |
|
|
|
66
|
|
|
|
|
176
|
|
|
|
|
|
|
&& ($value % 2) |
177
|
|
|
|
|
|
|
&& ! _is_infinite($value)) { |
178
|
500
|
|
|
|
|
1172
|
return 0; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# ENHANCE-ME: maybe BigInt as_bin() and string match position of lowest 1 |
182
|
|
|
|
|
|
|
# ENHANCE-ME: maybe (v^(v-1))+1 for lowest 1, ask if v < m*m |
183
|
|
|
|
|
|
|
# |
184
|
553
|
|
|
|
|
837
|
my $pow = ($value*0) + 2; # inherit bignum 2 |
185
|
553
|
|
|
|
|
555
|
for (;;) { |
186
|
|
|
|
|
|
|
### at: "$value $pow" |
187
|
1200
|
|
|
|
|
1244
|
$value = int($value/2); |
188
|
1200
|
100
|
|
|
|
2188
|
if ($value < $pow) { |
189
|
103
|
|
|
|
|
205
|
return 1; |
190
|
|
|
|
|
|
|
} |
191
|
1097
|
100
|
|
|
|
1801
|
if ($value % 2) { |
192
|
450
|
|
|
|
|
916
|
return ($value < $pow); |
193
|
|
|
|
|
|
|
} |
194
|
647
|
|
|
|
|
688
|
$pow *= 2; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# use 41/29 ~= sqrt(2) in case $value is a Math::BigInt, since can't |
199
|
|
|
|
|
|
|
# multiply BigInt*float |
200
|
|
|
|
|
|
|
# |
201
|
|
|
|
|
|
|
sub value_to_i_estimate { |
202
|
62
|
|
|
62
|
1
|
903
|
my ($self, $value) = @_; |
203
|
62
|
100
|
|
|
|
97
|
if ($value < 0) { return 0; } |
|
6
|
|
|
|
|
9
|
|
204
|
56
|
|
|
|
|
323
|
my $est = int(sqrt(int($value)) * 41 / 29); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
1; |
208
|
|
|
|
|
|
|
__END__ |