简体   繁体   English

了解任务:使用servlet搜索大于1千万亿的素数

[英]Understanding an assignment: Search for prime number above 1 quadrillion using servlets

I am a student studying IT in an University. 我是一名在大学学习IT的学生。 I've been giving an assignment of searching for prime numbers above one quadrillion. 我一直在寻找寻找超过一千万亿的素数的任务。 Steps have been given: 已经给出了步骤:

  • Start number as one quadrillion 起始数为1千万亿

  • Select odd-numbered candidates 选择奇数编号的候选人

  • Divide them by every odd integer between 3 and their square root. 将它们除以3和它们的平方根之间的每个奇数。 if one of the integers evenly divides the candidate, it's declared prime. 如果其中一个整数均匀地划分候选者,则它被声明为素数。

Now this is what i've come up with : 现在这就是我想出来的:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PrimeSearcher extends HttpServlet{
    private long number = 10000000000000001L;
private boolean found = false;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    PrintWriter out = response.getWriter();

    while(!checkForPrime(number)){
        number = number+2;
    }

    if(found){
        out.println("The first prime number above 1 quadrillion is : " + number);
    }

}

public boolean checkForPrime(long numberToCheck){
    double sqrRoof = Math.sqrt(numberToCheck);
    for(int i=3; i< sqrRoof; i++){
        if(numberToCheck%i==0){
         return false;
        }
    }
    found= true;
    return found;
}


}

My worry is that am not sure whether am on the right path and another issue is that this always one number ,the first one. 我担心的是,我不确定是否在正确的道路上,另一个问题是,这总是一个数字,第一个。 after googling i found that on servlet.com and javafaq that they are using thread and i've run theirs and it seem to be cool. 谷歌搜索后,我发现在servlet.comjavafaq ,他们正在使用线程,我运行他们的,它似乎很酷。 I don't really understand that one but it gives different numbers. 我真的不明白那个,但它给出了不同的数字。

So I am now confused right now about how to implement that algorithm and i really don't want to copy that one. 所以我现在很困惑如何实现该算法,我真的不想复制那个算法。 Maybe after understanding their method i can code this algorithm better. 也许在了解了他们的方法后,我可以更好地编码这个算

Thanks 谢谢

I think it looks OK, but you might need the i in checkForPrime to be a long type. 我觉得它看起来不错,但你可能需要checkForPrimei是一个long类型。 And you're not incrementing i by 2 (you only need to check for odd divisors). 并且你不会将i递增2(你只需要检查奇数除数)。

Just be prepared for this to take a long time....... 只是为此做好准备需要很长时间.......

此外,您需要checkForPrime直到i<=sqrRoof (sqrRoof可能是一个整数)。

you have 10 quadrillion written in your source code, not one quadrillion. 您的源代码中写入了10千万亿,而不是一千万亿。 Just so you know. 你知道吗 :) :)

In case you need them, primes above one quadrillion are: 如果您需要它们,超过一千万亿的素数是:

37,91,159,187,223 ...

Above 10 quadrillion: 超过10千万亿:

61,69,79 ... 

The standard way to check a large prime is to generate a list of low primes, up to some limit, using the Sieve of Eratosthenes. 检查大质数的标准方法是使用Eratosthenes筛选生成一个低质数列表,达到一定限度。 Use that list to check odd numbers in the quadrillion range, to eliminate most non-primes. 使用该列表检查千万亿次范围内的奇数,以消除大多数非素数。

Then use the Miller-Rabin probabilistic prime test to check if any remaining large number really is prime. 然后使用Miller-Rabin概率素数检验来检查剩余的大数是否真的是素数。 If you repeat the MR test up to 64 times, then there is a far larger chance that your hardware has failed than that you have inadvertently found a composite number. 如果重复MR测试最多64次,那么硬件失败的可能性远远大于您无意中发现的复合数。

The MR test is much faster than trial division for large numbers. 对于大数,MR测试比试验分区快得多。

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

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