java - 并行素因数分解

标签 java

有人知道并行素因数分解算法的方法是什么吗?

我不知道应该在算法的哪个阶段将其划分为线程.. 我如何以并行方式思考质因数分解?

考虑以下一个线程代码:

    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;
    }

最佳答案

看来这对于 Fork/Join Framework 来说确实是一个很好的用途。 。看来您应该能够通过递归传递您找到的新因素来使用它。尝试看一下 RecursiveAction以及。在伪代码中,您应该能够执行如下操作:

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);
    }  
}

顺便说一句,如果您从中间 (num/2) 开始并从那里开始,而不是从 1 开始,它可能会有更好的性能。

关于java - 并行素因数分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442329/

相关文章:

java - BIRT 报告错误列绑定(bind)

java - JSF 2.0 使用请求 bean 动态添加和删除表行

java - 从文本字段获取信息以在其他类的文本区域中使用它

java - 命名字符串字段上的 Guice 注入(inject)

java - 无法启动服务 jboss.deployment.unit - StartException

java - JavaFX WebView 中的 Youtube

java - 谷歌地图准确性问题

java - SOAP href XML 映射问题 - 哪个映射有帮助?

java - 将连续的字符串数据拆分为所需的垂直输出

java - 如何检查矩阵是否是三对角矩阵