Finding modulo of large numbers squared

ε祈祈猫儿з 提交于 2019-12-25 03:13:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!