The first functions is where the array is created, the the second is where it’s used.
def rangeres(modpn, n)
gap_residues = [1u64] of UInt64
n = 2 if modpn < 2310_u64
limit = modpn >> 1
range_n = limit // n
pc = (range_n - 3)|1
until pc.gcd(modpn) == 1; pc += 2 end
while pc < limit
until pc.gcd(modpn) == 1; pc += 2 end
gap_residues << pc
pc += (range_n|1)^1 # pc must be odd, so make range even: lsb = 0
end
gap_residues << limit if gap_residues.size < n + 1
gap_residues
end
def gapcounts(first, last, modpn)
gaps = Hash(UInt64, UInt64).new(0)
res = prev_res = first
inc = res % 6 == 5 ? 2 : 4
while res <= last
if res.gcd(modpn) == 1
gap = res - prev_res
gaps[gap] += 1
prev_res = res
#print "\r #{(res * 100)/last}%"
end
res += inc; inc ^= 0b0110
end
#puts
gaps.delete(0) # remove init gap size of '0'
gaps
end
def gaps(p, n = 8)
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] of UInt64
i = primes.index(p).not_nil!
modpn = primes[0..i].product
rescnt = primes[0..i].map{ |p| p-1 }.product
puts "modp#{p} = #{modpn}"
puts "rescnt = #{rescnt}"
a_n = Hash(UInt64, UInt64).new(0) # hash of gap coefficients a_n
gaps = [] of Hash(UInt64, UInt64) # array or gap hashes for each segment
i = 0
done = Channel(Nil).new() # process each segment in parallel
rangeres(modpn, n).each_cons_pair do |n1, n2|
spawn do
gaps << gapcounts(n1, n2, modpn)
print "\r#{i += 1} of #{n} segments done"
done.send(nil)
end end
n.times { done.receive } # wait for all threads to finish
puts
gaps.reduce { |h1, h2| a_n = h1.merge(h2) { |key, v1, v2| v1 + v2 } }
a_n = a_n.to_a.map{ |k, v| (k == 2 || k == 4) ? [k, v*2 | 1] : [k, v*2]}.to_h
a_n.to_a.sort
end
Here’s the error message.
25 | if res.gcd(modpn) == 1
^--
Error: no overload matches 'Int32#gcd' with type UInt64
But here’s another thing I just noticed:
In gaps
I do:
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] of UInt64
and all those values are cast as UInt64
s, because all the other values based on them are UInt64
s, but not for the single case. This seems like an edge case.