PHP 溢出模数
我正在编写一个使用连续平方来求解 a^k mod m 的算法。由于连续平方的工作方式,算法必须计算的最大数字是 2147483646^2(我将用户输入限制为 214738364)。不幸的是,它仍然需要计算这个。它似乎得到了平方部分,然后将溢出的数字转换为浮点数,但无法计算浮点数和整数的模。
示例行是:
3422422^2 mod 715924 = 661224^2 mod 715924 = 437217178176 mod 715924 = -354280
我怎样才能解决这个问题,以及如何找到解决 PHP 中整数溢出的方法?
I am writing an algorithm that uses successive squaring to solve a^k mod m. Because of the way successive squaring works, the maximum number that the algorithm will ever have to compute is 2147483646^2 (I limited the user input to 214738364). Unfortunately, it still has to compute this. It seems to get the squaring part right, and then turns the overflowing number into a float, but then can't compute the modulus of a float and an integer.
A sample line is:
3422422^2 mod 715924 = 661224^2 mod 715924 = 437217178176 mod 715924 = -354280
How can I fix this, and how does one find one's way around integer overflow in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议查看 GMP 扩展 并阅读 这个问题。
I would suggest checking out the GMP extension and reading this question.