有谁知道并行素数分解算法的方法是什么?
我不知道应该在算法的哪个阶段将其划分为线程。我如何以并行方式考虑素数分解?
考虑以下一个线程代码:
public static void primeFactorization(ArrayList<Integer> factors, int num){
//factors is an array to save the factorization elements
//num is the number to be factorized
int limit = num/2+1;
if(isPrime(num))
factors.add(num);
else{
while(num%2==0){
factors.add(2);
num=num/2;
}
for (int i=3; i<limit; i+=2){
while (isPrime(i) && num%i==0){
factors.add(i);
num = num/i;
}
}
}
}
private static boolean isPrime(int x) {
int top = (int)Math.sqrt(x);
for (int i = 2; i <= top; i++)
if ( x % i == 0 )
return false;
return true;
}