-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fermat_fib.pl
129 lines (97 loc) · 3.09 KB
/
test_fermat_fib.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
use 5.020;
use strict;
use warnings;
use experimental qw(signatures);
use Math::GMPz;
use ntheory qw(is_prime kronecker);
sub is_fermat_fibonacci_prime($n) {
if (ref($n) eq 'Math::GMPz') {
$n = Math::GMPz::Rmpz_init_set($n);
}
else {
$n = Math::GMPz->new("$n");
}
if (Math::GMPz::Rmpz_even_p($n)) {
return ((Math::GMPz::Rmpz_cmp_ui($n, 2) == 0) ? 1 : 0);
}
if (Math::GMPz::Rmpz_cmp_ui($n, 5) == 0) {
return 1;
}
my $t = Math::GMPz::Rmpz_init_set_ui(2);
my $u = Math::GMPz::Rmpz_init();
Math::GMPz::Rmpz_sub_ui($u, $n, 1);
Math::GMPz::Rmpz_powm($u, $t, $u, $n);
Math::GMPz::Rmpz_cmp_ui($u, 1) == 0 or return 0;
my $v = Math::GMPz::Rmpz_init();
my $w = Math::GMPz::Rmpz_init();
Math::GMPz::Rmpz_sub_ui($w, $n, 1);
my $m = Math::GMPz::Rmpz_init_set($n);
my $f = Math::GMPz::Rmpz_init_set_ui(0);
my $g = Math::GMPz::Rmpz_init_set_ui(1);
my $a = Math::GMPz::Rmpz_init_set_ui(0);
my $b = Math::GMPz::Rmpz_init_set_ui(1);
my $c = Math::GMPz::Rmpz_init_set_ui(1);
my $d = Math::GMPz::Rmpz_init_set_ui(1);
for (; ;) {
if (Math::GMPz::Rmpz_odd_p($n)) {
Math::GMPz::Rmpz_set($t, $f);
Math::GMPz::Rmpz_mul($f, $f, $a);
Math::GMPz::Rmpz_addmul($f, $g, $c);
Math::GMPz::Rmpz_mod($f, $f, $m);
Math::GMPz::Rmpz_mul($g, $g, $d);
Math::GMPz::Rmpz_addmul($g, $t, $b);
Math::GMPz::Rmpz_mod($g, $g, $m);
}
Math::GMPz::Rmpz_div_2exp($n, $n, 1);
Math::GMPz::Rmpz_sgn($n) || last;
Math::GMPz::Rmpz_set($t, $a);
Math::GMPz::Rmpz_mul($a, $a, $a);
Math::GMPz::Rmpz_addmul($a, $b, $c);
Math::GMPz::Rmpz_mod($a, $a, $m);
Math::GMPz::Rmpz_set($u, $b);
Math::GMPz::Rmpz_mul($b, $b, $d);
Math::GMPz::Rmpz_addmul($b, $u, $t);
Math::GMPz::Rmpz_mod($b, $b, $m);
Math::GMPz::Rmpz_set($v, $c);
Math::GMPz::Rmpz_mul($c, $c, $t);
Math::GMPz::Rmpz_addmul($c, $v, $d);
Math::GMPz::Rmpz_mod($c, $c, $m);
Math::GMPz::Rmpz_mul($d, $d, $d);
Math::GMPz::Rmpz_addmul($d, $v, $u);
Math::GMPz::Rmpz_mod($d, $d, $m);
}
if ( Math::GMPz::Rmpz_cmp_ui($f, 1) == 0
or Math::GMPz::Rmpz_cmp($f, $w) == 0) {
if (not is_prime($w + 1)) {
say "error for ", $w + 1, " -> with $f";
}
return 1;
}
return 0;
}
my %seen;
my @nums;
while (<>) {
next if /^\h*#/;
/\S/ or next;
my $n = (split(' ', $_))[-1];
$n || next;
next if $seen{$n}++;
#say $. if $. % 10000 == 0;
#if ($n >= (~0 >> 1)) {
$n = Math::GMPz->new("$n");
#}
push @nums, $n;
#ntheory::is_provable_prime($n) && die "error: $n\n";
#ntheory::is_prime($n) && die "error: $n\n";
#ntheory::is_aks_prime($n) && die "error: $n\n";
#ntheory::miller_rabin_random($n, 7) && die "error: $n\n";
}
@nums = sort { $a <=> $b } @nums;
foreach my $n (@nums) {
is_fermat_fibonacci_prime($n) && do {
warn "error: $n\n";
last;
};
}