is there any solution to this problem? why it returns 541 instead of 3.
public class Test {
public static void main(String[] args) {
double a = Math.pow(3, 561);
// it returns 541 instead of 3
System.out.println(a % 561);
}
}
According to the Fermat's little theorem:
Math.pow(a, p) % p == a % p
and so:
Math.pow(3, 561) % 561 = 3 % 561 = 3
Therefore, you don't need to do this heavy calculations. Just maths.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With