优化 digit <= 2 算法(类似于 Project Euler 303)

Mic*_*lle 5 java algorithm optimization

我正在创建一个程序来解决Project Euler Problem 303 我的查找 f(n)方法几乎不缺少蛮力:

static BigInteger findFn(int n){
    Long Fn = new Long(n); 
    String test = Fn.toString();
    Long multiplier = new Long("1"); 
    long counter = 0;
    boolean done = false; 
    BigInteger fn = new BigInteger("0");
    while(!done){
        counter = 0; 
        BigInteger tempOne = new BigInteger(multiplier.toString());
        BigInteger tempTwo = new BigInteger(Fn.toString());
        BigInteger product = tempOne.multiply(tempTwo);
        test = product.toString();
        for(int i = 0; i < test.toString().length(); i++){
            if(Character.getNumericValue(test.toString().charAt(i)) <= 2){
                counter++;
            }else{
                             //Is it better to set done = true here as opposed to breaking?
                break; //breaks if it encounters any number in the multiple >2. 
            }

        }

        if(counter == test.length()){
            fn = product;
            done = true;
            break;              
        }
        multiplier++;
    }
    return fn;
}
Run Code Online (Sandbox Code Playgroud)

它适用于大多数数字,但有一些(通常以 9 结尾的数字)会卡住。我认为 BigIntegers 会减慢它的速度,所以首先,有没有我在不需要的地方使用 BigInteger 的地方?其次,必须有一种替代方法或某种其他技巧来减少我没有想到的循环次数。有什么想法可以让我朝着正确的方向前进吗?

谢谢!!