简体   繁体   English

如何确定一个令人难以置信的大数字是否是素数?

[英]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. 2 ^ 7 - 1 2 ^ 31 - 1 2 ^ 127 - 1 ,等等。

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. 我也查看了EratosthenesSieve,但显然只适用于尺寸小得多的数字。

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). 我正在研究.NET Framework中的BigInteger结构,如果我能编写一个足够有效的算法(我会满足于几天内完成的东西),它看起来很有希望。

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. 这将远远低于每次上升2。 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. 如果你的数字是10 ^ 100,那么你需要10 ^ 50或更少的所有素数,这仍然是一个庞大的数字。

The numbers that you list: 2 ^ 7 - 1, 2 ^ 31 - 1, 2 ^ 127 - 1 are called Mersenne Numbers . 您列出的数字: 2 ^ 7 - 1, 2 ^ 31 - 1, 2 ^ 127 - 1称为Mersenne Numbers There's already an entire distributed computing project to find those. 已经有一个完整的分布式计算项目可以找到它们。 It's called GIMPS . 它被称为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 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 你也可以看看Miller-Rabin素性测试

Java comes with an implementation of one of the probabilistic primality tests - see java.math.BigInteger.isProbablePrime(). Java附带了一个概率素性测试的实现 - 参见java.math.BigInteger.isProbablePrime()。 I thought this would mean that C# had a look-alike in the language. 我认为这意味着C#在语言上看起来很像。 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 . 我找不到一个,但是找了一个我在网上找到了一些代码,网址是http://www.codeproject.com/KB/cs/biginteger.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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