3

Does anyone have any idea what's the approach for parallel prime factorization algorithm ?

I can't figure out at which stage of the algorithm I should divide it into threads .. How can I think about Prime factorization in a parallel way ?

consider the following one thread code:

    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;
    }
John Kane
  • 4,300
  • 1
  • 24
  • 39
Ohad
  • 1,381
  • 2
  • 14
  • 34
  • could you explain on what parallel prime factorization is, and what its supposed to do? – Infested May 08 '13 at 13:44
  • 2
    You can check many numbers for being primes at once, for a start. – Dariusz May 08 '13 at 13:45
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson May 08 '13 at 13:46
  • 1
    You'll want each iteration of the for loop in a thread of its own. – Geeky Guy May 08 '13 at 13:46
  • no need to excuse yourself. you can ask as many questions as you like. – UmNyobe May 08 '13 at 13:46
  • well prime factorization is the process of breaking a number into its prime factors.. for example : 888 =17 x 89 x 569 .. however I was asked to make a program that uses few threads at the same time .. but the way the algorithm works is by starting diving from the smallest number.. that's why I don't know how to divide the work between a few threads... – Ohad May 08 '13 at 13:49
  • Look here: http://stackoverflow.com/questions/12559788/parallel-algorithms-for-generating-prime-numbers-possibly-using-hadoops-map-re – Kuchi May 08 '13 at 13:50
  • Do you have to make the factorisation parallel? Or are you given multiple numbers and you can process them in parallel? – TheMerovingian May 08 '13 at 13:50
  • I am given only one number.. so yes I have to make factorization parallel.. – Ohad May 08 '13 at 13:52
  • @Renan, I am not sure you want each iteration of the for loop in its own thread, you might accidentally include the same number unless you add a way to check for that. – John Kane May 08 '13 at 14:13

1 Answers1

0

It seems like this could be a really good use for the Fork/Join Framework. It seems like you should be able to use this by recursively passing in the new factors that you find. Try taking a look at RecursiveAction as well. In pseudo code you should be able to do something like the following:

public void getFactors(List<Integer> factors, int num){
    if(you can find a factor){
        add the two factors to the pool to be factored further
    }
    else{
        factors.add(num);
    }  
}

As a side note, it might have better performance if you started in the middle (num/2) and went from there opposed to starting at one.

John Kane
  • 4,300
  • 1
  • 24
  • 39
  • Actually, it doesn't even require the framework. He just needs to spawn a new thread for each factor found, then have some sort of synchronized object to collect all the factors. – Austin B May 08 '13 at 13:56
  • Why will I want to create a thread for a factor? It's a prime number so there is nothing to calculate anymore.. why is it required a thread for it? – Ohad May 08 '13 at 13:59
  • You wouldnt need a thread for factor, you could use thread pooling (which the fork/join framework has built in). You could basically break up the task of finding factors. When you find a new one, throw both new factors into the pool to be broken up further. – John Kane May 08 '13 at 14:01
  • I just got an idea... every natural number has only one factorization... I can use this fact by creating maybe 4 threads , and each thread will check for divisible prime numbers in different range of numbers..the first thread will look for primes between 2 to 50.. etc... and then all of those prime numbers that I found will be my factorization.. – Ohad May 08 '13 at 14:13
  • Im not sure that is the way to go. You would need to be careful to not include duplicate values. For example, one thread might find that 2*25=50 and another might also see that 25*2=50. It might be safer to use some form of thread pooling where each factor gets added to the pool to be broken up further. That would avoid the possibility of including duplicate numbers. – John Kane May 08 '13 at 14:16
  • But each thread will check different range of numbers.. so how can they have the same calculation ? .. – Ohad May 08 '13 at 14:19
  • The reason I don't use what you have suggested is because I am not sure I am allowed.. I haven't even studied this concept of thread pooling.. – Ohad May 08 '13 at 14:22
  • lets say thread1 checks 1-12 thread2 checks 13-25 thread one will find 2*25, and thread2 will find 25*2 – John Kane May 08 '13 at 14:23
  • well but I will check only the prime numbers in those ranges.. – Ohad May 08 '13 at 14:24
  • I think that would double the work, but that should work. – John Kane May 08 '13 at 14:26
  • just out of curiosity, what types of things have you gone over in class? – John Kane May 08 '13 at 14:26
  • I am not getting you.. suppose I have the number 8976 = 2 x 2 x 2 x 2 x 3 x 11 x 17 , so the first range is 2-5 second range 6-10 , third range is 11-17 ... at each range I get the prime numbers that are part of the factorization of 8976... – Ohad May 08 '13 at 14:27
  • That should work, I was thinking about it a different way. Do what ever you think makes the most sense. – John Kane May 08 '13 at 14:30
  • Well it's a Java course.. so threads is only one part of the course.. we spoke about synchronization and race conditions, deadlock livelock etc.. we have really used only the basic methods like join(),run(),start(),wait(), and synchronization.. not all what you all talk about ... – Ohad May 08 '13 at 14:31