Created
June 18, 2020 15:03
-
-
Save ssiccha/ed6a9e953182e7ed461c2033714e29f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# suggested by Reimer Behrends | |
AllPrimesUpTo := function(n) | |
local i, j, sieve, result; | |
sieve := BlistList([1..n], [1..n]); | |
sieve[1] := false; | |
for i in [2..Int(n/2)] do | |
sieve[i*2] := false; | |
od; | |
i := 3; | |
while i * i <= n do | |
if sieve[i] then | |
j := 3*i; | |
while j <= n do | |
sieve[j] := false; | |
j := j + 2*i; | |
od; | |
fi; | |
i := i + 2; | |
od; | |
result := []; | |
for i in [2..n] do | |
if sieve[i] then | |
Add(result, i); | |
fi; | |
od; | |
return result; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment