处理大于最大小数值的数字

发布于 2024-10-04 02:29:17 字数 703 浏览 1 评论 0原文

我正在处理前 26 个素数的乘积。这需要超过 52 位的精度,我相信这是双精度数可以处理的最大值,并且超过了十进制可以提供的 28-29 位有效数字。那么对这么大的数字执行乘法和除法有哪些策略呢?

另外,为了实现这一目标,我必须克服哪些障碍,会对性能产生什么影响?

前 22 个素数的乘积(我可以在计算器上相乘而不进入科学模式的最大数)是:

10,642,978,845,819,148,849,204,664,294,430

最后四个素数的乘积是

72,370,439

当相乘时,我得到:

7.7023705133964511682328635583552e+38

性能影响在这里特别重要,因为我们本质上是试图解决质数字符串比较解决方案在实践中是否比直接比较字符更快的问题。促成此次调查的帖子位于此处。处理器针对浮点计算进行了优化;理想情况下,我希望在最终得到的任何解决方案中充分利用这种优化。

蒂亚!
James

PS:我拥有的代码是一个竞争解决方案;我不认为素数解决方案可能会更快,但我正在尽力给予它最公平的机会。

I'm working with the product of the first 26 prime numbers. This requires more than 52 bits of precision, which I believe is the max a double can handle, and more than the 28-29 significant digits a decimal can provide. So what would be some strategies for performing multiplication and division on numbers this large?

Also, what would the performance impacts be of whatever hoops I'd have to jump through to make this happen?

The product of the first 22 prime numbers (the most I can multiply together on my calculator without dropping into scientific mode) is:

10,642,978,845,819,148,849,204,664,294,430

The product of the last four is

72,370,439

When multiplied together, I get:

7.7023705133964511682328635583552e+38

The performance impacts are especially important here, because we're essentially trying to resolve the question of whether a prime-number string comparison solution is faster in practice than a straight comparison of characters. The post which prompted this investigation is here. Processors are optimized for floating-point calculations; ideally I'd want to leverage as much of that optimization in whatever solution I end up with.

TIA!
James

PS: The code I do have is for a competing solution; I don't think the prime number solution can possibly be faster, but I'm trying to give it the fairest chance I can.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

您可以在 C#4.0 中使用 BigInteger。对于旧版本,我认为您需要一个开源库,例如 这个

You can use BigInteger in C#4.0. For older versions, I think you need an open source library such as this one

爱,才寂寞 2024-10-11 02:29:17

我读了您链接到的关于面试问题的帖子。由于您仅对这些大整数进行乘法和除法,因此巨大优化就是将它们保持为质因数分解形式。每个大整数都是一个整数数组 [0..25],每个元素代表分解中第 n 个素数的指数。要以这种形式将两个大整数相乘,只需将指数逐个元素相加即可;除法、减指数。

但您会发现这相当于对两个字符串的字符计数进行制表。

I read the post you linked to, about the interview question. Since you're only multiplying and dividing these large integers, a huge optimization is to keep them in their prime-factorized form. Each large integer is an array [0..25] of ints, each element representing the exponent of the nth prime in the factorization. To multiply two large integers in this form, simply add the exponents element-by-element; to divide, subtract exponents.

But you will see this is equivalent to tabulating character counts on the two strings.

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