Sorry, I missed to explain my code and that lead you to the wrong application.
In order to work properly, the code must be enclosed in a block. The symbol is not a label in a goto sense, just a mark to say “if the code that the block is executing raises this particular label, then rescue it, otherwise re-raise it”. Probably the example you’re executing isn’t raising the error (prime
is never greater than Math.isqrt(val)
? Commenting that line produces the same result).
The correct rewrite should be:
def sozpg(val)
md, rscnt = 30u64, 8
res = [7,11,13,17,19,23,29,31]
bitn = [0,0,0,0,0,1,0,0,0,2,0,4,0,0,0,8,0,16,0,0,0,32,0,0,0,0,0,64,0,128]
kmax = (val - 2) // md + 1
prms = Array(UInt8).new(kmax, 0)
catch(:here) do
prms.each_with_index do |resgroup, k|
res.each_with_index do |prm_r, i|
next if resgroup & (1 << i) != 0
prime = md * k + prm_r
Break.new(:here) if prime > Math.isqrt(val)
res.each do |ri|
kn, rn = (prm_r * ri - 2).divmod md
kpm = k * (prime + ri) + kn
while kpm < kmax; prms[kpm] |= bitn[rn]; kpm += prime end
end end end
end
primes = [2, 3, 5] of UInt64
prms.each_with_index do |resgroup, k|
res.each_with_index do |r_i, i|
primes << md * k + r_i if resgroup & (1 << i) == 0
end end
primes
end