简体   繁体   中英

How can I calculate modulo of a large number in String format in java

I am trying to calculate an expression in java which is in String ie 99999999999999999^99999999999999999 I want to calculate this number modulo 1000000007 . I currently trying to store the big numbers as double but taking modulo with double gives me NaN . Can somebody help ?

You can use BigInteger and modPow(BigInteger, BigInteger) like

BigInteger m = new BigInteger("1000000007");
BigInteger a = new BigInteger("99999999999999999");
BigInteger b = new BigInteger("99999999999999999");
BigInteger answer = a.modPow(b, m);
System.out.println(answer);

which gives

265859324

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM