Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I have to compute the value of expression: pow(x, y) % n (remainder of pow(x,y) divided by n)
I've tried like this:
Java
long L1 = ((long)Math.pow(123,17)) % 3233;
long L2 = ((long)Math.pow(L1,2753)) % 3233;
System.out.format("L1 = %d%n", L1);
System.out.format("L2 = %d%n", L2);

and I haven't got what I've expected.
I think the problem is that we're dealing with too big numbers.

How can I compute these thing?

Thanks in advance!
Posted

1 solution

Use the BigInteger[^] class.
For instance:
Java
// implementation of 'long L1 = ((long)Math.pow(123,17)) % 3233;'
BigInteger b = new BigInteger("123");
BigInteger e = new BigInteger("17");
BigInteger m = new BigInteger("3233");
BigInteger r;
r = b.modPow(e, m);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900