Erdős–Woods numbers: Difference between revisions

(→‎{{header|Wren}}: Tidied but no quicker.)
Line 33:
=={{header|Julia}}==
{{trans|Python}}
<lang julia>""" modified from https://codegolf.stackexchange.com/questions/230509/find-the-erd%C5%91s-woods-origin/ """
<lang julia>""" Returns the smallest value for `a` of the Erdős-Woods number n, or -1 if n is not in the sequence """
 
using BitIntegers
 
"""
<lang julia>""" Returns the smallest value for `a` of the Erdős-Woods number n, or -1 if n is not in the sequence """
"""
function erdős_woods(n)
primes = Int[]
P = big"BigInt(1")
k = 1
while k < n
Line 43 ⟶ 49:
k += 1
end
divs = [evalpoly(big"2", [Int(a % p == 0) for p in primes]) for a in 0:n-1]
np = length(primes)
partitions = [(big"Int256(0"), big"Int256(0"), big"Int256(2")^np - 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))
new_partitions = Tuple{BigIntInt256, BigIntInt256, BigIntInt256}[]
factors = divs[i + 1]
other_factors = divs[n - i + 1]
Line 57 ⟶ 63:
continue
end
for (ix, v) in enumerate(digitsreverse(string(factors & r_primes, base=2)))
if v == '1'
w = Int256(1) << (ix - 1)
push!(new_partitions, (set_a ⊻ w, set_b, r_primes ⊻ w))
end
end
for (ix, v) in enumerate(digitsreverse(string(other_factors & r_primes, base=2)))
if v == '1'
w = Int256(1) << (ix - 1)
push!(new_partitions, (set_a, set_b ⊻ w, r_primes ⊻ w))
end
Line 72 ⟶ 78:
partitions = new_partitions
end
result = big"Int256(-1")
for (px, py, _) in partitions
x, y = big"Int256(1"), big"Int256(1")
for p in primes
isodd(px) && (x *= p)
Line 81 ⟶ 87:
py ÷= 2
end
newresult = ((n * invmod(x, y)) % y) * x - n
result = result == -1 ? newresult : min(result, newresult)
end
Line 87 ⟶ 93:
end
 
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
println("The first 20 Erdős–Woods numbers and their minimum interval start values are:")
aarr[k - startval + 1] = (k, erdős_woods(k))
while kcount < 20
end
a = erdős_woods(k)
ewvalues = filter(x -> iflast(x) a> !=0, -1arr)
println("The first 20$(length(ewvalues)) Erdős–Woods numbers and their minimum interval start values are:")
println(lpad(k, 3), " -> $a")
for (k, a) in kcount += 1ewvalues
endprintln(lpad(k, 3), " -> $a")
k += 1
end
end
4,102

edits