问题
I am currently writing a program that requires the calculation of:
(x^2) mod y
Both these numbers are integers. Both can be large numbers, though they never exceed 10^9.
That is still enough to overflow integer for x squared. Speed is crucial for this code so gradual multiplication is not usable.
Thank you.
回答1:
You can take a reference from this post
This should be a relatively easy example. 4^23=2^46. Now, since 2^5=32≡1(mod31),
2^46=(2^5)9×2=32^9×2≡1×2≡2(mod31)
回答2:
The solution was really simple. I used Long, instead of int and it works.
来源:https://stackoverflow.com/questions/22493409/finding-modulo-of-large-numbers-squared