PHP 溢出模数

发布于 2024-12-25 17:18:20 字数 314 浏览 2 评论 0原文

我正在编写一个使用连续平方来求解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2025-01-01 17:18:20

我建议查看 GMP 扩展 并阅读 这个问题

I would suggest checking out the GMP extension and reading this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文