line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Prime::Util; |
2
|
72
|
|
|
72
|
|
4280720
|
use strict; |
|
72
|
|
|
|
|
731
|
|
|
72
|
|
|
|
|
2074
|
|
3
|
72
|
|
|
72
|
|
377
|
use warnings; |
|
72
|
|
|
|
|
137
|
|
|
72
|
|
|
|
|
2222
|
|
4
|
72
|
|
|
72
|
|
483
|
use Carp qw/croak confess carp/; |
|
72
|
|
|
|
|
141
|
|
|
72
|
|
|
|
|
5874
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
BEGIN { |
7
|
72
|
|
|
72
|
|
277
|
$Math::Prime::Util::AUTHORITY = 'cpan:DANAJ'; |
8
|
72
|
|
|
|
|
1893
|
$Math::Prime::Util::VERSION = '0.73'; |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# parent is cleaner, and in the Perl 5.10.1 / 5.12.0 core, but not earlier. |
12
|
|
|
|
|
|
|
# use parent qw( Exporter ); |
13
|
72
|
|
|
72
|
|
463
|
use base qw( Exporter ); |
|
72
|
|
|
|
|
187
|
|
|
72
|
|
|
|
|
26065
|
|
14
|
|
|
|
|
|
|
our @EXPORT_OK = |
15
|
|
|
|
|
|
|
qw( prime_get_config prime_set_config |
16
|
|
|
|
|
|
|
prime_precalc prime_memfree |
17
|
|
|
|
|
|
|
is_prime is_prob_prime is_provable_prime is_provable_prime_with_cert |
18
|
|
|
|
|
|
|
prime_certificate verify_prime |
19
|
|
|
|
|
|
|
is_pseudoprime is_euler_pseudoprime is_strong_pseudoprime |
20
|
|
|
|
|
|
|
is_euler_plumb_pseudoprime |
21
|
|
|
|
|
|
|
is_lucas_pseudoprime |
22
|
|
|
|
|
|
|
is_strong_lucas_pseudoprime |
23
|
|
|
|
|
|
|
is_extra_strong_lucas_pseudoprime |
24
|
|
|
|
|
|
|
is_almost_extra_strong_lucas_pseudoprime |
25
|
|
|
|
|
|
|
is_frobenius_pseudoprime |
26
|
|
|
|
|
|
|
is_frobenius_underwood_pseudoprime is_frobenius_khashin_pseudoprime |
27
|
|
|
|
|
|
|
is_perrin_pseudoprime is_catalan_pseudoprime |
28
|
|
|
|
|
|
|
is_aks_prime is_bpsw_prime is_ramanujan_prime is_mersenne_prime |
29
|
|
|
|
|
|
|
is_power is_prime_power is_pillai is_semiprime is_square is_polygonal |
30
|
|
|
|
|
|
|
is_square_free is_primitive_root is_carmichael is_quasi_carmichael |
31
|
|
|
|
|
|
|
is_fundamental is_totient |
32
|
|
|
|
|
|
|
sqrtint rootint logint |
33
|
|
|
|
|
|
|
miller_rabin_random |
34
|
|
|
|
|
|
|
lucas_sequence lucasu lucasv |
35
|
|
|
|
|
|
|
primes twin_primes semi_primes ramanujan_primes |
36
|
|
|
|
|
|
|
sieve_prime_cluster sieve_range |
37
|
|
|
|
|
|
|
forprimes forcomposites foroddcomposites forsemiprimes fordivisors |
38
|
|
|
|
|
|
|
forpart forcomp forcomb forperm forderange formultiperm forsetproduct |
39
|
|
|
|
|
|
|
forfactored forsquarefree |
40
|
|
|
|
|
|
|
lastfor |
41
|
|
|
|
|
|
|
numtoperm permtonum randperm shuffle |
42
|
|
|
|
|
|
|
prime_iterator prime_iterator_object |
43
|
|
|
|
|
|
|
next_prime prev_prime |
44
|
|
|
|
|
|
|
prime_count |
45
|
|
|
|
|
|
|
prime_count_lower prime_count_upper prime_count_approx |
46
|
|
|
|
|
|
|
nth_prime nth_prime_lower nth_prime_upper nth_prime_approx inverse_li |
47
|
|
|
|
|
|
|
twin_prime_count twin_prime_count_approx |
48
|
|
|
|
|
|
|
nth_twin_prime nth_twin_prime_approx |
49
|
|
|
|
|
|
|
semiprime_count semiprime_count_approx |
50
|
|
|
|
|
|
|
nth_semiprime nth_semiprime_approx |
51
|
|
|
|
|
|
|
ramanujan_prime_count ramanujan_prime_count_approx |
52
|
|
|
|
|
|
|
ramanujan_prime_count_lower ramanujan_prime_count_upper |
53
|
|
|
|
|
|
|
nth_ramanujan_prime nth_ramanujan_prime_approx |
54
|
|
|
|
|
|
|
nth_ramanujan_prime_lower nth_ramanujan_prime_upper |
55
|
|
|
|
|
|
|
sum_primes print_primes |
56
|
|
|
|
|
|
|
random_prime random_ndigit_prime random_nbit_prime random_strong_prime |
57
|
|
|
|
|
|
|
random_proven_prime random_proven_prime_with_cert |
58
|
|
|
|
|
|
|
random_maurer_prime random_maurer_prime_with_cert |
59
|
|
|
|
|
|
|
random_shawe_taylor_prime random_shawe_taylor_prime_with_cert |
60
|
|
|
|
|
|
|
random_semiprime random_unrestricted_semiprime |
61
|
|
|
|
|
|
|
random_factored_integer |
62
|
|
|
|
|
|
|
primorial pn_primorial consecutive_integer_lcm gcdext chinese |
63
|
|
|
|
|
|
|
gcd lcm factor factor_exp divisors valuation hammingweight |
64
|
|
|
|
|
|
|
todigits fromdigits todigitstring sumdigits |
65
|
|
|
|
|
|
|
invmod sqrtmod addmod mulmod divmod powmod |
66
|
|
|
|
|
|
|
vecsum vecmin vecmax vecprod vecreduce vecextract |
67
|
|
|
|
|
|
|
vecany vecall vecnotall vecnone vecfirst vecfirstidx |
68
|
|
|
|
|
|
|
moebius mertens euler_phi jordan_totient exp_mangoldt liouville |
69
|
|
|
|
|
|
|
partitions bernfrac bernreal harmfrac harmreal |
70
|
|
|
|
|
|
|
chebyshev_theta chebyshev_psi |
71
|
|
|
|
|
|
|
divisor_sum carmichael_lambda kronecker hclassno inverse_totient |
72
|
|
|
|
|
|
|
ramanujan_tau ramanujan_sum |
73
|
|
|
|
|
|
|
binomial stirling znorder znprimroot znlog legendre_phi |
74
|
|
|
|
|
|
|
factorial factorialmod |
75
|
|
|
|
|
|
|
ExponentialIntegral LogarithmicIntegral RiemannZeta RiemannR LambertW Pi |
76
|
|
|
|
|
|
|
irand irand64 drand urandomb urandomm csrand random_bytes entropy_bytes |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [ @EXPORT_OK ], |
79
|
|
|
|
|
|
|
rand => [qw/srand rand irand irand64/], |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# These are only exported if specifically asked for |
83
|
|
|
|
|
|
|
push @EXPORT_OK, (qw/trial_factor fermat_factor holf_factor lehman_factor squfof_factor prho_factor pbrent_factor pminus1_factor pplus1_factor ecm_factor rand srand/); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my %_Config; |
86
|
|
|
|
|
|
|
my %_GMPfunc; # List of available MPU::GMP functions |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Similar to how boolean handles its option |
89
|
|
|
|
|
|
|
sub import { |
90
|
154
|
50
|
|
154
|
|
1011
|
if ($] < 5.020) { # Prevent "used only once" warnings |
91
|
0
|
|
|
|
|
0
|
my $pkg = caller; |
92
|
72
|
|
|
72
|
|
550
|
no strict 'refs'; ## no critic(strict) |
|
72
|
|
|
|
|
159
|
|
|
72
|
|
|
|
|
15244
|
|
93
|
0
|
|
|
|
|
0
|
${"${pkg}::a"} = ${"${pkg}::a"}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
94
|
0
|
|
|
|
|
0
|
${"${pkg}::b"} = ${"${pkg}::b"}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
95
|
|
|
|
|
|
|
} |
96
|
154
|
|
|
|
|
390
|
foreach my $opt (qw/nobigint secure/) { |
97
|
308
|
|
|
|
|
1406
|
my @options = grep $_ ne "-$opt", @_; |
98
|
308
|
50
|
|
|
|
978
|
$_Config{$opt} = 1 if @options != @_; |
99
|
308
|
|
|
|
|
1014
|
@_ = @options; |
100
|
|
|
|
|
|
|
} |
101
|
154
|
50
|
33
|
|
|
1030
|
_XS_set_secure() if $_Config{'xs'} && $_Config{'secure'}; |
102
|
154
|
|
|
|
|
125857
|
goto &Exporter::import; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
############################################################################# |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
BEGIN { |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Separate lines to keep compatible with default from 5.6.2. |
111
|
|
|
|
|
|
|
# We could alternately use Config's $Config{uvsize} for MAXBITS |
112
|
72
|
|
|
72
|
|
556
|
use constant OLD_PERL_VERSION=> $] < 5.008; |
|
72
|
|
|
|
|
151
|
|
|
72
|
|
|
|
|
7911
|
|
113
|
72
|
|
|
72
|
|
516
|
use constant MPU_MAXBITS => (~0 == 4294967295) ? 32 : 64; |
|
72
|
|
|
|
|
170
|
|
|
72
|
|
|
|
|
4538
|
|
114
|
72
|
|
|
72
|
|
442
|
use constant MPU_32BIT => MPU_MAXBITS == 32; |
|
72
|
|
|
|
|
164
|
|
|
72
|
|
|
|
|
3937
|
|
115
|
72
|
|
|
72
|
|
423
|
use constant MPU_MAXPARAM => MPU_32BIT ? 4294967295 : 18446744073709551615; |
|
72
|
|
|
|
|
146
|
|
|
72
|
|
|
|
|
3964
|
|
116
|
72
|
|
|
72
|
|
430
|
use constant MPU_MAXDIGITS => MPU_32BIT ? 10 : 20; |
|
72
|
|
|
|
|
183
|
|
|
72
|
|
|
|
|
3901
|
|
117
|
72
|
|
|
72
|
|
422
|
use constant MPU_MAXPRIME => MPU_32BIT ? 4294967291 : 18446744073709551557; |
|
72
|
|
|
|
|
132
|
|
|
72
|
|
|
|
|
3601
|
|
118
|
72
|
|
|
72
|
|
417
|
use constant MPU_MAXPRIMEIDX => MPU_32BIT ? 203280221 : 425656284035217743; |
|
72
|
|
|
|
|
141
|
|
|
72
|
|
|
|
|
3969
|
|
119
|
72
|
|
|
72
|
|
482
|
use constant UVPACKLET => MPU_32BIT ? 'L' : 'Q'; |
|
72
|
|
|
|
|
155
|
|
|
72
|
|
|
|
|
4368
|
|
120
|
72
|
|
|
72
|
|
466
|
use constant INTMAX => (!OLD_PERL_VERSION || MPU_32BIT) ? ~0 : 562949953421312; |
|
72
|
|
|
|
|
156
|
|
|
72
|
|
|
|
|
28435
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
eval { |
123
|
72
|
50
|
33
|
|
|
599
|
return 0 if defined $ENV{MPU_NO_XS} && $ENV{MPU_NO_XS} == 1; |
124
|
72
|
|
|
|
|
456
|
require XSLoader; |
125
|
72
|
|
|
|
|
100818
|
XSLoader::load(__PACKAGE__, $Math::Prime::Util::VERSION); |
126
|
72
|
|
|
|
|
7940
|
prime_precalc(0); |
127
|
72
|
|
|
|
|
340
|
$_Config{'maxbits'} = _XS_prime_maxbits(); |
128
|
72
|
|
|
|
|
143
|
$_Config{'xs'} = 1; |
129
|
72
|
|
|
|
|
343
|
1; |
130
|
72
|
50
|
|
72
|
|
268
|
} or do { |
131
|
|
|
|
|
|
|
carp "Using Pure Perl implementation: $@" |
132
|
0
|
0
|
0
|
|
|
0
|
unless defined $ENV{MPU_NO_XS} && $ENV{MPU_NO_XS} == 1; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
0
|
$_Config{'xs'} = 0; |
135
|
0
|
|
|
|
|
0
|
$_Config{'maxbits'} = MPU_MAXBITS; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Load PP front end code |
138
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PPFE; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Init rand |
141
|
0
|
|
|
|
|
0
|
Math::Prime::Util::csrand(); |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
0
|
*prime_count = \&Math::Prime::Util::_generic_prime_count; |
144
|
0
|
|
|
|
|
0
|
*factor = \&Math::Prime::Util::_generic_factor; |
145
|
0
|
|
|
|
|
0
|
*factor_exp = \&Math::Prime::Util::_generic_factor_exp; |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
|
148
|
72
|
|
|
|
|
182
|
$_Config{'secure'} = 0; |
149
|
72
|
|
|
|
|
176
|
$_Config{'nobigint'} = 0; |
150
|
72
|
|
|
|
|
160
|
$_Config{'gmp'} = 0; |
151
|
|
|
|
|
|
|
# See if they have the GMP module and haven't requested it not to be used. |
152
|
72
|
50
|
33
|
|
|
493
|
if (!defined $ENV{MPU_NO_GMP} || $ENV{MPU_NO_GMP} != 1) { |
153
|
72
|
50
|
|
|
|
203
|
if (eval { require Math::Prime::Util::GMP; |
|
72
|
|
|
|
|
11510
|
|
154
|
0
|
|
|
|
|
0
|
Math::Prime::Util::GMP->import(); |
155
|
0
|
|
|
|
|
0
|
1; }) { |
156
|
0
|
|
|
|
|
0
|
$_Config{'gmp'} = int(100*$Math::Prime::Util::GMP::VERSION); |
157
|
|
|
|
|
|
|
} |
158
|
72
|
|
|
|
|
398
|
for my $e (@Math::Prime::Util::GMP::EXPORT_OK) { |
159
|
0
|
|
|
|
|
0
|
$Math::Prime::Util::_GMPfunc{"$e"} = $_Config{'gmp'}; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
# If we have GMP, it is not seeded properly but we are, seed with our data. |
162
|
72
|
0
|
33
|
|
|
447965
|
if ( $_Config{'gmp'} >= 42 |
|
|
|
33
|
|
|
|
|
163
|
|
|
|
|
|
|
&& !Math::Prime::Util::GMP::is_csprng_well_seeded() |
164
|
|
|
|
|
|
|
&& Math::Prime::Util::_is_csprng_well_seeded()) { |
165
|
0
|
|
|
|
|
0
|
Math::Prime::Util::GMP::seed_csprng(256, random_bytes(256)); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
croak "Perl and XS don't agree on bit size" |
171
|
|
|
|
|
|
|
if $_Config{'xs'} && MPU_MAXBITS != _XS_prime_maxbits(); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
$_Config{'maxparam'} = MPU_MAXPARAM; |
174
|
|
|
|
|
|
|
$_Config{'maxdigits'} = MPU_MAXDIGITS; |
175
|
|
|
|
|
|
|
$_Config{'maxprime'} = MPU_MAXPRIME; |
176
|
|
|
|
|
|
|
$_Config{'maxprimeidx'} = MPU_MAXPRIMEIDX; |
177
|
|
|
|
|
|
|
$_Config{'assume_rh'} = 0; |
178
|
|
|
|
|
|
|
$_Config{'verbose'} = 0; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# used for code like: |
181
|
|
|
|
|
|
|
# return _XS_foo($n) if $n <= $_XS_MAXVAL |
182
|
|
|
|
|
|
|
# which builds into one scalar whether XS is available and if we can call it. |
183
|
|
|
|
|
|
|
my $_XS_MAXVAL = $_Config{'xs'} ? MPU_MAXPARAM : -1; |
184
|
|
|
|
|
|
|
my $_HAVE_GMP = $_Config{'gmp'}; |
185
|
|
|
|
|
|
|
_XS_set_callgmp($_HAVE_GMP) if $_Config{'xs'}; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Infinity in Perl is rather O/S specific. |
188
|
|
|
|
|
|
|
our $_Infinity = 0+'inf'; |
189
|
|
|
|
|
|
|
$_Infinity = 20**20**20 if 65535 > $_Infinity; # E.g. Windows |
190
|
|
|
|
|
|
|
our $_Neg_Infinity = -$_Infinity; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub prime_get_config { |
193
|
2263
|
|
|
2263
|
1
|
56036
|
my %config = %_Config; |
194
|
|
|
|
|
|
|
|
195
|
2263
|
100
|
|
|
|
9367
|
$config{'precalc_to'} = ($_Config{'xs'}) |
196
|
|
|
|
|
|
|
? _get_prime_cache_size() |
197
|
|
|
|
|
|
|
: Math::Prime::Util::PP::_get_prime_cache_size(); |
198
|
|
|
|
|
|
|
|
199
|
2263
|
|
|
|
|
6843
|
return \%config; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# Note: You can cause yourself pain if you turn on xs or gmp when they're not |
203
|
|
|
|
|
|
|
# loaded. Your calls will probably die horribly. |
204
|
|
|
|
|
|
|
sub prime_set_config { |
205
|
20
|
|
|
20
|
1
|
126308
|
my %params = (@_); # no defaults |
206
|
20
|
|
|
|
|
98
|
foreach my $param (keys %params) { |
207
|
23
|
|
|
|
|
71
|
my $value = $params{$param}; |
208
|
23
|
|
|
|
|
91
|
$param = lc $param; |
209
|
|
|
|
|
|
|
# dispatch table should go here. |
210
|
23
|
100
|
66
|
|
|
374
|
if ($param eq 'xs') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
211
|
2
|
100
|
|
|
|
10
|
$_Config{'xs'} = ($value) ? 1 : 0; |
212
|
2
|
100
|
|
|
|
16
|
$_XS_MAXVAL = $_Config{'xs'} ? MPU_MAXPARAM : -1; |
213
|
|
|
|
|
|
|
} elsif ($param eq 'gmp') { |
214
|
2
|
50
|
|
|
|
6
|
$_HAVE_GMP = ($value) ? int(100*$Math::Prime::Util::GMP::VERSION) : 0; |
215
|
2
|
|
|
|
|
6
|
$_Config{'gmp'} = $_HAVE_GMP; |
216
|
|
|
|
|
|
|
$Math::Prime::Util::_GMPfunc{$_} = $_HAVE_GMP |
217
|
2
|
|
|
|
|
7
|
for keys %Math::Prime::Util::_GMPfunc; |
218
|
2
|
100
|
|
|
|
12
|
_XS_set_callgmp($_HAVE_GMP) if $_Config{'xs'}; |
219
|
|
|
|
|
|
|
} elsif ($param eq 'nobigint') { |
220
|
2
|
100
|
|
|
|
9
|
$_Config{'nobigint'} = ($value) ? 1 : 0; |
221
|
|
|
|
|
|
|
} elsif ($param eq 'secure') { |
222
|
0
|
0
|
0
|
|
|
0
|
croak "Cannot disable secure once set" if !$value && $_Config{'secure'}; |
223
|
0
|
0
|
|
|
|
0
|
if ($value) { |
224
|
0
|
|
|
|
|
0
|
$_Config{'secure'} = 1; |
225
|
0
|
0
|
|
|
|
0
|
_XS_set_secure() if $_Config{'xs'}; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
} elsif ($param eq 'irand') { |
228
|
0
|
|
|
|
|
0
|
carp "ntheory irand option is deprecated"; |
229
|
|
|
|
|
|
|
} elsif ($param eq 'use_primeinc') { |
230
|
0
|
|
|
|
|
0
|
carp "ntheory use_primeinc option is deprecated"; |
231
|
|
|
|
|
|
|
} elsif ($param =~ /^(assume[_ ]?)?[ge]?rh$/ || $param =~ /riemann\s*h/) { |
232
|
8
|
100
|
|
|
|
44
|
$_Config{'assume_rh'} = ($value) ? 1 : 0; |
233
|
|
|
|
|
|
|
} elsif ($param eq 'verbose') { |
234
|
9
|
50
|
|
|
|
65
|
if ($value =~ /^\d+$/) { } |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
235
|
0
|
|
|
|
|
0
|
elsif ($value =~ /^[ty]/i) { $value = 1; } |
236
|
0
|
|
|
|
|
0
|
elsif ($value =~ /^[fn]/i) { $value = 0; } |
237
|
0
|
|
|
|
|
0
|
else { croak("Invalid setting for verbose. 0, 1, 2, etc."); } |
238
|
9
|
|
|
|
|
30
|
$_Config{'verbose'} = $value; |
239
|
9
|
100
|
|
|
|
90
|
_XS_set_verbose($value) if $_Config{'xs'}; |
240
|
9
|
50
|
|
|
|
39
|
Math::Prime::Util::GMP::_GMP_set_verbose($value) if $_Config{'gmp'}; |
241
|
|
|
|
|
|
|
} else { |
242
|
0
|
|
|
|
|
0
|
croak "Unknown or invalid configuration setting: $param\n"; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
} |
245
|
20
|
|
|
|
|
226
|
1; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub _bigint_to_int { |
249
|
18
|
|
|
18
|
|
4098
|
return (OLD_PERL_VERSION) ? unpack(UVPACKLET,pack(UVPACKLET,$_[0]->bstr)) |
250
|
|
|
|
|
|
|
: int($_[0]->bstr); |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
sub _to_bigint { |
253
|
7451
|
50
|
|
7451
|
|
24933
|
do { require Math::BigInt; Math::BigInt->import(try=>"GMP,Pari"); } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
254
|
|
|
|
|
|
|
unless defined $Math::BigInt::VERSION; |
255
|
7451
|
100
|
66
|
|
|
71727
|
return (!defined($_[0]) || ref($_[0]) eq 'Math::BigInt') ? $_[0] : Math::BigInt->new("$_[0]"); |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
sub _to_gmpz { |
258
|
0
|
0
|
|
0
|
|
0
|
do { require Math::GMPz; } unless defined $Math::GMPz::VERSION; |
|
0
|
|
|
|
|
0
|
|
259
|
0
|
0
|
|
|
|
0
|
return (ref($_[0]) eq 'Math::GMPz') ? $_[0] : Math::GMPz->new($_[0]); |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
sub _to_gmp { |
262
|
0
|
0
|
|
0
|
|
0
|
do { require Math::GMP; } unless defined $Math::GMP::VERSION; |
|
0
|
|
|
|
|
0
|
|
263
|
0
|
0
|
|
|
|
0
|
return (ref($_[0]) eq 'Math::GMP') ? $_[0] : Math::GMP->new($_[0]); |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
sub _reftyped { |
266
|
625
|
50
|
|
625
|
|
2250
|
return unless defined $_[1]; |
267
|
625
|
|
|
|
|
848
|
my $ref0 = ref($_[0]); |
268
|
625
|
100
|
|
|
|
954
|
if ($ref0) { |
269
|
59
|
100
|
|
|
|
229
|
return ($ref0 eq ref($_[1])) ? $_[1] : $ref0->new("$_[1]"); |
270
|
|
|
|
|
|
|
} |
271
|
566
|
50
|
|
|
|
831
|
if (OLD_PERL_VERSION) { |
272
|
|
|
|
|
|
|
# Perl 5.6 truncates arguments to doubles if you look at them funny |
273
|
|
|
|
|
|
|
return "$_[1]" if "$_[1]" <= INTMAX; |
274
|
0
|
|
|
|
|
0
|
} elsif ($_[1] >= 0) { |
275
|
|
|
|
|
|
|
# TODO: This wasn't working right in 5.20.0-RC1, verify correct |
276
|
566
|
50
|
|
|
|
1092
|
return $_[1] if $_[1] <= ~0; |
277
|
|
|
|
|
|
|
} else { |
278
|
0
|
0
|
|
|
|
0
|
return $_[1] if ''.int($_[1]) >= -(~0>>1); |
279
|
|
|
|
|
|
|
} |
280
|
0
|
0
|
|
|
|
0
|
do { require Math::BigInt; Math::BigInt->import(try=>"GMP,Pari"); } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
281
|
|
|
|
|
|
|
unless defined $Math::BigInt::VERSION; |
282
|
0
|
|
|
|
|
0
|
return Math::BigInt->new("$_[1]"); |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
#*_validate_positive_integer = \&Math::Prime::Util::PP::_validate_positive_integer; |
287
|
|
|
|
|
|
|
sub _validate_positive_integer { |
288
|
103
|
|
|
103
|
|
3187
|
my($n, $min, $max) = @_; |
289
|
103
|
50
|
|
|
|
345
|
croak "Parameter must be defined" if !defined $n; |
290
|
103
|
50
|
|
|
|
385
|
if (ref($n) eq 'CODE') { |
291
|
0
|
|
|
|
|
0
|
$_[0] = $_[0]->(); |
292
|
0
|
|
|
|
|
0
|
$n = $_[0]; |
293
|
|
|
|
|
|
|
} |
294
|
103
|
100
|
|
|
|
343
|
if (ref($n) eq 'Math::BigInt') { |
|
|
50
|
|
|
|
|
|
295
|
90
|
50
|
33
|
|
|
344
|
croak "Parameter '$n' must be a positive integer" |
296
|
|
|
|
|
|
|
if $n->sign() ne '+' || !$n->is_int(); |
297
|
90
|
50
|
|
|
|
1812
|
$_[0] = _bigint_to_int($_[0]) if $n <= '' . INTMAX; |
298
|
|
|
|
|
|
|
} elsif (ref($n) eq 'Math::GMPz') { |
299
|
0
|
0
|
|
|
|
0
|
croak "Parameter '$n' must be a positive integer" if Math::GMPz::Rmpz_sgn($n) < 0; |
300
|
0
|
0
|
|
|
|
0
|
$_[0] = _bigint_to_int($_[0]) if $n <= INTMAX; |
301
|
|
|
|
|
|
|
} else { |
302
|
13
|
|
|
|
|
35
|
my $strn = "$n"; |
303
|
13
|
50
|
33
|
|
|
83
|
croak "Parameter '$strn' must be a positive integer" |
|
|
|
33
|
|
|
|
|
304
|
|
|
|
|
|
|
if $strn eq '' || ($strn =~ tr/0123456789//c && $strn !~ /^\+?\d+$/); |
305
|
13
|
100
|
|
|
|
41
|
if ($n <= INTMAX) { |
306
|
7
|
50
|
|
|
|
12
|
$_[0] = $strn if ref($n); |
307
|
|
|
|
|
|
|
} else { |
308
|
|
|
|
|
|
|
#$_[0] = Math::BigInt->new($strn) |
309
|
6
|
|
|
|
|
25
|
$_[0] = _to_bigint($strn); |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
} |
312
|
103
|
50
|
66
|
|
|
13344
|
$_[0]->upgrade(undef) if ref($_[0]) eq 'Math::BigInt' && $_[0]->upgrade(); |
313
|
103
|
50
|
33
|
|
|
1487
|
croak "Parameter '$_[0]' must be >= $min" if defined $min && $_[0] < $min; |
314
|
103
|
50
|
33
|
|
|
316
|
croak "Parameter '$_[0]' must be <= $max" if defined $max && $_[0] > $max; |
315
|
103
|
|
|
|
|
204
|
1; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
############################################################################# |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
# These are called by the XS code to keep the GMP CSPRNG in sync with us. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
sub _srand_p { |
323
|
0
|
|
|
0
|
|
0
|
my($seedval) = @_; |
324
|
0
|
0
|
|
|
|
0
|
return unless $_Config{'gmp'} >= 42; |
325
|
0
|
0
|
|
|
|
0
|
$seedval = unpack("L",entropy_bytes(4)) unless defined $seedval; |
326
|
0
|
|
|
|
|
0
|
Math::Prime::Util::GMP::seed_csprng(4, pack("L",$seedval)); |
327
|
0
|
|
|
|
|
0
|
$seedval; |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
sub _csrand_p { |
331
|
0
|
|
|
0
|
|
0
|
my($str) = @_; |
332
|
0
|
0
|
|
|
|
0
|
return unless $_Config{'gmp'} >= 42; |
333
|
0
|
0
|
|
|
|
0
|
$str = entropy_bytes(256) unless defined $str; |
334
|
0
|
|
|
|
|
0
|
Math::Prime::Util::GMP::seed_csprng(length($str), $str); |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
############################################################################# |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
sub primes { |
340
|
1153
|
|
|
1153
|
1
|
34780
|
my($low,$high) = @_; |
341
|
1153
|
100
|
|
|
|
2045
|
if (scalar @_ > 1) { |
342
|
117
|
100
|
|
|
|
417
|
_validate_num($low) || _validate_positive_integer($low); |
343
|
|
|
|
|
|
|
} else { |
344
|
1036
|
|
|
|
|
1612
|
($low,$high) = (2, $low); |
345
|
|
|
|
|
|
|
} |
346
|
1146
|
100
|
|
|
|
2656
|
_validate_num($high) || _validate_positive_integer($high); |
347
|
|
|
|
|
|
|
|
348
|
1138
|
|
|
|
|
1756
|
my $sref = []; |
349
|
1138
|
100
|
100
|
|
|
3448
|
return $sref if ($low > $high) || ($high < 2); |
350
|
|
|
|
|
|
|
|
351
|
1128
|
100
|
|
|
|
2053
|
if ($high > $_XS_MAXVAL) { |
352
|
1
|
50
|
|
|
|
128
|
if ($_HAVE_GMP) { |
353
|
0
|
|
|
|
|
0
|
$sref = Math::Prime::Util::GMP::primes($low,$high); |
354
|
0
|
0
|
|
|
|
0
|
if ($high > ~0) { |
355
|
|
|
|
|
|
|
# Convert the returned strings into BigInts |
356
|
0
|
|
|
|
|
0
|
@$sref = map { _reftyped($_[0],$_) } @$sref; |
|
0
|
|
|
|
|
0
|
|
357
|
|
|
|
|
|
|
} else { |
358
|
0
|
|
|
|
|
0
|
@$sref = map { int($_) } @$sref; |
|
0
|
|
|
|
|
0
|
|
359
|
|
|
|
|
|
|
} |
360
|
0
|
|
|
|
|
0
|
return $sref; |
361
|
|
|
|
|
|
|
} |
362
|
1
|
|
|
|
|
10
|
require Math::Prime::Util::PP; |
363
|
1
|
|
|
|
|
6
|
return Math::Prime::Util::PP::primes($low,$high); |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
# Decide the method to use. We have four to choose from: |
367
|
|
|
|
|
|
|
# 1. Trial No memory, no overhead, but more time per prime. |
368
|
|
|
|
|
|
|
# 2. Sieve Monolithic cached sieve. |
369
|
|
|
|
|
|
|
# 3. Erat Monolithic uncached sieve. |
370
|
|
|
|
|
|
|
# 4. Segment Segment sieve. Never a bad decision. |
371
|
|
|
|
|
|
|
|
372
|
1127
|
100
|
66
|
|
|
4015
|
if (($low+1) >= $high || # Tiny range, or |
|
|
100
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
373
|
|
|
|
|
|
|
$high > 10**14 && ($high-$low) < 50000) { # Small relative range |
374
|
|
|
|
|
|
|
|
375
|
18
|
|
|
|
|
294
|
$sref = trial_primes($low, $high); |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
} elsif ($high <= (65536*30) || # Very small, or |
378
|
|
|
|
|
|
|
$high <= _get_prime_cache_size()) { # already in the main cache. |
379
|
|
|
|
|
|
|
|
380
|
1100
|
|
|
|
|
56309
|
$sref = sieve_primes($low, $high); |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
} else { |
383
|
|
|
|
|
|
|
|
384
|
9
|
|
|
|
|
22154
|
$sref = segment_primes($low, $high); |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
# We could return an array ref in scalar context, array in list context with: |
389
|
|
|
|
|
|
|
# return (wantarray) ? @{$sref} : $sref; |
390
|
|
|
|
|
|
|
# but I think the dual interface could be confusing, albeit often handy. |
391
|
1127
|
|
|
|
|
12642
|
return $sref; |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
# Shortcut for primes returning an array instead of array reference. |
395
|
|
|
|
|
|
|
# sub aprimes { @{primes(@_)}; } |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub twin_primes { |
398
|
20
|
|
|
20
|
1
|
8048
|
my($low,$high) = @_; |
399
|
20
|
100
|
|
|
|
58
|
if (scalar @_ > 1) { |
400
|
18
|
100
|
|
|
|
75
|
_validate_num($low) || _validate_positive_integer($low); |
401
|
|
|
|
|
|
|
} else { |
402
|
2
|
|
|
|
|
7
|
($low,$high) = (2, $low); |
403
|
|
|
|
|
|
|
} |
404
|
20
|
100
|
|
|
|
105
|
_validate_num($high) || _validate_positive_integer($high); |
405
|
|
|
|
|
|
|
|
406
|
20
|
50
|
33
|
|
|
97
|
return [] if ($low > $high) || ($high < 2); |
407
|
|
|
|
|
|
|
|
408
|
20
|
100
|
|
|
|
210
|
if ($high > $_XS_MAXVAL) { |
409
|
3
|
|
|
|
|
133
|
my @tp; |
410
|
3
|
50
|
33
|
|
|
19
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::sieve_twin_primes && $low > 2**31) { |
|
|
|
33
|
|
|
|
|
411
|
0
|
|
|
|
|
0
|
@tp = map { _reftyped($_[0],$_) } Math::Prime::Util::GMP::sieve_twin_primes($low, $high); |
|
0
|
|
|
|
|
0
|
|
412
|
|
|
|
|
|
|
} else { |
413
|
3
|
|
|
|
|
26
|
require Math::Prime::Util::PP; |
414
|
3
|
|
|
|
|
17
|
@tp = map { _reftyped($_[0],$_) } Math::Prime::Util::PP::sieve_prime_cluster($low,$high, 2); |
|
566
|
|
|
|
|
794
|
|
415
|
|
|
|
|
|
|
} |
416
|
3
|
|
|
|
|
35
|
return \@tp; |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
17
|
|
|
|
|
3061
|
return segment_twin_primes($low, $high); |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
sub semi_primes { |
423
|
18
|
|
|
18
|
1
|
10240
|
my($low,$high) = @_; |
424
|
18
|
100
|
|
|
|
44
|
if (scalar @_ > 1) { |
425
|
17
|
50
|
|
|
|
71
|
_validate_num($low) || _validate_positive_integer($low); |
426
|
|
|
|
|
|
|
} else { |
427
|
1
|
|
|
|
|
3
|
($low,$high) = (4, $low); |
428
|
|
|
|
|
|
|
} |
429
|
18
|
50
|
|
|
|
68
|
_validate_num($high) || _validate_positive_integer($high); |
430
|
|
|
|
|
|
|
|
431
|
18
|
50
|
33
|
|
|
90
|
return [] if ($low > $high) || ($high < 4); |
432
|
|
|
|
|
|
|
|
433
|
18
|
100
|
100
|
|
|
497
|
return Math::Prime::Util::semi_prime_sieve($low,$high) |
|
|
|
66
|
|
|
|
|
434
|
|
|
|
|
|
|
if $high <= $_XS_MAXVAL |
435
|
|
|
|
|
|
|
&& ($low <= 4 || ($high-$low+1) > ($high/(600*sqrt($high)))); |
436
|
|
|
|
|
|
|
|
437
|
1
|
|
|
|
|
3
|
my $sp = []; |
438
|
1
|
|
|
3
|
|
40
|
Math::Prime::Util::forsemiprimes(sub { push @$sp,$_; }, $low, $high); |
|
3
|
|
|
|
|
28
|
|
439
|
1
|
|
|
|
|
7
|
$sp; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
sub ramanujan_primes { |
443
|
15
|
|
|
15
|
1
|
8091
|
my($low,$high) = @_; |
444
|
15
|
100
|
|
|
|
35
|
if (scalar @_ > 1) { |
445
|
14
|
50
|
|
|
|
52
|
_validate_num($low) || _validate_positive_integer($low); |
446
|
|
|
|
|
|
|
} else { |
447
|
1
|
|
|
|
|
3
|
($low,$high) = (2, $low); |
448
|
|
|
|
|
|
|
} |
449
|
15
|
50
|
|
|
|
47
|
_validate_num($high) || _validate_positive_integer($high); |
450
|
|
|
|
|
|
|
|
451
|
15
|
50
|
33
|
|
|
64
|
return [] if ($low > $high) || ($high < 2); |
452
|
|
|
|
|
|
|
|
453
|
15
|
50
|
|
|
|
25
|
if ($high > $_XS_MAXVAL) { |
454
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PP; |
455
|
0
|
|
|
|
|
0
|
return Math::Prime::Util::PP::_ramanujan_primes($low,$high); |
456
|
|
|
|
|
|
|
} |
457
|
15
|
|
|
|
|
439
|
return _ramanujan_primes($low, $high); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
############################################################################# |
461
|
|
|
|
|
|
|
# Random primes. These are front end functions that do input validation, |
462
|
|
|
|
|
|
|
# load the RandomPrimes module, and call its function. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
sub random_maurer_prime_with_cert { |
465
|
1
|
|
|
1
|
1
|
4035
|
my($bits) = @_; |
466
|
1
|
50
|
|
|
|
30
|
_validate_num($bits, 2) || _validate_positive_integer($bits, 2); |
467
|
|
|
|
|
|
|
|
468
|
1
|
50
|
|
|
|
6
|
if ($Math::Prime::Util::_GMPfunc{"random_maurer_prime_with_cert"}) { |
469
|
0
|
|
|
|
|
0
|
my($n,$cert) = Math::Prime::Util::GMP::random_maurer_prime_with_cert($bits); |
470
|
0
|
|
|
|
|
0
|
return (Math::Prime::Util::_reftyped($_[0],$n), $cert); |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
1
|
|
|
|
|
556
|
require Math::Prime::Util::RandomPrimes; |
474
|
1
|
|
|
|
|
6
|
return Math::Prime::Util::RandomPrimes::random_maurer_prime_with_cert($bits); |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
sub random_shawe_taylor_prime_with_cert { |
478
|
1
|
|
|
1
|
1
|
564
|
my($bits) = @_; |
479
|
1
|
50
|
|
|
|
10
|
_validate_num($bits, 2) || _validate_positive_integer($bits, 2); |
480
|
|
|
|
|
|
|
|
481
|
1
|
50
|
|
|
|
5
|
if ($Math::Prime::Util::_GMPfunc{"random_shawe_taylor_prime_with_cert"}) { |
482
|
0
|
|
|
|
|
0
|
my($n,$cert) =Math::Prime::Util::GMP::random_shawe_taylor_prime_with_cert($bits); |
483
|
0
|
|
|
|
|
0
|
return (Math::Prime::Util::_reftyped($_[0],$n), $cert); |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
1
|
|
|
|
|
12
|
require Math::Prime::Util::RandomPrimes; |
487
|
1
|
|
|
|
|
8
|
return Math::Prime::Util::RandomPrimes::random_shawe_taylor_prime_with_cert($bits); |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
sub random_proven_prime_with_cert { |
491
|
1
|
|
|
1
|
1
|
549
|
my($bits) = @_; |
492
|
1
|
50
|
|
|
|
9
|
_validate_num($bits, 2) || _validate_positive_integer($bits, 2); |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
# Go to Maurer with GMP |
495
|
1
|
50
|
|
|
|
6
|
if ($Math::Prime::Util::_GMPfunc{"random_maurer_prime_with_cert"}) { |
496
|
0
|
|
|
|
|
0
|
my($n,$cert) = Math::Prime::Util::GMP::random_maurer_prime_with_cert($bits); |
497
|
0
|
|
|
|
|
0
|
return (Math::Prime::Util::_reftyped($_[0],$n), $cert); |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
1
|
|
|
|
|
13
|
require Math::Prime::Util::RandomPrimes; |
501
|
1
|
|
|
|
|
9
|
return Math::Prime::Util::RandomPrimes::random_proven_prime_with_cert($bits); |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
############################################################################# |
506
|
|
|
|
|
|
|
# These functions almost always return bigints, so there is no XS |
507
|
|
|
|
|
|
|
# implementation. Try to run the GMP version, and if it isn't available, |
508
|
|
|
|
|
|
|
# load PP and call it. |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
sub primorial { |
511
|
35
|
|
|
35
|
1
|
25525
|
my($n) = @_; |
512
|
35
|
50
|
|
|
|
184
|
_validate_num($n) || _validate_positive_integer($n); |
513
|
35
|
100
|
|
|
|
128
|
return (1,1,2,6,6,30,30,210,210,210)[$n] if $n < 10; |
514
|
|
|
|
|
|
|
|
515
|
30
|
50
|
33
|
|
|
91
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::primorial) { |
516
|
0
|
|
|
|
|
0
|
return _reftyped($_[0], Math::Prime::Util::GMP::primorial($n)); |
517
|
|
|
|
|
|
|
} |
518
|
30
|
|
|
|
|
1374
|
require Math::Prime::Util::PP; |
519
|
30
|
|
|
|
|
99
|
return Math::Prime::Util::PP::primorial($n); |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
sub pn_primorial { |
523
|
36
|
|
|
36
|
1
|
94
|
my($n) = @_; |
524
|
36
|
50
|
|
|
|
155
|
_validate_num($n) || _validate_positive_integer($n); |
525
|
36
|
100
|
|
|
|
343
|
return (1,2,6,30,210,2310,30030,510510,9699690,223092870)[$n] if $n < 10; |
526
|
|
|
|
|
|
|
|
527
|
25
|
50
|
33
|
|
|
66
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::pn_primorial) { |
528
|
0
|
|
|
|
|
0
|
return _reftyped($_[0], Math::Prime::Util::GMP::pn_primorial($n)); |
529
|
|
|
|
|
|
|
} |
530
|
|
|
|
|
|
|
|
531
|
25
|
|
|
|
|
177
|
require Math::Prime::Util::PP; |
532
|
25
|
|
|
|
|
183
|
return Math::Prime::Util::PP::primorial(nth_prime($n)); |
533
|
|
|
|
|
|
|
} |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
sub consecutive_integer_lcm { |
536
|
104
|
|
|
104
|
1
|
59242
|
my($n) = @_; |
537
|
104
|
50
|
|
|
|
420
|
_validate_num($n) || _validate_positive_integer($n); |
538
|
104
|
100
|
|
|
|
258
|
return 0 if $n < 1; |
539
|
|
|
|
|
|
|
|
540
|
103
|
50
|
33
|
|
|
244
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::consecutive_integer_lcm) { |
541
|
0
|
|
|
|
|
0
|
return _reftyped($_[0],Math::Prime::Util::GMP::consecutive_integer_lcm($n)); |
542
|
|
|
|
|
|
|
} |
543
|
103
|
|
|
|
|
1752
|
require Math::Prime::Util::PP; |
544
|
103
|
|
|
|
|
283
|
return Math::Prime::Util::PP::consecutive_integer_lcm($n); |
545
|
|
|
|
|
|
|
} |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
# See 2011+ FLINT and Fredrik Johansson's work for state of the art. |
548
|
|
|
|
|
|
|
# Very crude timing estimates (ignores growth rates). |
549
|
|
|
|
|
|
|
# Perl-comb partitions(10^5) ~ 300 seconds ~200,000x slower than Pari |
550
|
|
|
|
|
|
|
# GMP-comb partitions(10^6) ~ 120 seconds ~1,000x slower than Pari |
551
|
|
|
|
|
|
|
# Pari partitions(10^8) ~ 100 seconds |
552
|
|
|
|
|
|
|
# Bober 0.6 partitions(10^9) ~ 20 seconds ~50x faster than Pari |
553
|
|
|
|
|
|
|
# Johansson partitions(10^12) ~ 10 seconds >1000x faster than Pari |
554
|
|
|
|
|
|
|
sub partitions { |
555
|
58
|
|
|
58
|
1
|
33084
|
my($n) = @_; |
556
|
58
|
100
|
66
|
|
|
331
|
return 1 if defined $n && $n <= 0; |
557
|
57
|
50
|
|
|
|
255
|
_validate_num($n) || _validate_positive_integer($n); |
558
|
|
|
|
|
|
|
|
559
|
57
|
50
|
33
|
|
|
142
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::partitions) { |
560
|
0
|
|
|
|
|
0
|
return _reftyped($_[0],Math::Prime::Util::GMP::partitions($n)); |
561
|
|
|
|
|
|
|
} |
562
|
|
|
|
|
|
|
|
563
|
57
|
|
|
|
|
1651
|
require Math::Prime::Util::PP; |
564
|
57
|
|
|
|
|
158
|
return Math::Prime::Util::PP::partitions($n); |
565
|
|
|
|
|
|
|
} |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
############################################################################# |
568
|
|
|
|
|
|
|
# forprimes, forcomposites, fordivisors. |
569
|
|
|
|
|
|
|
# These are used when the XS code can't handle it. |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
sub _generic_forprimes { |
572
|
1
|
|
|
1
|
|
4
|
my($sub, $beg, $end) = @_; |
573
|
1
|
50
|
|
|
|
5
|
if (!defined $end) { $end = $beg; $beg = 2; } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
574
|
1
|
|
|
|
|
5
|
_validate_positive_integer($beg); |
575
|
1
|
|
|
|
|
4
|
_validate_positive_integer($end); |
576
|
1
|
50
|
|
|
|
15
|
$beg = 2 if $beg < 2; |
577
|
1
|
|
|
|
|
7
|
my $oldforexit = Math::Prime::Util::_start_for_loop(); |
578
|
|
|
|
|
|
|
{ |
579
|
1
|
|
|
|
|
3
|
my $pp; |
|
1
|
|
|
|
|
2
|
|
580
|
1
|
|
|
|
|
3
|
local *_ = \$pp; |
581
|
1
|
|
|
|
|
13
|
for (my $p = next_prime($beg-1); $p <= $end; $p = next_prime($p)) { |
582
|
7
|
|
|
|
|
12
|
$pp = $p; |
583
|
7
|
|
|
|
|
16
|
$sub->(); |
584
|
7
|
50
|
|
|
|
60
|
last if Math::Prime::Util::_get_forexit(); |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
} |
587
|
1
|
|
|
|
|
8
|
Math::Prime::Util::_end_for_loop($oldforexit); |
588
|
|
|
|
|
|
|
} |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
sub _generic_forcomp_sub { |
591
|
2
|
|
|
2
|
|
7
|
my($what, $sub, $beg, $end) = @_; |
592
|
2
|
50
|
|
|
|
7
|
if (!defined $end) { $end = $beg; $beg = 0; } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
593
|
2
|
|
|
|
|
7
|
_validate_positive_integer($beg); |
594
|
2
|
|
|
|
|
7
|
_validate_positive_integer($end); |
595
|
2
|
|
|
|
|
13
|
my $cinc = 1; |
596
|
2
|
|
|
|
|
18
|
my $semiprimes = ($what eq 'semiprimes'); |
597
|
2
|
100
|
|
|
|
7
|
if ($what eq 'oddcomposites') { |
598
|
1
|
50
|
|
|
|
5
|
$beg = 9 if $beg < 9; |
599
|
1
|
50
|
|
|
|
4
|
$beg++ unless $beg & 1; |
600
|
1
|
|
|
|
|
3
|
$cinc = 2; |
601
|
|
|
|
|
|
|
} else { |
602
|
1
|
50
|
|
|
|
4
|
$beg = 4 if $beg < 4; |
603
|
|
|
|
|
|
|
} |
604
|
2
|
50
|
33
|
|
|
12
|
$end = Math::BigInt->new(''.~0) if ref($end) ne 'Math::BigInt' && $end == ~0; |
605
|
2
|
|
|
|
|
7
|
my $oldforexit = Math::Prime::Util::_start_for_loop(); |
606
|
|
|
|
|
|
|
{ |
607
|
2
|
|
|
|
|
5
|
my $pp; |
|
2
|
|
|
|
|
3
|
|
608
|
2
|
|
|
|
|
6
|
local *_ = \$pp; |
609
|
2
|
|
|
|
|
27
|
for (my $p = next_prime($beg-1); $beg <= $end; $p = next_prime($p)) { |
610
|
10
|
|
100
|
|
|
31
|
for ( ; $beg < $p && $beg <= $end ; $beg += $cinc ) { |
611
|
10
|
50
|
33
|
|
|
30
|
next if $semiprimes && !is_semiprime($beg); |
612
|
10
|
|
|
|
|
13
|
$pp = $beg; |
613
|
10
|
|
|
|
|
24
|
$sub->(); |
614
|
10
|
50
|
|
|
|
49
|
last if Math::Prime::Util::_get_forexit(); |
615
|
|
|
|
|
|
|
} |
616
|
10
|
|
|
|
|
20
|
$beg += $cinc; |
617
|
10
|
50
|
|
|
|
62
|
last if Math::Prime::Util::_get_forexit(); |
618
|
|
|
|
|
|
|
} |
619
|
|
|
|
|
|
|
} |
620
|
2
|
|
|
|
|
7
|
Math::Prime::Util::_end_for_loop($oldforexit); |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
sub _generic_forcomposites { |
624
|
1
|
|
|
1
|
|
748
|
_generic_forcomp_sub('composites', @_); |
625
|
|
|
|
|
|
|
} |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
sub _generic_foroddcomposites { |
628
|
1
|
|
|
1
|
|
612
|
_generic_forcomp_sub('oddcomposites', @_); |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
sub _generic_forsemiprimes { |
632
|
0
|
|
|
0
|
|
0
|
_generic_forcomp_sub('semiprimes', @_); |
633
|
|
|
|
|
|
|
} |
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
sub _generic_forfac { |
636
|
0
|
|
|
0
|
|
0
|
my($sf, $sub, $beg, $end) = @_; |
637
|
0
|
|
|
|
|
0
|
_validate_positive_integer($beg); |
638
|
0
|
0
|
|
|
|
0
|
if (defined $end) { |
639
|
0
|
|
|
|
|
0
|
_validate_positive_integer($end); |
640
|
0
|
0
|
|
|
|
0
|
$beg = 1 if $beg < 1; |
641
|
|
|
|
|
|
|
} else { |
642
|
0
|
|
|
|
|
0
|
($beg,$end) = (1,$beg); |
643
|
|
|
|
|
|
|
} |
644
|
0
|
|
|
|
|
0
|
my $oldforexit = Math::Prime::Util::_start_for_loop(); |
645
|
|
|
|
|
|
|
{ |
646
|
0
|
|
|
|
|
0
|
my $pp; |
|
0
|
|
|
|
|
0
|
|
647
|
0
|
|
|
|
|
0
|
local *_ = \$pp; |
648
|
0
|
|
|
|
|
0
|
while ($beg <= $end) { |
649
|
0
|
0
|
0
|
|
|
0
|
if (!$sf || is_square_free($beg)) { |
650
|
0
|
|
|
|
|
0
|
$pp = $beg; |
651
|
0
|
|
|
|
|
0
|
my @f = factor($beg); |
652
|
0
|
|
|
|
|
0
|
$sub->(@f); |
653
|
0
|
0
|
|
|
|
0
|
last if Math::Prime::Util::_get_forexit(); |
654
|
|
|
|
|
|
|
} |
655
|
0
|
|
|
|
|
0
|
$beg++; |
656
|
|
|
|
|
|
|
} |
657
|
|
|
|
|
|
|
} |
658
|
0
|
|
|
|
|
0
|
Math::Prime::Util::_end_for_loop($oldforexit); |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
sub _generic_forfactored { |
661
|
0
|
|
|
0
|
|
0
|
_generic_forfac(0, @_); |
662
|
|
|
|
|
|
|
} |
663
|
|
|
|
|
|
|
sub _generic_forsquarefree { |
664
|
0
|
|
|
0
|
|
0
|
_generic_forfac(1, @_); |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
sub _generic_fordivisors { |
668
|
1
|
|
|
1
|
|
565
|
my($sub, $n) = @_; |
669
|
1
|
|
|
|
|
5
|
_validate_positive_integer($n); |
670
|
1
|
|
|
|
|
26
|
my @divisors = divisors($n); |
671
|
1
|
|
|
|
|
4
|
my $oldforexit = Math::Prime::Util::_start_for_loop(); |
672
|
|
|
|
|
|
|
{ |
673
|
1
|
|
|
|
|
3
|
my $pp; |
|
1
|
|
|
|
|
3
|
|
674
|
1
|
|
|
|
|
4
|
local *_ = \$pp; |
675
|
1
|
|
|
|
|
2
|
foreach my $d (@divisors) { |
676
|
16
|
|
|
|
|
20
|
$pp = $d; |
677
|
16
|
|
|
|
|
32
|
$sub->(); |
678
|
16
|
50
|
|
|
|
68
|
last if Math::Prime::Util::_get_forexit(); |
679
|
|
|
|
|
|
|
} |
680
|
|
|
|
|
|
|
} |
681
|
1
|
|
|
|
|
10
|
Math::Prime::Util::_end_for_loop($oldforexit); |
682
|
|
|
|
|
|
|
} |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
sub formultiperm (&$) { ## no critic qw(ProhibitSubroutinePrototypes) |
685
|
5
|
|
|
5
|
1
|
54544
|
my($sub, $iref) = @_; |
686
|
5
|
50
|
|
|
|
23
|
croak("formultiperm first argument must be an array reference") unless ref($iref) eq 'ARRAY'; |
687
|
|
|
|
|
|
|
|
688
|
5
|
|
|
|
|
13
|
my($sum, %h, @n) = (0); |
689
|
5
|
|
|
|
|
26
|
$h{$_}++ for @$iref; |
690
|
5
|
|
|
|
|
31
|
@n = map { [$_, $h{$_}] } sort(keys(%h)); |
|
11
|
|
|
|
|
40
|
|
691
|
5
|
|
|
|
|
18
|
$sum += $_->[1] for @n; |
692
|
|
|
|
|
|
|
|
693
|
5
|
|
|
|
|
40
|
require Math::Prime::Util::PP; |
694
|
5
|
|
|
|
|
20
|
my $oldforexit = Math::Prime::Util::_start_for_loop(); |
695
|
5
|
|
|
|
|
26
|
Math::Prime::Util::PP::_multiset_permutations( $sub, [], \@n, $sum ); |
696
|
5
|
|
|
|
|
28
|
Math::Prime::Util::_end_for_loop($oldforexit); |
697
|
|
|
|
|
|
|
} |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
############################################################################# |
700
|
|
|
|
|
|
|
# Iterators |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
sub prime_iterator { |
703
|
26
|
|
|
26
|
1
|
30176
|
my($start) = @_; |
704
|
26
|
100
|
|
|
|
76
|
$start = 0 unless defined $start; |
705
|
26
|
100
|
|
|
|
107
|
_validate_num($start) || _validate_positive_integer($start); |
706
|
23
|
100
|
|
|
|
121
|
my $p = ($start > 0) ? $start-1 : 0; |
707
|
|
|
|
|
|
|
# This works fine: |
708
|
|
|
|
|
|
|
# return sub { $p = next_prime($p); return $p; }; |
709
|
|
|
|
|
|
|
# but we can optimize a little |
710
|
23
|
100
|
66
|
|
|
476
|
if (ref($p) ne 'Math::BigInt' && $p <= $_XS_MAXVAL) { |
|
|
50
|
|
|
|
|
|
711
|
|
|
|
|
|
|
# This is simple and low memory, but slower than segments: |
712
|
|
|
|
|
|
|
# return sub { $p = next_prime($p); return $p; }; |
713
|
22
|
|
|
|
|
39
|
my $pr = []; |
714
|
|
|
|
|
|
|
return sub { |
715
|
88
|
100
|
|
88
|
|
518
|
if (scalar(@$pr) == 0) { |
716
|
|
|
|
|
|
|
# Once we're into bigints, just use next_prime |
717
|
22
|
50
|
|
|
|
44
|
return $p=next_prime($p) if $p >= MPU_MAXPRIME; |
718
|
|
|
|
|
|
|
# Get about 10k primes |
719
|
22
|
100
|
|
|
|
55
|
my $segment = ($p <= 1e4) ? 10_000 : int(10000*log($p)+1); |
720
|
22
|
50
|
33
|
|
|
55
|
$segment = ~0-$p if $p+$segment > ~0 && $p+1 < ~0; |
721
|
22
|
|
|
|
|
46
|
$pr = primes($p+1, $p+$segment); |
722
|
|
|
|
|
|
|
} |
723
|
88
|
|
|
|
|
258
|
return $p = shift(@$pr); |
724
|
22
|
|
|
|
|
112
|
}; |
725
|
|
|
|
|
|
|
} elsif ($_HAVE_GMP) { |
726
|
0
|
|
|
0
|
|
0
|
return sub { $p = $p-$p+Math::Prime::Util::GMP::next_prime($p); return $p;}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
727
|
|
|
|
|
|
|
} else { |
728
|
1
|
|
|
|
|
9
|
require Math::Prime::Util::PP; |
729
|
3
|
|
|
3
|
|
24
|
return sub { $p = Math::Prime::Util::PP::next_prime($p); return $p; } |
|
3
|
|
|
|
|
27
|
|
730
|
1
|
|
|
|
|
9
|
} |
731
|
|
|
|
|
|
|
} |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
sub prime_iterator_object { |
734
|
11
|
|
|
11
|
1
|
4508
|
my($start) = @_; |
735
|
11
|
|
|
|
|
1083
|
require Math::Prime::Util::PrimeIterator; |
736
|
11
|
|
|
|
|
40
|
return Math::Prime::Util::PrimeIterator->new($start); |
737
|
|
|
|
|
|
|
} |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
############################################################################# |
740
|
|
|
|
|
|
|
# Front ends to functions. |
741
|
|
|
|
|
|
|
# |
742
|
|
|
|
|
|
|
# These will do input validation, then call the appropriate internal function |
743
|
|
|
|
|
|
|
# based on the input (XS, GMP, PP). |
744
|
|
|
|
|
|
|
############################################################################# |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
sub _generic_prime_count { |
747
|
1
|
|
|
1
|
|
2115
|
my($low,$high) = @_; |
748
|
1
|
50
|
|
|
|
8
|
if (defined $high) { |
749
|
1
|
50
|
|
|
|
9
|
_validate_num($low) || _validate_positive_integer($low); |
750
|
1
|
50
|
|
|
|
6
|
_validate_num($high) || _validate_positive_integer($high); |
751
|
|
|
|
|
|
|
} else { |
752
|
0
|
|
|
|
|
0
|
($low,$high) = (2, $low); |
753
|
0
|
0
|
|
|
|
0
|
_validate_num($high) || _validate_positive_integer($high); |
754
|
|
|
|
|
|
|
} |
755
|
1
|
50
|
33
|
|
|
6
|
return 0 if $high < 2 || $low > $high; |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
# We can relax these constraints if MPU::GMP gets a fast implementation. |
758
|
1
|
0
|
33
|
|
|
174
|
return Math::Prime::Util::GMP::prime_count($low,$high) if $_HAVE_GMP |
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
759
|
|
|
|
|
|
|
&& defined &Math::Prime::Util::GMP::prime_count |
760
|
|
|
|
|
|
|
&& ( (ref($high) eq 'Math::BigInt') |
761
|
|
|
|
|
|
|
|| (($high-$low) < int($low/1_000_000)) |
762
|
|
|
|
|
|
|
); |
763
|
1
|
|
|
|
|
15
|
require Math::Prime::Util::PP; |
764
|
1
|
|
|
|
|
10
|
return Math::Prime::Util::PP::prime_count($low,$high); |
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
sub _generic_factor { |
768
|
64
|
|
|
64
|
|
13780
|
my($n) = @_; |
769
|
64
|
50
|
|
|
|
365
|
_validate_num($n) || _validate_positive_integer($n); |
770
|
|
|
|
|
|
|
|
771
|
64
|
50
|
|
|
|
246
|
if ($_HAVE_GMP) { |
772
|
0
|
|
|
|
|
0
|
my @factors; |
773
|
0
|
0
|
|
|
|
0
|
if ($n != 1) { |
774
|
0
|
|
|
|
|
0
|
@factors = Math::Prime::Util::GMP::factor($n); |
775
|
|
|
|
|
|
|
#if (ref($_[0]) eq 'Math::BigInt') { |
776
|
|
|
|
|
|
|
# @factors = map { ($_ > ~0) ? Math::BigInt->new(''.$_) : $_ } @factors; |
777
|
|
|
|
|
|
|
#} |
778
|
0
|
0
|
|
|
|
0
|
if (ref($_[0])) { |
779
|
0
|
0
|
|
|
|
0
|
@factors = map { ($_ > ~0) ? ref($_[0])->new(''.$_) : $_ } @factors; |
|
0
|
|
|
|
|
0
|
|
780
|
|
|
|
|
|
|
} |
781
|
|
|
|
|
|
|
} |
782
|
0
|
|
|
|
|
0
|
return @factors; |
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
|
785
|
64
|
|
|
|
|
477
|
require Math::Prime::Util::PP; |
786
|
64
|
|
|
|
|
310
|
return Math::Prime::Util::PP::factor($n); |
787
|
|
|
|
|
|
|
} |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
sub _generic_factor_exp { |
790
|
14
|
|
|
14
|
|
572
|
my($n) = @_; |
791
|
14
|
50
|
|
|
|
89
|
_validate_num($n) || _validate_positive_integer($n); |
792
|
|
|
|
|
|
|
|
793
|
14
|
|
|
|
|
35
|
my %exponents; |
794
|
14
|
|
|
|
|
75
|
my @factors = grep { !$exponents{$_}++ } factor($n); |
|
447
|
|
|
|
|
1057
|
|
795
|
14
|
50
|
|
|
|
151
|
return scalar @factors unless wantarray; |
796
|
14
|
|
|
|
|
40
|
return (map { [$_, $exponents{$_}] } @factors); |
|
224
|
|
|
|
|
544
|
|
797
|
|
|
|
|
|
|
} |
798
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
############################################################################# |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
sub _is_gaussian_prime { |
802
|
0
|
|
|
0
|
|
0
|
my($a,$b) = @_; |
803
|
0
|
0
|
|
|
|
0
|
return ((($b % 4) == 3) ? is_prime($b) : 0) if $a == 0; |
|
|
0
|
|
|
|
|
|
804
|
0
|
0
|
|
|
|
0
|
return ((($a % 4) == 3) ? is_prime($a) : 0) if $b == 0; |
|
|
0
|
|
|
|
|
|
805
|
0
|
|
|
|
|
0
|
is_prime( vecsum( vecprod($a,$a), vecprod($b,$b) ) ); |
806
|
|
|
|
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
############################################################################# |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
# Return just the cert portion. |
811
|
|
|
|
|
|
|
sub prime_certificate { |
812
|
4
|
|
|
4
|
1
|
12
|
my($n) = @_; |
813
|
4
|
|
|
|
|
11
|
my ($is_prime, $cert) = is_provable_prime_with_cert($n); |
814
|
4
|
|
|
|
|
14
|
return $cert; |
815
|
|
|
|
|
|
|
} |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
sub is_provable_prime_with_cert { |
819
|
105
|
|
|
105
|
1
|
3559
|
my($n) = @_; |
820
|
105
|
50
|
33
|
|
|
733
|
return 0 if defined $n && $n < 2; |
821
|
105
|
100
|
|
|
|
13085
|
_validate_num($n) || _validate_positive_integer($n); |
822
|
105
|
|
|
|
|
5303
|
my $header = "[MPU - Primality Certificate]\nVersion 1.0\n\nProof for:\nN $n\n\n"; |
823
|
|
|
|
|
|
|
|
824
|
105
|
100
|
|
|
|
742
|
if ($n <= $_XS_MAXVAL) { |
825
|
94
|
|
|
|
|
634
|
my $isp = is_prime($n); |
826
|
94
|
100
|
|
|
|
333
|
return ($isp, '') unless $isp == 2; |
827
|
84
|
|
|
|
|
545
|
return (2, $header . "Type Small\nN $n\n"); |
828
|
|
|
|
|
|
|
} |
829
|
|
|
|
|
|
|
|
830
|
11
|
50
|
33
|
|
|
1456
|
if ($_HAVE_GMP && defined &Math::Prime::Util::GMP::is_provable_prime_with_cert) { |
831
|
0
|
|
|
|
|
0
|
my ($isp, $cert) = Math::Prime::Util::GMP::is_provable_prime_with_cert($n); |
832
|
|
|
|
|
|
|
# New version that returns string format. |
833
|
|
|
|
|
|
|
#return ($isp, $cert) if ref($cert) ne 'ARRAY'; |
834
|
0
|
0
|
|
|
|
0
|
if (ref($cert) ne 'ARRAY') { |
835
|
|
|
|
|
|
|
# Fix silly 0.13 mistake (TODO: deprecate this) |
836
|
0
|
|
|
|
|
0
|
$cert =~ s/^Type Small\n(\d+)/Type Small\nN $1/smg; |
837
|
0
|
|
|
|
|
0
|
return ($isp, $cert); |
838
|
|
|
|
|
|
|
} |
839
|
|
|
|
|
|
|
# Old version. Convert. |
840
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PrimalityProving; |
841
|
0
|
|
|
|
|
0
|
return ($isp, Math::Prime::Util::PrimalityProving::convert_array_cert_to_string($cert)); |
842
|
|
|
|
|
|
|
} |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
{ |
845
|
11
|
|
|
|
|
23
|
my $isp = is_prob_prime($n); |
|
11
|
|
|
|
|
58
|
|
846
|
11
|
50
|
|
|
|
1763
|
return ($isp, '') if $isp == 0; |
847
|
11
|
50
|
|
|
|
45
|
return (2, $header . "Type Small\nN $n\n") if $isp == 2; |
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
# Choice of methods for proof: |
851
|
|
|
|
|
|
|
# ECPP needs a fair bit of programming work |
852
|
|
|
|
|
|
|
# APRCL needs a lot of programming work |
853
|
|
|
|
|
|
|
# BLS75 combo Corollary 11 of BLS75. Trial factor n-1 and n+1 to B, find |
854
|
|
|
|
|
|
|
# factors F1 of n-1 and F2 of n+1. Quit when: |
855
|
|
|
|
|
|
|
# B > (N/(F1*F1*(F2/2)))^1/3 or B > (N/((F1/2)*F2*F2))^1/3 |
856
|
|
|
|
|
|
|
# BLS75 n+1 Requires factoring n+1 to (n/2)^1/3 (theorem 19) |
857
|
|
|
|
|
|
|
# BLS75 n-1 Requires factoring n-1 to (n/2)^1/3 (theorem 5 or 7) |
858
|
|
|
|
|
|
|
# Pocklington Requires factoring n-1 to n^1/2 (BLS75 theorem 4) |
859
|
|
|
|
|
|
|
# Lucas Easy, requires factoring of n-1 (BLS75 theorem 1) |
860
|
|
|
|
|
|
|
# AKS horribly slow |
861
|
|
|
|
|
|
|
# See http://primes.utm.edu/prove/merged.html or other sources. |
862
|
|
|
|
|
|
|
|
863
|
11
|
|
|
|
|
1245
|
require Math::Prime::Util::PrimalityProving; |
864
|
|
|
|
|
|
|
#my ($isp, $pref) = Math::Prime::Util::PrimalityProving::primality_proof_lucas($n); |
865
|
11
|
|
|
|
|
83
|
my ($isp, $pref) = Math::Prime::Util::PrimalityProving::primality_proof_bls75($n); |
866
|
11
|
50
|
|
|
|
79
|
carp "proved $n is not prime\n" if !$isp; |
867
|
11
|
|
|
|
|
85
|
return ($isp, $pref); |
868
|
|
|
|
|
|
|
} |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
sub verify_prime { |
872
|
50
|
|
|
50
|
1
|
18250
|
require Math::Prime::Util::PrimalityProving; |
873
|
50
|
|
|
|
|
247
|
return Math::Prime::Util::PrimalityProving::verify_cert(@_); |
874
|
|
|
|
|
|
|
} |
875
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
############################################################################# |
877
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
sub RiemannZeta { |
879
|
6
|
|
|
6
|
1
|
2765
|
my($n) = @_; |
880
|
6
|
50
|
|
|
|
26
|
croak("Invalid input to RiemannZeta: x must be > 0") if $n <= 0; |
881
|
|
|
|
|
|
|
|
882
|
6
|
50
|
|
|
|
15
|
return $n-$n if $n > 10_000_000; # Over 3M leading zeros |
883
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
return _XS_RiemannZeta($n) |
885
|
6
|
50
|
33
|
|
|
94
|
if !defined $bignum::VERSION && ref($n) ne 'Math::BigFloat' && $_Config{'xs'}; |
|
|
|
33
|
|
|
|
|
886
|
|
|
|
|
|
|
|
887
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PP; |
888
|
0
|
|
|
|
|
0
|
return Math::Prime::Util::PP::RiemannZeta($n); |
889
|
|
|
|
|
|
|
} |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
sub RiemannR { |
892
|
11
|
|
|
11
|
1
|
5338
|
my($n) = @_; |
893
|
11
|
100
|
|
|
|
224
|
croak("Invalid input to RiemannR: x must be > 0") if $n <= 0; |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
return _XS_RiemannR($n) |
896
|
9
|
50
|
33
|
|
|
257
|
if !defined $bignum::VERSION && ref($n) ne 'Math::BigFloat' && $_Config{'xs'}; |
|
|
|
33
|
|
|
|
|
897
|
|
|
|
|
|
|
|
898
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PP; |
899
|
0
|
|
|
|
|
0
|
return Math::Prime::Util::PP::RiemannR($n); |
900
|
|
|
|
|
|
|
} |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
sub ExponentialIntegral { |
903
|
23
|
|
|
23
|
1
|
8062
|
my($n) = @_; |
904
|
23
|
100
|
|
|
|
98
|
return $_Neg_Infinity if $n == 0; |
905
|
22
|
100
|
|
|
|
63
|
return 0 if $n == $_Neg_Infinity; |
906
|
21
|
100
|
|
|
|
52
|
return $_Infinity if $n == $_Infinity; |
907
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
return _XS_ExponentialIntegral($n) |
909
|
20
|
100
|
33
|
|
|
390
|
if !defined $bignum::VERSION && ref($n) ne 'Math::BigFloat' && $_Config{'xs'}; |
|
|
|
66
|
|
|
|
|
910
|
|
|
|
|
|
|
|
911
|
2
|
|
|
|
|
23
|
require Math::Prime::Util::PP; |
912
|
2
|
|
|
|
|
10
|
return Math::Prime::Util::PP::ExponentialIntegral($n); |
913
|
|
|
|
|
|
|
} |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
sub LogarithmicIntegral { |
916
|
28
|
|
|
28
|
1
|
6065
|
my($n) = @_; |
917
|
28
|
100
|
|
|
|
92
|
return 0 if $n == 0; |
918
|
26
|
100
|
|
|
|
56
|
return $_Neg_Infinity if $n == 1; |
919
|
25
|
100
|
|
|
|
53
|
return $_Infinity if $n == $_Infinity; |
920
|
|
|
|
|
|
|
|
921
|
24
|
100
|
|
|
|
251
|
croak("Invalid input to LogarithmicIntegral: x must be >= 0") if $n <= 0; |
922
|
|
|
|
|
|
|
|
923
|
23
|
50
|
33
|
|
|
116
|
if (!defined $bignum::VERSION && ref($n) ne 'Math::BigFloat' && $_Config{'xs'}) { |
|
|
|
33
|
|
|
|
|
924
|
23
|
100
|
|
|
|
55
|
return 1.045163780117492784844588889194613136522615578151 if $n == 2; |
925
|
22
|
|
|
|
|
196
|
return _XS_LogarithmicIntegral($n); |
926
|
|
|
|
|
|
|
} |
927
|
|
|
|
|
|
|
|
928
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PP; |
929
|
0
|
|
|
|
|
0
|
return Math::Prime::Util::PP::LogarithmicIntegral(@_); |
930
|
|
|
|
|
|
|
} |
931
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
sub LambertW { |
933
|
9
|
|
|
9
|
1
|
4085
|
my($x) = @_; |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
return _XS_LambertW($x) |
936
|
9
|
50
|
33
|
|
|
154
|
if !defined $bignum::VERSION && ref($x) ne 'Math::BigFloat' && $_Config{'xs'}; |
|
|
|
33
|
|
|
|
|
937
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
# TODO: Call GMP function here directly |
939
|
|
|
|
|
|
|
|
940
|
0
|
|
|
|
|
0
|
require Math::Prime::Util::PP; |
941
|
0
|
|
|
|
|
0
|
return Math::Prime::Util::PP::LambertW($x); |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
sub bernfrac { |
945
|
116
|
|
|
116
|
1
|
3795
|
my($n) = @_; |
946
|
116
|
50
|
33
|
|
|
644
|
return map { _to_bigint($_) } (0,1) if defined $n && $n < 0; |
|
0
|
|
|
|
|
0
|
|
947
|
116
|
50
|
|
|
|
569
|
_validate_num($n) || _validate_positive_integer($n); |
948
|
116
|
100
|
100
|
|
|
515
|
return map { _to_bigint($_) } (0,1) if $n > 1 && ($n & 1); |
|
22
|
|
|
|
|
1240
|
|
949
|
|
|
|
|
|
|
|
950
|
105
|
50
|
|
|
|
301
|
if ($Math::Prime::Util::_GMPfunc{"bernfrac"}) { |
951
|
0
|
|
|
|
|
0
|
return map { _to_bigint($_) } Math::Prime::Util::GMP::bernfrac($n); |
|
0
|
|
|
|
|
0
|
|
952
|
|
|
|
|
|
|
} |
953
|
|
|
|
|
|
|
|
954
|
105
|
|
|
|
|
2064
|
require Math::Prime::Util::PP; |
955
|
105
|
|
|
|
|
322
|
return Math::Prime::Util::PP::bernfrac($n); |
956
|
|
|
|
|
|
|
} |
957
|
|
|
|
|
|
|
sub bernreal { |
958
|
26
|
|
|
26
|
1
|
62202
|
my($n, $precision) = @_; |
959
|
26
|
100
|
|
|
|
74
|
do { require Math::BigFloat; Math::BigFloat->import(); } unless defined $Math::BigFloat::VERSION; |
|
1
|
|
|
|
|
1513
|
|
|
1
|
|
|
|
|
54704
|
|
960
|
|
|
|
|
|
|
|
961
|
26
|
50
|
|
|
|
26415
|
if ($Math::Prime::Util::_GMPfunc{"bernreal"}) { |
962
|
0
|
0
|
|
|
|
0
|
return Math::BigFloat->new(Math::Prime::Util::GMP::bernreal($n)) if !defined $precision; |
963
|
0
|
|
|
|
|
0
|
return Math::BigFloat->new(Math::Prime::Util::GMP::bernreal($n,$precision),$precision); |
964
|
|
|
|
|
|
|
} |
965
|
|
|
|
|
|
|
|
966
|
26
|
|
|
|
|
56
|
my($num,$den) = bernfrac($n); |
967
|
26
|
100
|
|
|
|
490
|
return Math::BigFloat->bzero if $num->is_zero; |
968
|
15
|
|
|
|
|
190
|
scalar Math::BigFloat->new($num)->bdiv($den, $precision); |
969
|
|
|
|
|
|
|
} |
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
sub harmfrac { |
972
|
79
|
|
|
79
|
1
|
25122
|
my($n) = @_; |
973
|
79
|
50
|
|
|
|
284
|
_validate_num($n) || _validate_positive_integer($n); |
974
|
79
|
50
|
|
|
|
180
|
return map { _to_bigint($_) } (0,1) if $n <= 0; |
|
0
|
|
|
|
|
0
|
|
975
|
|
|
|
|
|
|
|
976
|
79
|
50
|
|
|
|
182
|
if ($Math::Prime::Util::_GMPfunc{"harmfrac"}) { |
977
|
0
|
|
|
|
|
0
|
return map { _to_bigint($_) } Math::Prime::Util::GMP::harmfrac($n); |
|
0
|
|
|
|
|
0
|
|
978
|
|
|
|
|
|
|
} |
979
|
|
|
|
|
|
|
|
980
|
79
|
|
|
|
|
417
|
require Math::Prime::Util::PP; |
981
|
79
|
|
|
|
|
200
|
Math::Prime::Util::PP::harmfrac($n); |
982
|
|
|
|
|
|
|
} |
983
|
|
|
|
|
|
|
sub harmreal { |
984
|
22
|
|
|
22
|
1
|
65339
|
my($n, $precision) = @_; |
985
|
22
|
50
|
|
|
|
111
|
_validate_num($n) || _validate_positive_integer($n); |
986
|
22
|
50
|
|
|
|
50
|
do { require Math::BigFloat; Math::BigFloat->import(); } unless defined $Math::BigFloat::VERSION; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
987
|
22
|
100
|
|
|
|
56
|
return Math::BigFloat->bzero if $n <= 0; |
988
|
|
|
|
|
|
|
|
989
|
21
|
50
|
|
|
|
57
|
if ($Math::Prime::Util::_GMPfunc{"harmreal"}) { |
990
|
0
|
0
|
|
|
|
0
|
return Math::BigFloat->new(Math::Prime::Util::GMP::harmreal($n)) if !defined $precision; |
991
|
0
|
|
|
|
|
0
|
return Math::BigFloat->new(Math::Prime::Util::GMP::harmreal($n,$precision),$precision); |
992
|
|
|
|
|
|
|
} |
993
|
|
|
|
|
|
|
|
994
|
|
|
|
|
|
|
# If low enough precision, use native floating point. Fast. |
995
|
21
|
50
|
33
|
|
|
47
|
if (defined $precision && $precision <= 13) { |
996
|
|
|
|
|
|
|
return Math::BigFloat->new( |
997
|
0
|
0
|
|
|
|
0
|
($n < 80) ? do { my $h = 0; $h += 1/$_ for 1..$n; $h; } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
998
|
|
|
|
|
|
|
: log($n) + 0.57721566490153286060651209 + 1/(2*$n) - 1/(12*$n*$n) + 1/(120*$n*$n*$n*$n) |
999
|
|
|
|
|
|
|
,$precision |
1000
|
|
|
|
|
|
|
); |
1001
|
|
|
|
|
|
|
} |
1002
|
|
|
|
|
|
|
|
1003
|
21
|
50
|
|
|
|
45
|
if ($Math::Prime::Util::_GMPfunc{"harmfrac"}) { |
1004
|
0
|
|
|
|
|
0
|
my($num,$den) = map { _to_bigint($_) } Math::Prime::Util::GMP::harmfrac($n); |
|
0
|
|
|
|
|
0
|
|
1005
|
0
|
|
|
|
|
0
|
return scalar Math::BigFloat->new($num)->bdiv($den, $precision); |
1006
|
|
|
|
|
|
|
} |
1007
|
|
|
|
|
|
|
|
1008
|
21
|
|
|
|
|
128
|
require Math::Prime::Util::PP; |
1009
|
21
|
|
|
|
|
74
|
Math::Prime::Util::PP::harmreal($n, $precision); |
1010
|
|
|
|
|
|
|
} |
1011
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
############################################################################# |
1013
|
|
|
|
|
|
|
|
1014
|
72
|
|
|
72
|
|
32362
|
use Math::Prime::Util::MemFree; |
|
72
|
|
|
|
|
184
|
|
|
72
|
|
|
|
|
5432
|
|
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
1; |
1017
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
__END__ |