$ ruby -v
ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux]
$ crystal -v
Crystal 1.22.3 [3462c0774] (2026-11-13)
LLVM: 22.1.3
Default target: x86_64-unknown-linux-gnu
Ruby Code
def sozpn(val, residues)
res = residues
modpn, rescnt, r0 = res.last-1,res.size, res[0] # for in-order Pn generators only
return [2,3,5,7,11,13].select{ |p| p <= val } if val < r0
num = val - 1 | 1 # if val even number subtract 1 to make odd
k = num/modpn; resk = num-modpn*k; r = 0 # num resgroup|residue values, 1st res posn
while resk >= res[r]; r += 1 end # find largest residue <= num posn in its resgroup
maxpcs = k*rescnt + r # number of prime candidates <= num
puts "maxpcs = #{maxpcs}"
pcs = Array.new(maxpcs) # array for pcs table, set to all 'prime'|nil
sqrtn = Integer.sqrt(num) # only need sieve primes up to here
# sieve to mark prime multiples locations in prms, unmarked (falsy) are then primes
maxpcs.times do |i| # do for number of pcs upto sqrt(end_num)
next if pcs[i] # skip pc if not prime
prm_r = res[i%rescnt] # if prime save its residue value
prime = modpn*(k=i/rescnt)+prm_r # numerate its value; set k resgroup value
break if prime > sqrtn # stop sieving when true
prmstep = prime * rescnt # step value for prime's multiples
res.each do |ri| # mark prime's multiples in pcs table
# compute (prime * pc_i) position parameters in prms
ki, rn = (prm_r * ri - 2).divmod modpn
prm_mult = rescnt*(k*(prime + ri) + ki) + res.index(rn + 2)
while prm_mult < maxpcs; pcs[prm_mult] = true; prm_mult += prmstep end
end
end
# prms now has all the prime multiples marked from r0..N, extract unmarked primes
primes = [2,3,5,7,11,13].select{ |p| p < r0 } # init with Pn’s modulus primes
maxpcs.times do |i| # find|numerate the prime values
primes << modpn*(i/rescnt) + res[i%rescnt] if !pcs[i]
end
primes # return array of primes <= N
end
def tm; t = Time.now; yield; Time.now - t end
resp4, resp3, resp24, resp5 = [3,5], [5,7], [5,7,11,13,17,19,23,25], [7,11,13,17,19,23,29,31]
resp7 = [11, 13, 17, 19, 23, 2def sozpn(val, residues)
29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97,101,103,107,109,113,121,127,131,137,139,143,149,151,157,163,
167,169,173,179,181,187,191,193,197,199,209,211]
n = 10_000_000_000
primes = []; puts tm{ primes = sozpn(n,resp7) }
puts "The number of primes up to #{n} are #{primes.size}"