BigInteger modInverse不使用被检查为可逆的数字

时间:2012-02-28 09:34:59

标签: java math biginteger

我收到此错误消息:

Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible.

这很奇怪,因为我已经检查过它是否可以反转但是有一段时间了:

if(e.gcd(f)==BigInteger.valueOf(1)){d=e.modInverse(f);}

(我只使用modInverse一次,所以问题只能是这个。)我也尝试使用equals函数检查,相同的结果,并用BigInteger.ONE替换BigInteger.valueOf(1),也是相同的结果。

e和f都是相当大的数字。这可能是问题吗?如果没有,那是什么?

编辑:两个应该是好的数字(e和f都是随机生成的): e: 9621052046061456501366587335847490032034738260531416442599992125724770869143777724434621136148270408224358486480789065076015439260049732834961669339663651068040517049948746219457579643120163445760970644691744741533662899190172821721584052976577686282851438621400884199179254302505283244747995592596611537181094200162016550417633813815524000523611778694711681246885146830340987509832366125391293211772272830763010707464147876271519220158561249284055201778976275 f: 16676513155155711435633556290292399841994478533147079158165313450742666183857468374630705186073152798730185754009359158564262184760166850555421565829031677397531160732952407631566282672221888347405139275392725582249315145105384589417633027161798592285078352417743828277682057499510687432654973434263500652446355805121287249351524290685634309632867270787070026404872073959084720337580246072021126301925486445661096650037029829869513910200205317091132530162195304846449018937204755880662935929704531348715166585715335080615831412163500338513091079355521203276478413800219497108551811002174097217821125116752809771773184

1 个答案:

答案 0 :(得分:3)

你的号码不是彼此的主要原因:

public static void main(String... args) throws Exception {
    BigInteger e = new BigInteger("9621052046061456501366587335847490032034738260531416442599992125724770869143777724434621136148270408224358486480789065076015439260049732834961669339663651068040517049948746219457579643120163445760970644691744741533662899190172821721584052976577686282851438621400884199179254302505283244747995592596611537181094200162016550417633813815524000523611778694711681246885146830340987509832366125391293211772272830763010707464147876271519220158561249284055201778976275");
    BigInteger f =  new BigInteger("16676513155155711435633556290292399841994478533147079158165313450742666183857468374630705186073152798730185754009359158564262184760166850555421565829031677397531160732952407631566282672221888347405139275392725582249315145105384589417633027161798592285078352417743828277682057499510687432654973434263500652446355805121287249351524290685634309632867270787070026404872073959084720337580246072021126301925486445661096650037029829869513910200205317091132530162195304846449018937204755880662935929704531348715166585715335080615831412163500338513091079355521203276478413800219497108551811002174097217821125116752809771773184");
    System.out.println(e.gcd(f)); //prints 3
    BigInteger d;
    if (e.gcd(f).equals(BigInteger.ONE)) {
        d = e.modInverse(f);
        System.out.println(d);
    } else {
        System.out.println("nop");
    }
}

现在有了下面的数字就可以了:

public static void main(String... args) throws Exception {
    BigInteger e = new BigInteger("2");
    e = e.pow(2048);
    BigInteger f =  e.add(BigInteger.ONE);
    System.out.println(e.gcd(f)); //1
    BigInteger d;
    if (e.gcd(f).equals(BigInteger.ONE)) {
        d = e.modInverse(f);
        System.out.println(d);
    } else {
        System.out.println("nop");
    }
}
相关问题