-
Notifications
You must be signed in to change notification settings - Fork 0
/
fermat_pseudoprimes_in_range.sf
136 lines (95 loc) · 4.13 KB
/
fermat_pseudoprimes_in_range.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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 30 August 2022
# https://github.com/trizen
# Generate all the k-omega Fermat pseudoprimes in range [a,b]. (not in sorted order)
# Definition:
# k-omega primes are numbers n such that omega(n) = k.
# See also:
# https://en.wikipedia.org/wiki/Almost_prime
# https://en.wikipedia.org/wiki/Prime_omega_function
# https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
# PARI/GP program (slow):
# fermat_psp(A, B, k, base) = A=max(A, vecprod(primes(k))); (f(m, l, p, j) = my(list=List()); forprime(q=p, sqrtnint(B\m, j), if(base%q != 0, my(v=m*q, t=q, r=nextprime(q+1)); while(v <= B, my(L=lcm(l, znorder(Mod(base, t)))); if(gcd(L, v) == 1, if(j==1, if(v>=A && if(k==1, !isprime(v), 1) && (v-1)%L == 0, listput(list, v)), if(v*r <= B, list=concat(list, f(v, L, r, j-1)))), break); v *= q; t *= q))); list); vecsort(Vec(f(1, 1, 2, k)));
# PARI/GP program (fast):
# fermat_psp(A, B, k, base) = A=max(A, vecprod(primes(k))); (f(m, l, lo, k) = my(list=List()); my(hi=sqrtnint(B\m, k)); if(lo > hi, return(list)); if(k==1, forstep(p=lift(1/Mod(m, l)), hi, l, if(isprimepower(p) && gcd(m*base, p) == 1, my(n=m*p); if(n >= A && (n-1) % znorder(Mod(base, p)) == 0, listput(list, n)))), forprime(p=lo, hi, base%p == 0 && next; my(z=znorder(Mod(base, p))); gcd(m,z) == 1 || next; my(q=p, v=m*p); while(v <= B, list=concat(list, f(v, lcm(l, z), p+1, k-1)); q *= p; Mod(base, q)^z == 1 || break; v *= p))); list); vecsort(Set(f(1, 1, 2, k)));
func fermat_pseudoprimes_in_range(A, B, k, base, callback) {
A = max(k.pn_primorial, A)
var seen = Hash()
func (m, L, lo, j) {
var hi = idiv(B,m).iroot(j)
lo > hi && return nil
if (j == 1) {
if (L == 1) { # optimization
each_prime(lo, hi, {|p|
p.divides(base) && next
for (var v = (m == 1 ? p*p : m*p); v <= B; v *= p) {
v >= A || next
v.is_psp(base) || break
callback(v) if !(seen{v} := 0 ++)
}
})
return nil
}
var t = m.invmod(L)
t > hi && return nil
t += L*idiv_ceil(lo - t, L) if (t < lo)
t > hi && return nil
for p in (range(t, hi, L)) {
p.is_prime_power || next
p.is_coprime(m) || next
p.is_coprime(base) || next
var v = m*p
v >= A || next
if (znorder(base, p) `divides` v.dec) {
callback(v) if !(seen{v} := 0 ++)
}
}
return nil
}
each_prime(lo, hi, {|p|
p.divides(base) && next
var z = znorder(base, p)
m.is_coprime(z) || next
var v = m*p
var q = p
while (v <= B) {
__FUNC__(v, lcm(L, z), p+1, j-1)
q *= p
powmod(base, z, q) == 1 || break
v *= p
}
})
}(1, 1, 2, k)
return callback
}
# Generate all the Fermat pseudoprimes to base 3 in range [1, 10^4]
var from = 1
var upto = 1e4
var base = 3
var arr = gather {
for k in (1..100) {
break if (k.pn_primorial > upto)
fermat_pseudoprimes_in_range(from, upto, k, base, { take(_) })
}
}
say arr.sort
# Run some tests
if (false) { # true to run some tests
for k in (1..5) {
say "Testing: k = #{k}"
var lo = k.pn_primorial
var hi = lo*1000
for base in (2..100) {
var this = k.fermat_psp(base, lo, hi)
#var this = k.omega_primes(lo,hi).grep{.is_psp(base) && .is_composite }
var that = gather {
fermat_pseudoprimes_in_range(lo, hi, k, base, func (n) { take(n) })
}.sort
this == that ||
die "Error for k = #{k} and base = #{base} with hi = #{hi}\n#{this} != #{that}";
}
}
}
__END__
[91, 121, 286, 671, 703, 949, 1105, 1541, 1729, 1891, 2465, 2665, 2701, 2821, 3281, 3367, 3751, 4961, 5551, 6601, 7381, 8401, 8911]