Sieve of Eratosthenes: Difference between revisions

Content deleted Content added
Laurence (talk | contribs)
imported>Tecosaur
→‎{{header|Julia}}: More concise findall + boolean array based implementation
Line 10,560: Line 10,560:
Alternate version using <code>findall</code> to get all primes at once in the end
Alternate version using <code>findall</code> to get all primes at once in the end


<syntaxhighlight lang="julia">function sieve(n :: Int)
<syntaxhighlight lang="julia">function sieve(n::Integer)
isprime = trues(n)
primes = fill(true, n)
isprime[1] = false
primes[1] = false
for p in 2:n
for p in 2:n
if isprime[p]
primes[p] || continue
j = p * p
primes[p .* (2:n÷p)] .= false
if j > n
return findall(isprime)
else
for k in j:p:n
isprime[k] = false
end
end
end
end
end
findall(primes)
end</syntaxhighlight>
end</syntaxhighlight>