public static List <int> GetPrimesUpToN(int n) { var output = new List <int>(); var sieve = new PrimeSieve(n); for (int index = 2; index <= n; index++) { //If not already identified as a non-prime if (!sieve.IsComposite(index)) { //Add every multiple of index up to n for (int multiple = 2; multiple *index <= n; multiple++) { sieve.SetComposite(multiple * index); } } } for (int index = 2; index <= n; index++) { if (!sieve.IsComposite(index)) { output.Add(index); } } return(output); }
public static List<int> GetPrimesUpToN(int n) { var output = new List<int>(); var sieve = new PrimeSieve(n); for (int index = 2; index <= n; index++) { //If not already identified as a non-prime if (!sieve.IsComposite(index)) { //Add every multiple of index up to n for (int multiple = 2; multiple * index <= n; multiple++) { sieve.SetComposite(multiple * index); } } } for (int index = 2; index <= n; index++) { if (!sieve.IsComposite(index)) output.Add(index); } return output; }