-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudoprime_search.pl
115 lines (84 loc) · 1.88 KB
/
pseudoprime_search.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl
use 5.020;
use warnings;
use strict;
use ntheory qw(:all);
use experimental qw(signatures);
use List::Util qw(uniqnum);
use File::Find qw(find);
use Math::GMPz;
# a(5) <= 5113747913401
# a(6) <= 30990302851201
#~ a(5) <= 32203213602841
#~ a(6) <= 323346556958041
#~ a(7) <= 2528509579568281
#~ a(8) <= 5189206896360728641
#~ a(9) <= 12155831039329417441
my $N = 9;
my $P = nth_prime($N);
my $MAX = 323346556958041;
# a(5) <= 5113747913401
# a(6) <= 30990302851201
# a(7) <=
my @primes_bellow = @{primes($P-1)};
my @primes = @{primes(100)};
sub non_residue {
my ($n) = @_;
foreach my $p (@primes) {
if ($p > $P) {
return -1;
}
sqrtmod($p, $n) // return $p;
}
return -1;
}
sub isok_a {
my ($k) = @_;
if (powmod($P, ($k-1)>>1, $k) == $k-1) {
return vecall { powmod($_, ($k-1)>>1, $k) == 1 } 2..($P-1);
}
return;
}
sub isok_b {
my ($k) = @_;
if (powmod($P, ($k-1)>>1, $k) == 1) {
return vecall { powmod($_, ($k-1)>>1, $k) == $k-1 } @primes_bellow;
}
return;
}
my %seen;
sub process_file {
my ($file) = @_;
open my $fh, '<', $file;
while (<$fh>) {
next if /^\h*#/;
/\S/ or next;
my $n = (split(' ', $_))[-1];
$n || next;
if ($n > $MAX) {
next;
}
if ($n > ~0) {
$n = Math::GMPz->new("$n");
}
next if is_prime($n);
next if $seen{$n}++;
#if (isok_b($n)) {
if (isok_a($n)) {
say $n;
if ($n < $MAX) {
$MAX = $n;
say "New record: $n";
}
}
}
}
my $psp = "/home/swampyx/Other/Programare/experimental-projects/pseudoprimes/oeis-pseudoprimes";
find({
wanted => sub {
if ( /\.txt\z/) {
process_file($_);
}
},
no_chdir => 1,
} => $psp);