Sieve of Erastosthenes
Runs in time. Used to compute all Prime Number up to .
Taken from cp-algorithms
Why are you allowed to start
j
ati*i
?Because if
j
wasn’t prime beforei*i
, it should have already been marked by a smallerj
.
- Ex: consider
i = 5
. You only need to start atj = 5*5
, because any smaller number (5*4, 5*3, 5*2
) should have been marked by a previous prime number since those are non-prime.
Segmented Sieve
I ran into this DMOJ problem and it required , which is huge and meant I couldn’t store all of it into the array.
https://www.geeksforgeeks.org/segmented-sieve/ On CP algorithms there is also a solution.