java - Parallel prime factorization -
this question has answer here:
does have idea what's approach parallel prime factorization algorithm ?
i can't figure out @ stage of algorithm should divide threads .. how can think prime factorization in parallel way ?
consider following 1 thread code:
public static void primefactorization(arraylist<integer> factors, int num){ //factors array save factorization elements //num number factorized int limit = num/2+1; if(isprime(num)) factors.add(num); else{ while(num%2==0){ factors.add(2); num=num/2; } (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); (int = 2; <= top; i++) if ( x % == 0 ) return false; return true; }
it seems use fork/join framework. seems should able use recursively passing in new factors find. try taking @ recursiveaction well. in pseudo code should able following:
public void getfactors(list<integer> factors, int num){ if(you can find factor){ add 2 factors pool factored further } else{ factors.add(num); } }
as side note, might have better performance if started in middle (num/2) , went there opposed starting @ one.
Comments
Post a Comment