Skip to content

Instantly share code, notes, and snippets.

@ssiccha
Created June 18, 2020 15:03
Show Gist options
  • Save ssiccha/ed6a9e953182e7ed461c2033714e29f9 to your computer and use it in GitHub Desktop.
Save ssiccha/ed6a9e953182e7ed461c2033714e29f9 to your computer and use it in GitHub Desktop.
# 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