-
Notifications
You must be signed in to change notification settings - Fork 0
/
620_lucas-fibonacci_pseudoprimes.sf
154 lines (116 loc) · 3.12 KB
/
620_lucas-fibonacci_pseudoprimes.sf
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/ruby
# Author: Daniel "Trizen" Șuteu
# Date: 21 September 2018
# https://github.com/trizen
# A new algorithm for generating Fibonacci pseudoprimes.
# See also:
# https://oeis.org/A081264 -- Odd Fibonacci pseudoprimes.
# https://oeis.org/A212424 -- Frobenius pseudoprimes with respect to Fibonacci polynomial x^2 - x - 1.
func fibonacci_pseudoprimes(limit, callback) {
var table = Hash()
#var r = 1e20.irand
for p in (primes(1e5)) {
#for p in (primes(1e5)) {
for d in (p - p.legendre(5) -> divisors) {
#if (fibmod(d, p) == 0) {
table{d} := [] << p
#}
}
}
#gather {
var seen = Hash()
table.each_v { |a|
var L = a.len
L > 1 || next
L < 8 || next
for k in (2..L) {
a.subsets(k, {|*t|
var num = t.prod
if (!seen{num}) {
callback(num)
seen{num} = true
}
})
}
}
# }.uniq
}
func is_fibonacci_pseudoprime(n) {
fibmod(n - n.legendre(5), n) == 0
}
func BPSW_primality_test(n) {
return false if (n <= 1)
return true if (n == 2)
return false if n.is_power
# 2.powmod(n-1, n) == 1 || return false
var Q
for k in (2 .. Inf) {
var D = ((-1)**k * (2*k + 1))
if (D.kronecker(n) == -1) {
Q = (1 - D)/4
break
}
}
var d = (n + 1)
var s = d.valuation(2)
var(U1 = 1)
var(V1 = 2, V2 = 1)
var(Q1 = 1, Q2 = 1)
for bit in (d.as_bin.first(-s - 1).chars) {
Q1 = (Q1 * Q2)%n
if (bit) {
Q2 = (Q1 * Q)%n
U1 = (U1 * V2)%n
V1 = (V2*V1 - Q1)%n
V2 = (V2*V2 - 2*Q2)%n
}
else {
Q2 = Q1
U1 = (U1*V1 - Q1)%n
V2 = (V2*V1 - Q1)%n
V1 = (V1*V1 - 2*Q2)%n
}
}
Q1 = (Q1 * Q2)%n
Q2 = (Q1 * Q)%n
U1 = (U1*V1 - Q1)%n
V1 = (V2*V1 - Q1)%n
Q1 = (Q1 * Q2)%n
return true if U1.is_congruent(0, n)
return true if V1.is_congruent(0, n)
s.times {
U1 = (U1 * V1)%n
V1 = (V1*V1 - 2*Q1)%n
Q1 = (Q1 * Q1)%n
return true if V1.is_congruent(0, n)
}
return false
}
func PSW_primality_test(n) {
return false if (n <= 1)
return true if (n == 2)
return false if n.is_power
# 2.powmod(n-1, n) == 1 || return false
var P = (1..Inf -> first_by {|k|
n.kronecker(k*k + 4) == -1
})
lucasUmod(P, -1, n+1, n) == 0
}
fibonacci_pseudoprimes(100, {|n|
if (powmod(2, n-1, n) == 1) {
say "Fermat pseudoprime: #{n}"
if (BPSW_primality_test(n)) {
die "BPSW counter-example: #{n}"
}
if (PSW_primality_test(n)) {
die "PSW counter-example: #{n}"
}
if (n.legendre(5) == -1) {
say "Almost found a special pseudoprime: #{n}"
Sys.sleep(1)
if (is_fibonacci_pseudoprime(n)) {
die "Found a special pseudoprime: #{n}"
}
}
}
})