首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java分解算法BigInteger不工作

Java分解算法BigInteger不工作
EN

Stack Overflow用户
提问于 2015-01-27 16:19:37
回答 1查看 692关注 0票数 0

我正在用BigInteger实现费马分解算法,这样我就可以考虑因素了。但目前,代码不起作用;它因某种原因而挂起。有人能告诉我问题在哪里,或者让我知道我的算法是否不正确?BigInteger使我的生活变得困难,所以我不得不寻找平方根方法。

代码语言:javascript
复制
import java.math.BigInteger;
import java.util.Scanner;

public class Fermat
{
    /** Fermat factor **/
    public void FermatFactor(BigInteger N)
    {
        BigInteger a = sqrt(N);
        BigInteger b2 = a.multiply(a).subtract(N);

        while (!isSquare(b2)) {
            a = a.add(a);
            b2 = a.multiply(a).subtract(N);
        }

        BigInteger r1 = a.subtract(sqrt(b2));
        BigInteger r2 = N.divide(r1);
        display(r1, r2);
    }

    /** function to display roots **/
    public void display(BigInteger r1, BigInteger r2) {
        System.out.println("\nRoots = "+ r1 +" , "+ r2);    
    }

    /** function to check if N is a perfect square or not **/
    public boolean isSquare(BigInteger N) {
        BigInteger ONE = new BigInteger("1");
        BigInteger sqr = sqrt(N);

        if (sqr.multiply(sqr) == N  || (sqr.add(ONE)).multiply(sqr.add(ONE)) == N)
            return true;
        return false;
    }


    public static BigInteger sqrt(BigInteger x)
            throws IllegalArgumentException {
        if (x.compareTo(BigInteger.ZERO) < 0) {
            throw new IllegalArgumentException("Negative argument.");
        }
        // square roots of 0 and 1 are trivial and
        // y == 0 will cause a divide-by-zero exception
        if (x == BigInteger.ZERO || x == BigInteger.ONE) {
            return x;
        } // end if
        BigInteger two = BigInteger.valueOf(2L);
        BigInteger y;
        // starting with y = x / 2 avoids magnitude issues with x squared
        for (y = x.divide(two);
                y.compareTo(x.divide(y)) > 0;
                y = ((x.divide(y)).add(y)).divide(two));
        if (x.compareTo(y.multiply(y)) == 0) {
            return y;
        } else {
            return y.add(BigInteger.ONE);
        }
    } // end bigIntSqRootCeil


    /** main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Fermat Factorization Test\n");
        System.out.println("Enter odd number");
        BigInteger N = scan.nextBigInteger();
        Fermat ff = new Fermat();
        ff.FermatFactor(N);
        scan.close();
    }
}

我知道我有很多错误,但是任何帮助都是值得感激的。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-27 16:37:46

你的"for“循环:

代码语言:javascript
复制
for (y = x.divide(two);
    y.compareTo(x.divide(y)) > 0;
    y = ((x.divide(y)).add(y)).divide(two));

不会终止。也许您可以跟踪变量'y‘的值,以猜测何时必须停止。

编辑:这是错误的(见评论)。问题就在眼前

代码语言:javascript
复制
a = a.add(a)

程序内部FermatFactor。应该是

代码语言:javascript
复制
a = a.add(ONE)

在我的机器上,我也遇到了使用'A == B‘测试均衡器的问题。方法'A.equals(B)‘固定它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28175077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档