The following code snippet calculate prime factors of a given number:
public static LinkedList<Long> getPrimeFactors(Long number) {
LinkedList<Long> primeFactors = new LinkedList<Long>();
for (Long factor = Long.valueOf(2); factor <= number / factor; factor++) {
if (number % factor == 0) {
primeFactors.add(factor);
while (number % factor == 0) {
number /= factor;
}
}
}
if (number > 1) {
primeFactors.add(number);
}
return primeFactors;
}
It takes 140937ms to calculate prime factors of 9223372036854775783 (it is a last prime less than Long.MAX_VALUE
). Is there any way to implement this factorization by concurrency i.e., by using ExecutorService
?
Edit:
public static void getPrimeFactors(Long number) {
LinkedList<Long> primeFactors = new LinkedList<Long>();
if (number % 2 == 0) {
primeFactors.add(2L);
while (number % 2 == 0) {
number /= 2;
}
}
long limit = (long) Math.sqrt(number) + 1;
ExecutorService service = Executors.newFixedThreadPool(2);
LinkedList<Future<LinkedList<Long>>> futures = new LinkedList<Future<LinkedList<Long>>>();
futures.add(service.submit(new PrimeFactor(3, limit / 2, number)));
futures.add(service.submit(new PrimeFactor(1 + limit / 2, limit, number)));
for (Future<LinkedList<Long>> future : futures) {
try {
primeFactors.addAll(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
service.shutdown();
if(number>1) {
primeFactors.add(number);
}
System.out.println(primeFactors);
}
private static class PrimeFactor implements Callable<LinkedList<Long>> {
private long lowerLimit;
private long upperLimit;
private Long number;
public PrimeFactor(long lowerLimit, long upperLimit, Long number) {
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
this.number = number;
}
public LinkedList<Long> call() throws Exception {
LinkedList<Long> primeFactors = new LinkedList<Long>();
for (long i = lowerLimit; i < upperLimit; i += 2) {
if (number % i == 0) {
primeFactors.add(i);
while (number % 2 == 0) {
number /= i;
}
}
}
return primeFactors;
}
}
2nd Edit:
public static LinkedList<Long> getPrimeFactorsByFastGeneralMethod(long number) {
LinkedList<Long> primeFactors = new LinkedList<Long>();
if (number % 2 == 0) {
primeFactors.add(2L);
while (number % 2 == 0) {
number /= 2;
}
}
long limit = (long) Math.sqrt(number);
for (long factor = 3; factor <= limit; factor += 2) {
if (number % factor == 0) {
primeFactors.add(factor);
while (number % factor == 0) {
number /= factor;
}
}
}
if (number > 1) {
primeFactors.add(number);
}
return primeFactors;
}
Now code snippet:
LinkedList<Long> primeFactors = Factorization.getPrimeFactorsByConcurrentGeneralMethod(600851475143L);
System.out.println("Result: " + primeFactors.get(primeFactors.size() - 1));
primeFactors = Factorization.getPrimeFactorsByFastGeneralMethod(600851475143L);
System.out.println("Result: " + primeFactors.get(primeFactors.size() - 1));
is giving the output:
Result: 600851475143
Result: 6857
Note: The class name is Factorization
and I changed the name of the method getPrimeFactors
to getPrimeFactorsByConcurrentGeneralMethod
Long
variables bylong
ones. This reduces the overhead of boxing and unboxing the wrapper objects. – ReinwaldBigInteger
in place ofLong
. – Billy