4

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.

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • 2
    There's an overview on Wikipedia: http://en.wikipedia.org/wiki/Primality_test – David Heffernan Sep 28 '11 at 20:23
  • 1
    [Mersenne primes](http://en.wikipedia.org/wiki/Mersenne_prime) also has info about that specific form of primes (`2^p-1`) – Mat Sep 28 '11 at 20:26
  • 1
    The hunt for large primes is a continuous scientific effort, with prizes awarded for new ones. To prove that an 'incredibly' large number is a prime is something for a super computer and perhaps a team of mathematicians. The practical limit for solving using .NET code running on every day hardware will likely be very low compared to the biggest prime found so far. – Adam Ralph Sep 28 '11 at 20:28
  • See for example http://www.ellipsa.eu/public/primo/primo.html – Dr. belisarius Sep 28 '11 at 20:28
  • 1
    There are very few known mersenne primes, you might as well list them all because you're almost certainly not going to find any new ones, they're very hard to find – harold Sep 28 '11 at 21:10

4 Answers4

3

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.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 3
    Determing whether numbers are prime or not is (probably) a smaller problem than factoring them. There are deterministic algorithms to test primality that are polynomial in the number of digits of the value, but no such known algorithms for factorization. Basically, you can prove that a number is composite without actually exhibiting its factors, and prove that it's prime without exhaustively checking all candidate factors. – Steve Jessop Sep 28 '11 at 20:57
  • The question is not about factoring, it's just about checking if a number is prime. Primality testing can be done efficiently on primes with a few thousand bits. For example using the probabilistic [Miller-Rabin test](http://en.wikipedia.org/wiki/Miller–Rabin_primality_test). – CodesInChaos Feb 04 '15 at 10:26
2

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

Mysticial
  • 464,885
  • 45
  • 335
  • 332
0

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

rohit89
  • 5,745
  • 2
  • 25
  • 42
  • `2^127-1` is really large (and is prime, as it happens, but you won't prove that using the questioner's code in a few days). – Steve Jessop Sep 28 '11 at 20:59
0

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.

mcdowella
  • 19,301
  • 2
  • 19
  • 25