Sieve of Pritchard: Difference between revisions

→‎Julia: Started by noticing that the max() should be a min(), then noticed there were some other places that could be made more efficient.
(→‎Python: Started by noticing that the max() should be a min(), then noticed there was some other places that could be made more efficient.)
(→‎Julia: Started by noticing that the max() should be a min(), then noticed there were some other places that could be made more efficient.)
Line 94:
=={{header|Julia}}==
{{trans|Raku}}
Added add/remove statistics. "Removed" figure is a combination of composites and primes under sqrt of limit.
 
Running on Julia 1.0
<syntaxhighlight lang="julia">""" Rosetta Code task rosettacode.org/wiki/Sieve_of_Pritchard """
 
""" Pritchard sieve of primes up to limit, uses limit for type of the primes """
 
""" using Julia 1.0 (on Tio.run) """
 
function pritchard(limit::T) where T <: Integer
members = Set(one(T))
steplength = 1 # wheel size
prime = T(2)
primes = T[]
whilenlimit = prime * primesteplength <=# wheel limit
ac = 2 # added count, since adding 1 & 2 during initialization
rc = 1 # removed count, since 1 will be removed at the end
rtlim = T(floor(sqrt(limit))) # this allows the main loop to go
while prime <= rtlim # one extra time, eliminating the follow-up for
# the last partial wheel (if present)
if steplength < limit
for w in members
n = w + steplength
while n <= max(prime * steplength, limit)nlimit
push!(members, n)
n ac += steplength1
n += steplength
end
end
steplength = min(primenlimit *# steplength,advance limit)wheel size
end
endnp = 5
for w in sort!(collect(members))
delete!(members,if np == 5 && w > prime *np = w) end
endn = prime * w
if n > nlimit break end
rc += 1
pushdelete!(members, n)
end
if np < prime break end
push!(primes, prime)
prime# =this primeworks, ==but 2doubles ?the 3runtime :for minimum(filter(>(1),limit = members))1000000
# prime = prime == 2 ? 3 : sort!(collect(members))[2]
end
# this is faster
if steplength < limit
forprime w= inprime members== 2 ? 3 : np
n = w + steplength
nlimit = min(steplength * whileprime, nlimit) <=# advance wheel limit
push!(members, n)
n += steplength
end
end
steplength = limit
end
appenddelete!(primesmembers, filter(!=(1), members))
println("up to ",limit, ", added:", ac, " removed:", rc, " prime count:", length(primes) + length(members) )
return sort!(filterappend!(<=(limit)primes, primesmembers))
end