简体   繁体   中英

How to determine if an incredibly large number is prime?

The numbers I'm trying to figure out are in this form (some examples):

2 ^ 7 - 1 , 2 ^ 31 - 1 , 2 ^ 127 - 1 , et cetera.

This is not a homework question I was just researching primes and a lot of the information is going a bit over my head (Fourier transformations). Originally I was just using a function like this:

public static bool IsPrime(int candidate)
{
    if ((candidate & 1) == 0)
    {
        return candidate == 2;
    }

    for (int i = 3; (i * i) <= candidate; i += 2)
    {
        if ((candidate % i) == 0)
        {
            return false;
        }
    }

    return candidate != 1;
}

But that stopped working once the numbers got too big. I also looked in to the Sieve of Eratosthenes but apparently that only works for numbers of much smaller size.

To clarify, I'm not looking to write a program to find prime numbers, but rather, to determine if a given number is prime. I was looking in to the BigInteger structure in the .NET Framework and it looks promising if I could just write an efficient enough algorithm (I'd settle for something that finished in days).

I'm not sure if a mathematical proof would be better in this circumstance but I do not have much knowledge in that area as opposed to programming but if there was a proof that specialized in these kinds of numbers, that'd definitely be worth looking in to.

Thanks.

Factoring numbers is a big deal. The fact that it's difficult is the basis of modern cryptography.

Depending on how large your number is... You could get a list of all prime numbers up to the square root of the number you're looking for. This will be significantly less than just going up by 2 every time. The problem would be finding a list that large. If you have a number that's 10^100, then you'd need all primes of 10^50 and less, which is still a huge amount of numbers.

The numbers that you list: 2 ^ 7 - 1, 2 ^ 31 - 1, 2 ^ 127 - 1 are called Mersenne Numbers . There's already an entire distributed computing project to find those. It's called GIMPS .

So the answer to your question is not trivial. All the currently existing known primes of that form are listed here:

http://en.wikipedia.org/wiki/Mersenne_prime#List_of_known_Mersenne_primes

Do you have some upper limit on your numbers ? You mention that you'd settle for days. If so, unless your numbers are really large, your current algo will work.

You can also look at Miller–Rabin primality test

Java comes with an implementation of one of the probabilistic primality tests - see java.math.BigInteger.isProbablePrime(). I thought this would mean that C# had a look-alike in the language. I didn't find one, but looking for one I found some code on the web at http://www.codeproject.com/KB/cs/biginteger.aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM