Is there a faster way to get all primes between 0 - n?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace myPrime
{
public class simplePrimer
{
public List<BigInteger> primeList;
public void findVersion(BigInteger goal)
{
BigInteger number = 3;
primeList = new List<BigInteger> { 2 };
try
{
while (number < goal)
{
if (isPrime(number))
primeList.Add(number);
number += 2;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("i found " + primeList.Count + " Prime Numbers");
Console.WriteLine(primeList.Last() + "was the last Prime");
}
}
private bool isPrime(BigInteger number)
{
foreach (BigInteger prime in primeList)
if (number % prime == 0)
return false;
return true;
}
}
}
You can call it like:
primer.findVersion1(999999);