Sieve of Pritchard: Difference between revisions

m
blue style formatting
(→‎Julia: Started by noticing that the max() should be a min(), then noticed there were some other places that could be made more efficient.)
m (blue style formatting)
Line 93:
 
=={{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,. usesUses type of `limit` arg for type of the primes """
function pritchard(limit::T, verbose=false) where {T <: Integer}
 
""" using Julia 1.0 (on Tio.run) """
 
function pritchard(limit::T) where T <: Integer
members = Set(one(T))
steplength = 1 # wheel size
Line 113 ⟶ 107:
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
Line 127 ⟶ 121:
np = 5
for w in sort!(collect(members))
if np == 5 && w > prime && (np = w end)
n = prime * w
if n > nlimit && break end
rc += 1
delete!(members, n)
end
if np < prime && break end
push!(primes, prime)
# this works, but doubles the runtime for limit = 1000000
# prime = prime == 2 ? 3 : sort!(collect(members))[2]
# this is faster
prime = prime == 2 ? 3 : np
nlimit = min(steplength * prime, limit) # advance wheel limit
end
delete!(members, 1)
verbose && println(
println( "up to ",$limit, ", added:", $ac, " removed:", $rc, " prime count: ", length(primes) + length(members) )
length(primes) + length(members),
)
return sort!(append!(primes, members))
end
4,108

edits