-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlower-bounds.sf
177 lines (128 loc) · 4.7 KB
/
lower-bounds.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/ruby
# a(n) is the smallest odd b > 1 such that (b^(2n) + 1)/2 has all prime divisors p == 1 (mod 2n).
# https://oeis.org/A298398
# Find lower-bounds to a(n).
include("../../../factordb/auto.sf")
func a(n, from = 2, upto = 1e9) {
for k in (from .. upto) {
k.is_odd || next
var m = (2*n)
var v = (k**m + 1)/2
is_congruent(v, 1, m) || next
# Fast check for small factors
var f1 = v.trial_factor
f1.all { is_congruent(_, 1, m) } || next
f1.all { is_congruent(v/_, 1, m) } || next
if (f1.all { .is_prime }) {
return (k, true)
}
# Fast Miller factorization check
if (v.len < 2000) {
var f = v.miller_factor
f.all { is_congruent(_, 1, m) } || next
f.all { is_congruent(v/_, 1, m) } || next
var c = f.grep { .is_composite }
return (k, true) if (c.len == 0)
if (c.all { .len <= 50 }) {
if (c.map { .factor }.flat.all { .is_congruent(1, m) }) {
return (k, true)
}
else {
next
}
}
var t = c.grep { .len <= 50 }.map { .factor }.flat
t.all { is_congruent(_, 1, m) } || next
t.all { is_congruent(v/_, 1, m) } || next
}
# Check prime factors up to 10^8
var f2 = v.trial_factor(1e8)
f2.all { is_congruent(_, 1, m) } || next
f2.all { is_congruent(v/_, 1, m) } || next
if (f2.all { .is_prime }) {
return (k, true)
}
# Try the difference of powers factorization method (finding algebraic factors)
#var f4 = (v*2).dop_factor.map {|d| gcd(v, d) }.grep { _ > 1 }
var f4 = Math.gcd_factors(v, uniq(dop_factor(2*v) + cop_factor(2*v, m+1) + cyclotomic_factor(v, k)))
f4.all { is_congruent(_, 1, m) } || next
f4.all { is_congruent(v/_, 1, m) } || next
do { # check the composite factors
var c = f4.grep { .is_composite }
return (k, true) if (c.len == 0)
if (c.all { .len <= 50 }) { # unlikely
if (c.map { .factor }.flat.all { .is_congruent(1, m) }) {
return (k, true)
}
else {
next
}
}
var t = c.grep { .len <= 50 }.map { .factor }.flat
t.all { is_congruent(_, 1, m) } || next
t.all { is_congruent(v/_, 1, m) } || next
}
# Try to find more special factors
var f3 = Math.gcd_factors(v, special_factor(2*v))
f3.all { is_congruent(_, 1, m) } || next
f3.all { is_congruent(v/_, 1, m) } || next
do { # check the composite factors
var c = f3.grep { .is_composite }
return (k, true) if (c.len == 0)
if (c.all { .len <= 50 }) { # unlikely
if (c.map { .factor }.flat.all { .is_congruent(1, m) }) {
return (k, true)
}
else {
next
}
}
var t = c.grep { .len <= 50 }.map { .factor }.flat
t.all { is_congruent(_, 1, m) } || next
t.all { is_congruent(v/_, 1, m) } || next
}
# Check FactorDB (#1)
#var fdb1 = factordb("#{k}^#{m} + 1").map {|d| gcd(v, d) }.grep { _ > 1 }
var fdb1 = Math.gcd_factors(v, factordb("#{k}^#{m} + 1"))
fdb1.all { is_congruent(_, 1, m) } || next
fdb1.all { is_congruent(v/_, 1, m) } || next
if ((fdb1.prod == v) && (fdb1.all { .is_prime })) {
return (k, true)
}
# Check FactorDB (#2)
var fdb = factordb("(#{k}^#{m} + 1)/2")
fdb.all { is_congruent(_, 1, m) } || next
fdb.all { is_congruent(v/_, 1, m) } || next
if (fdb.all { .is_prime }) {
return (k, true)
}
else {
return (k, false)
}
}
}
var lower_bounds_file = File("lower-bounds_factordb.txt")
lower_bounds_file.open_r.lines.each {|line|
if (line =~ /^a\((\d+)\) >= (\d+)/) { |m|
var n = Num(m[0])
var k = Num(m[1])
say ":: Trying to find a lower-bound for a(#{n}) >= #{k} <--> (#{k}^#{2*n} + 1)/2"
var (new_k, is_term) = a(n, k)
if (is_term) {
say "\n\t-> Found term: a(#{n}) = #{new_k}\n"
}
elsif (k != new_k) {
say "\n\t-> Found a better lower-bound: a(#{n}) >= #{new_k} <--> (#{new_k}^#{2*n} + 1)/2\n"
}
}
}
__END__
for n in (1..100) {
var (k, is_term) = a(n)
if (is_term) {
say "a(#{n}) = #{k}"
}
else {
say "a(#{n}) >= #{k}"
}
}