An ancient algorithm for finding primes: Sieve of Eratosthenes.
using System; using System.Collections.Generic; using System.Linq; namespace SoEPrimes { static class SoEPrimeFinder { static void Main() { var primes = new PrimeFinder(1024).Primes; Action<long> print = PrintResults; primes.ToList().ForEach(print); Console.ReadKey(); } private static void PrintResults(long result) { Console.WriteLine(result); } } public class PrimeFinder { readonly List<long> _primes = new List<long>(); public PrimeFinder(long seed) { CalculatePrimes(seed); } public IEnumerable<long> Primes { get { return _primes; } } private void CalculatePrimes(long maxValue) { for (var checkValue = 3; checkValue <= maxValue; checkValue += 2) { if (IsPrime(checkValue)) { _primes.Add(checkValue); } } } private bool IsPrime(long checkValue) { return _primes.All(prime => (checkValue % prime) != 0 || !(prime <= Math.Sqrt(checkValue))); } } }