0

Possible Duplicates:
Program to find prime numbers in C#
prime numbers c#
Most elegant way to generate prime numbers

Hi Guys.

How does one check if a number is prime or not?

Community
  • 1
  • 1
user336671
  • 311
  • 2
  • 5
  • 8
  • 1
    In my prime it was pretty big ... – Hamish Grubijan May 31 '10 at 18:47
  • 1
    @Randolpho: Your comment is a possible duplicate of "possible duplicate of Program to find prime numbers in C# – Thorarin" – Simon May 31 '10 at 18:51
  • 3
    I think there should be a dedicated supercomputer) server with a giant hashtable, which gets remote requests such as "prime?1234567891011" and which responds (at a high level) with "1234567891011 is prime.", "1234567891011 is not prime.", "1234567891011 is too large.", "1234567891011 has not yet been factored.", "bad number format". – Hamish Grubijan May 31 '10 at 18:53
  • @Simon: keep in mind that comment was automatically generated. – Randolpho May 31 '10 at 18:53
  • 2
    @Hamish Grubijan: that'd be a great web application – Randolpho May 31 '10 at 18:53
  • @Randolpho: Really? How can you automatically generate comments? – Simon May 31 '10 at 18:55
  • 1
    (Continued) I suppose such server can even utilize the file system (to serve more numbers) and organize files in directories for faster lookup, while also having most popular values sitting in memory. However, the most performance would probably come from filtering ranges of numbers. – Hamish Grubijan May 31 '10 at 18:57
  • @Simon: when voting to close as an exact duplicate, the system will automatically add a comment linking to the dupe that was nominated as a dupe, helping others to vote it closed if necessary. The system will also delete those comments once the question is actually closed. Note that there were three or four separate dupe links provided before the post was closed, and they're all gone. All were automatically generated. Now... I'm not a post-close nazi like some mods, but this one was a no-brainer. Dupe. – Randolpho May 31 '10 at 19:01
  • @Hamish Grubijan: rather than a hashcode and files and other complicated stuff like that, just use a B-Tree, which can be found in any simple database. `Select number from knownprimes where number=@possiblyPrimeNumber`. If you get a row, it's prime. If not, it's not prime or hasn't been tested for primality. – Randolpho May 31 '10 at 19:04
  • @Hamish Grubijan: On second thought... the size of the largest known primes is larger than can be held in an integer data type in a database. Silly idea. – Randolpho May 31 '10 at 19:05
  • By the way, there is a fast way to populate the database with prime numbers. http://stackoverflow.com/questions/2211990/how-to-implement-an-efficient-infinite-generator-of-prime-numbers-in-python – Hamish Grubijan Jun 01 '10 at 02:30
  • http://primes.utm.edu/nthprime/index.php#nth – Hamish Grubijan Jun 01 '10 at 02:40
  • http://en.wikipedia.org/wiki/Primality_test – Michael Borgwardt May 31 '10 at 18:42

4 Answers4

5

That's one I use to write any time i need to do this check:

inline bool isPrime(const int a)
{
    if(a == 1) return false;
    if(a == 2 || a == 3) return true;
    if(!(a & 1)) return false;
    if(!((a + 1)%6 || (a-1)%6)) return false;
    int q = sqrt((long double)a) + 1;
    for(int v = 3; v < q; v += 2)
        if(a % v == 0)
            return false;
    return true;
}

It works really well because of some useful prunings.

UnknownGosu
  • 786
  • 5
  • 9
2

Don't know if there's a standard function for it, but you could always built a method using the Sieve of Eratosthenes (link: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). Pretty easy to implement.

riffnl
  • 2,773
  • 16
  • 29
  • Sieve of Eratosthenes is used to create **list** of prime numbers, not check is number prime. – Andrey May 31 '10 at 18:54
  • True, but a prime sieve can compute each of the prime numbers up to 1 billion in a couple hundred milliseconds (in a single thread, on modern hardware), so I think it is worth mentioning for reasonably small numbers. I typically implement isPrime using a combination of a sieve (kick it off in a separate thread on start-up, and use the results as they become available) with the relatively fast Miller-Rabin test. The sieve also enables me to ask for the Nth prime (if it is available). – Jeff G May 29 '17 at 19:08
0

There are a couple of c# versions here: LTD Puzzle 4: Finding Prime Numbers There are also f#, c, JavaScript. PHP etc etc versions

SQLMenace
  • 125,031
  • 23
  • 195
  • 219
0

If it is divisible by 1 or itself, it is prime. You can shorten the number of tests by realizing that all primes except 2 are odd, or it would be divisible by 2. Also, all prime numbers above 5 can be represented as 6n+1 or 6n-1, but not all numbers generated this way are primes. Additionally, the largest possible divisor of the number will be its square root. These facts are enough to make the test much faster than if you just tested all numbers till the number you want to check.

liewl
  • 3,849
  • 12
  • 41
  • 60
  • I think the Sieve of Eratosthenes is pretty elegant and performs well – SQLMenace May 31 '10 at 18:50
  • Yeah, but for some problems, like "Find the 10001st prime" from project euler, I chose to test each possible prime, since I didn't knew how large the 10001st prime would be. If I knew that, the sieve would probably be faster. – liewl May 31 '10 at 18:59
  • David, for those problems there is a faster way actually. Primality testing gets you into computational trouble very fast. – Hamish Grubijan Jun 01 '10 at 02:29
  • I've taken a look at some mathematician-named methods, but they're overly difficult to comprehend for my not so mathematically versed mind. – liewl Jun 01 '10 at 11:28