简体   繁体   中英

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

I am a student studying IT in an University. I've been giving an assignment of searching for prime numbers above one quadrillion. Steps have been given:

  • Start number as one quadrillion

  • Select odd-numbered candidates

  • Divide them by every odd integer between 3 and their square root. 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. 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. And you're not incrementing i by 2 (you only need to check for odd divisors).

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. Just so you know. :)

In case you need them, primes above one quadrillion are:

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

Above 10 quadrillion:

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. 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. 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.

The MR test is much faster than trial division for large numbers.

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