需要帮助来找到分解代码中的错误

时间:2013-07-25 21:13:11

标签: java debugging biginteger factorization

我正在编写一个程序来计算一个数字因素是主要因素。该计划的工作原理如下:

1)输入您想要考虑的数字(我将引用为“inputNumber”)

2)测试你是否可以将inputNumber除以2,3,5和7(前4个素数)。如果你可以除以这些中的任何一个,那么你可以多次这样做(即12可以除以2两次,之后,剩下的3) note 每当我除以一个数字时,我将该数字存储在一个数组列表中,并保留其余部分以供进一步测试

3)从while循环中的i = 11(下一个素数)开始,我执行以下操作:

while (i < remainder+1) { 
divides by i? yes: store i, 
repeat until you cant divide by i. i=i+2, 
divides by i? yes: store i, repeat until you can't divide by i.
 i=i+4, same as before... i=i+2... 
and finally stop the loop at i=i+2 
} 

这样,while循环的每次成功迭代都会将余数除以1,3,7,9之后的数字。我们不需要测试偶数,因为我们已经除以2,也不需要测试以5结尾的数字,因为我们已经除以5.最后,我们

这是一个非常简洁的算法,因为它比通过测试一个数字来反映数字要快得多,事实上,你只需要测试所有数字的40%。

我的问题是:当我尝试分解84738329279时,一个随机选取的数字,它省略了将最后一个素数因子放在列表中,我无法弄明白这一点。出现的唯一因素是41,61和61.有人能帮助我找到我做错的事吗?这是我的代码:

import java.util.Scanner;
import java.math.BigInteger;

public class Test {

public static void main(String[] args) {
                    // create a scanner object for inputs
            Scanner in = new Scanner(System.in);

            // prompt user for number to factor
            System.out.print("enter a number to factor: ");
            String digits = in.next();
            BigInteger BigDigits = new BigInteger(digits);

            BigInteger[] results = factor.factorThis(BigDigits);


            System.out.print("Factors are ");
            for (int i=0; i<results.length;i++){
            System.out.print(results[i] + " ");
            }
            System.out.println("");
        }
}

import java.util.ArrayList;
import java.math.BigInteger;


public class factor {

// Returns the prime factors of the BigInteger number
public static BigInteger[] factorThis(BigInteger number) {

    BigInteger i = new BigInteger("11");
    ArrayList<BigInteger> divisors = new ArrayList<BigInteger>(0);

    BigInteger[] firstPrimes = new BigInteger[4];
    firstPrimes[0] = new BigInteger("2");
    firstPrimes[1] = new BigInteger("3");
    firstPrimes[2] = new BigInteger("5");
    firstPrimes[3] = new BigInteger("7");

    // loop that test for first 4 prime numbers
    for (int l=0;l<4;l++){
        while ((number.mod(firstPrimes[l])).compareTo(BigInteger.ZERO) == 0) {
            number = number.divide(firstPrimes[l]);
            divisors.add(firstPrimes[l]);
        }
    }


    // loop that factors only numbers finishing by 1,3,7,9
    while (i.compareTo(number) == -1){

        // check for ending by 1
        if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
            while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
                number = number.divide(i);
                divisors.add(i);
            }
        }
        else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
            i=i.add(firstPrimes[0]);
        }

        // check for ending by 3
        if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
            while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
                number = number.divide(i);
                divisors.add(i);
            }
        }
        else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
            i=i.add(firstPrimes[0].multiply(firstPrimes[0]));
        }

        //check for ending by 7
        if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
            while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
                number = number.divide(i);
                divisors.add(i);
            }
        }
        else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
            i=i.add(firstPrimes[0]);
        }

        // check for ending by 9
        if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
            while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
                number = number.divide(i);
                divisors.add(i);
            }
        }
        else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
            i=i.add(firstPrimes[0]);
        }
    }

    // store prime factors into a BigInt array
    String[] strArrayDivisors = divisors.toString().replaceAll("\\[", "").replaceAll("\\]","").replaceAll("\\s","").split(",");

    BigInteger[] BigIntDivisors = new BigInteger[strArrayDivisors.length];

    for(int j=0;j<strArrayDivisors.length;j++){
        BigIntDivisors[j] = new BigInteger(strArrayDivisors[j]);
    }

    // returns all factors of "number"
    return BigIntDivisors;

}   
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

首先,84738329279 = 41 * 61 * 61 * 555439。你的41,61和61是正确的。

但是当你的算法终止时,你的最后一个素数仍然在number。您需要在最后添加测试number的代码:如果它是1,那么您已经完成了,否则需要将其添加到divisors以便稍后打印