Guest User

Untitled

a guest
Feb 20th, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import Thirteen.Thirteen;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.ArrayList;
  5. import java.util.concurrent.ExecutorCompletionService;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8.  
  9. @SuppressWarnings("Duplicates")
  10. public class Application {
  11.     public static void main(String[] args) {
  12.         Application application = new Application();
  13.         application.executeWithoutPara();
  14.         application.executeWithPara();
  15.     }
  16.  
  17.     private void executeWithPara() {
  18.         try {
  19.             long startTime = System.currentTimeMillis();
  20.             ArrayList<BigInteger> possiblePValues = getPrimes(Configuration.instance.rangeFrom, Configuration.instance.rangeTo);
  21.             ArrayList<BigInteger> possibleQValues = new ArrayList<>(possiblePValues);
  22.             ArrayList<BigInteger> possibleRValues = new ArrayList<>(possiblePValues);
  23.  
  24.             ExecutorService executorService = Executors.newFixedThreadPool(Configuration.instance.maximumNumberOfCores);
  25.             ExecutorCompletionService executorCompletionService = new ExecutorCompletionService(executorService);
  26.  
  27.             for (BigInteger pValue : possiblePValues) {
  28.                 for (BigInteger qValue : possibleQValues) {
  29.                     for (BigInteger rValue : possibleRValues) {
  30.                         executorCompletionService.submit(new Thirteen(pValue, qValue, rValue), null);
  31.                     }
  32.                 }
  33.             }
  34.             executorCompletionService.take();
  35.  
  36.             executorService.shutdown();
  37.             System.out.println("Task took: " + (System.currentTimeMillis() - startTime) + "ms\n");
  38.  
  39.         } catch (
  40.                 Exception e) {
  41.             System.out.println(e.getMessage());
  42.         }
  43.     }
  44.     private void executeWithoutPara() {
  45.         long startTime = System.currentTimeMillis();
  46.         ArrayList<BigInteger> possiblePValues = getPrimes(Configuration.instance.rangeFrom, Configuration.instance.rangeTo);
  47.         ArrayList<BigInteger> possibleQValues = new ArrayList<>(possiblePValues);
  48.         ArrayList<BigInteger> possibleRValues = new ArrayList<>(possiblePValues);
  49.  
  50.         for (BigInteger pValue : possiblePValues) {
  51.             for (BigInteger qValue : possibleQValues) {
  52.                 for (BigInteger rValue : possibleRValues) {
  53.                     new Thirteen(pValue, qValue, rValue).calculateEquation();
  54.                 }
  55.             }
  56.         }
  57.         System.out.println("Task took: " + (System.currentTimeMillis() - startTime) + "ms\n");
  58.     }
  59.     ArrayList<BigInteger> getPrimes(BigInteger start, BigInteger end) {
  60.         ArrayList<BigInteger> primeNumbersList = new ArrayList<>();
  61.         for (BigInteger counter = start; counter.compareTo(end) < 0; counter = counter.add(BigInteger.ONE)) {
  62.             if (returnPrime(counter)) {
  63.                 primeNumbersList.add(counter);
  64.             }
  65.         }
  66.         return primeNumbersList;
  67.     }
  68.     boolean returnPrime(BigInteger number) {
  69.         if (!number.isProbablePrime(5)) return false;
  70.         BigInteger two = new BigInteger("2");
  71.         if (!two.equals(number) && BigInteger.ZERO.equals(number.mod(two))) return false;
  72.         for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(number) < 1; i = i.add(two)) {
  73.             if (BigInteger.ZERO.equals(number.mod(i)))
  74.                 return false;
  75.         }
  76.         return true;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment