Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
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 |
<syntaxhighlight lang="julia">function sieve(n::Integer) |
||
primes = fill(true, n) |
|||
primes[1] = false |
|||
for p in 2:n |
for p in 2:n |
||
primes[p] || continue |
|||
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> |
||