Erdős–Woods numbers: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Tidied but no quicker.) |
(→{{header|Julia}}: threading) |
||
Line 33: | Line 33: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang julia>""" modified from https://codegolf.stackexchange.com/questions/230509/find-the-erd%C5%91s-woods-origin/ """ |
|||
⚫ | |||
using BitIntegers |
|||
""" |
|||
⚫ | |||
""" |
|||
function erdős_woods(n) |
function erdős_woods(n) |
||
primes = Int[] |
primes = Int[] |
||
P = |
P = BigInt(1) |
||
k = 1 |
k = 1 |
||
while k < n |
while k < n |
||
Line 43: | Line 49: | ||
k += 1 |
k += 1 |
||
end |
end |
||
divs = [evalpoly( |
divs = [evalpoly(2, [Int(a % p == 0) for p in primes]) for a in 0:n-1] |
||
np = length(primes) |
np = length(primes) |
||
partitions = [( |
partitions = [(Int256(0), Int256(0), Int256(2)^np - 1)] |
||
ort(x) = trailing_zeros(divs[x + 1] | divs[n - x + 1]) |
ort(x) = trailing_zeros(divs[x + 1] | divs[n - x + 1]) |
||
for i in sort(collect(1:n-1), lt = (b, c) -> ort(b) > ort(c)) |
for i in sort(collect(1:n-1), lt = (b, c) -> ort(b) > ort(c)) |
||
new_partitions = Tuple{ |
new_partitions = Tuple{Int256, Int256, Int256}[] |
||
factors = divs[i + 1] |
factors = divs[i + 1] |
||
other_factors = divs[n - i + 1] |
other_factors = divs[n - i + 1] |
||
Line 57: | Line 63: | ||
continue |
continue |
||
end |
end |
||
for (ix, v) in enumerate( |
for (ix, v) in enumerate(reverse(string(factors & r_primes, base=2))) |
||
if v == 1 |
if v == '1' |
||
w = 1 << (ix - 1) |
w = Int256(1) << (ix - 1) |
||
push!(new_partitions, (set_a ⊻ w, set_b, r_primes ⊻ w)) |
push!(new_partitions, (set_a ⊻ w, set_b, r_primes ⊻ w)) |
||
end |
end |
||
end |
end |
||
for (ix, v) in enumerate( |
for (ix, v) in enumerate(reverse(string(other_factors & r_primes, base=2))) |
||
if v == 1 |
if v == '1' |
||
w = 1 << (ix - 1) |
w = Int256(1) << (ix - 1) |
||
push!(new_partitions, (set_a, set_b ⊻ w, r_primes ⊻ w)) |
push!(new_partitions, (set_a, set_b ⊻ w, r_primes ⊻ w)) |
||
end |
end |
||
Line 72: | Line 78: | ||
partitions = new_partitions |
partitions = new_partitions |
||
end |
end |
||
result = |
result = Int256(-1) |
||
for (px, py, _) in partitions |
for (px, py, _) in partitions |
||
x, y = |
x, y = Int256(1), Int256(1) |
||
for p in primes |
for p in primes |
||
isodd(px) && (x *= p) |
isodd(px) && (x *= p) |
||
Line 81: | Line 87: | ||
py ÷= 2 |
py ÷= 2 |
||
end |
end |
||
newresult = n * invmod(x, y) % y * x - n |
newresult = ((n * invmod(x, y)) % y) * x - n |
||
result = result == -1 ? newresult : min(result, newresult) |
result = result == -1 ? newresult : min(result, newresult) |
||
end |
end |
||
Line 87: | Line 93: | ||
end |
end |
||
function test_erdős_woods() |
function test_erdős_woods(startval=3, endval=116) |
||
arr = fill((0, Int256(-1)), endval - startval + 1) |
|||
k, kcount = 16, 0 |
|||
@Threads.threads for k in startval:endval |
|||
⚫ | |||
⚫ | |||
while kcount < 20 |
|||
end |
|||
⚫ | |||
ewvalues = filter(x -> last(x) > 0, arr) |
|||
⚫ | |||
println(lpad(k, 3), " -> $a") |
|||
for (k, a) in ewvalues |
|||
println(lpad(k, 3), " -> $a") |
|||
k += 1 |
|||
end |
end |
||
end |
end |