Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Is there a public one somwhere else as well?
  • If no, why is it kept internal isn't it something very commonly used?
  • Is the copy & paste the way to go if I need prime numbers in my code?
like image 491
George Mamaladze Avatar asked Jul 30 '12 11:07

George Mamaladze


People also ask

How do you find a list of prime numbers?

And so on. The numbers that remain are prime: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

How do I get a list of prime numbers in C++?

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don't think that's the way my book wants it to be written.

How do I print all prime numbers?

First, take the number N as input. Then use a for loop to iterate the numbers from 1 to N. Then check for each number to be a prime number. If it is a prime number, print it.


2 Answers

  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.
like image 51
Sergey Kalinichenko Avatar answered Sep 21 '22 23:09

Sergey Kalinichenko


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.

like image 40
Dave New Avatar answered Sep 20 '22 23:09

Dave New