George Mamaladze
George Mamaladze

Reputation: 7931

Is there a publically available table of prime numbers in .NET

I know there is one which is used in all kind of .NET dictionaries and hashtables in:

internal static class HashHelpers

Upvotes: 7

Views: 475

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

  1. As far as I know, there is no public version of that table available in .NET
  2. Because this table is not a table of all prime numbers in a range, but rather a table of arbitrarily chosen subset of prime numbers suitable for a particular purpose (sizing hash-based containers)
  3. No, you should either generate your own table on the fly, or copy-paste a table from one of many complete sources.

Upvotes: 5

Dave New
Dave New

Reputation: 40002

I cannot answer your question regarding the availability of HashHelpers, but here are ways of implementing it yourself.

Here a post with some imperative implementations on generating prime numbers: Most elegant way to generate prime numbers

Additionally, you can do it in LINQ:

var odds =
    from n in Enumerable.Range(0, int.MaxValue)
    select 3 + (long) n * 2;

var primes = (new[] { 2L }).Concat(
    from p in odds
    where ! odds.TakeWhile(odd => odd * odd <= p).Any(odd => p % odd == 0)
    select p);

Source: http://jacobcarpenter.wordpress.com/2008/03/26/linq-to-prime-numbers/

Edit: Don't use int.MaxValue in your initial range. Limit this to something appropriate.

Upvotes: 3

Related Questions