import Thirteen.Thirteen; import java.math.BigInteger; import java.util.ArrayList; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @SuppressWarnings("Duplicates") public class Application { public static void main(String[] args) { Application application = new Application(); application.executeWithoutPara(); application.executeWithPara(); } private void executeWithPara() { try { long startTime = System.currentTimeMillis(); ArrayList possiblePValues = getPrimes(Configuration.instance.rangeFrom, Configuration.instance.rangeTo); ArrayList possibleQValues = new ArrayList<>(possiblePValues); ArrayList possibleRValues = new ArrayList<>(possiblePValues); ExecutorService executorService = Executors.newFixedThreadPool(Configuration.instance.maximumNumberOfCores); ExecutorCompletionService executorCompletionService = new ExecutorCompletionService(executorService); for (BigInteger pValue : possiblePValues) { for (BigInteger qValue : possibleQValues) { for (BigInteger rValue : possibleRValues) { executorCompletionService.submit(new Thirteen(pValue, qValue, rValue), null); } } } executorCompletionService.take(); executorService.shutdown(); System.out.println("Task took: " + (System.currentTimeMillis() - startTime) + "ms\n"); } catch ( Exception e) { System.out.println(e.getMessage()); } } private void executeWithoutPara() { long startTime = System.currentTimeMillis(); ArrayList possiblePValues = getPrimes(Configuration.instance.rangeFrom, Configuration.instance.rangeTo); ArrayList possibleQValues = new ArrayList<>(possiblePValues); ArrayList possibleRValues = new ArrayList<>(possiblePValues); for (BigInteger pValue : possiblePValues) { for (BigInteger qValue : possibleQValues) { for (BigInteger rValue : possibleRValues) { new Thirteen(pValue, qValue, rValue).calculateEquation(); } } } System.out.println("Task took: " + (System.currentTimeMillis() - startTime) + "ms\n"); } ArrayList getPrimes(BigInteger start, BigInteger end) { ArrayList primeNumbersList = new ArrayList<>(); for (BigInteger counter = start; counter.compareTo(end) < 0; counter = counter.add(BigInteger.ONE)) { if (returnPrime(counter)) { primeNumbersList.add(counter); } } return primeNumbersList; } boolean returnPrime(BigInteger number) { if (!number.isProbablePrime(5)) return false; BigInteger two = new BigInteger("2"); if (!two.equals(number) && BigInteger.ZERO.equals(number.mod(two))) return false; for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(number) < 1; i = i.add(two)) { if (BigInteger.ZERO.equals(number.mod(i))) return false; } return true; } }