Posted By

FMalk on 08/30/11


Tagged

prime


Versions (?)

Prime Numbers Generator


 / Published in: C#
 

  1. static List<int> findPrimes(int max)
  2. {
  3. var vals = new List<int>((int)(max / (Math.Log(max) - 1.08366)));
  4. var maxSquareRoot = Math.Sqrt(max);
  5. var eliminated = new BitArray(max + 1);
  6. vals.Add(2);
  7. for (int i = 3; i <= max; i += 2)
  8. {
  9. if (!eliminated[i])
  10. {
  11. if (i < maxSquareRoot)
  12. {
  13. for (int j = i * i; j <= max; j += 2 * i)
  14. eliminated[j] = true;
  15. }
  16. vals.Add(i);
  17. }
  18. }
  19. return vals;
  20. }

Report this snippet  

You need to login to post a comment.