计算机如何评估大量数字? [英] How do computers evaluate huge numbers?
问题描述
如果我在Wolfram Alpha中输入一个值,例如1234567 ^ 98787878,它可以为我提供许多详细信息.这包括十进制近似值,总长度,最后一位数字等.您如何评估这么大的数字?据我了解,一种编程语言必须具有特殊的数据类型才能存储数字,更不用说将其添加到其他东西了.虽然我可以看到如何处理两个非常大的数字的加法运算,但我看不到如何计算出巨大的数字.
If I enter a value, for example 1234567^98787878 into Wolfram Alpha it can provide me with a number of details. This includes decimal approximation, total length, last digits etc. How do you evaluate such large numbers? As I understand it a programming language would have to have a special data type in order to store the number, let alone add it to something else. While I can see how one might approach the addition of two very large numbers, I can't see how huge numbers are evaluated.
10 ^ 2可以通过重复加法来计算.但是,如上面的示例这样的数字将需要一个巨大的循环.有人可以解释如何评估这么大的数字吗?另外,例如,有人如何创建自定义的大型数据类型来支持C#中的大量数字?
10^2 could be calculated through repeated addition. However a number such as the example above would require a gigantic loop. Could someone explain how such large numbers are evaluated? Also, how could someone create a custom large datatype to support large numbers in C# for example?
推荐答案
很简单,您可以自己完成
Well it's quite easy and you can have done it yourself
-
位数可以通过对数获得:
自A^B = 10 ^ (B * log(A, 10))
在这种情况下,我们可以计算(A = 1234567; B = 98787878)
we can compute (A = 1234567; B = 98787878)
in our case that
B * log(A, 10)
= 98787878 * log(1234567, 10)
= 601767807.4709646...
integer part + 1
(601767807 +1 = 601767808 )是位数
integer part + 1
(601767807 + 1 = 601767808) is the number of digits
第一个,例如,五个,个数字也可以通过对数获得; 现在我们应该分析
First, say, five, digits can be gotten via logarithm as well; now we should analyze fractional part of the
B * log(A, 10)
= 98787878 * log(1234567, 10)
= 601767807.4709646...
f = 0.4709646...
第一个数字为
10^f
(删除了小数点)= 29577 ...
first digits are
10^f
(decimal point removed) = 29577...
最后一个,例如,五个,个数字可以作为对应的剩余:
Last, say, five, digits can be obtained as a corresponding remainder:
最后五位数字= A^B rem 10^5
A rem 10^5
= 1234567 rem 10^5
= "34567
A rem 10^5
= 1234567 rem 10^5
= `34567
A ^ B rem 10 ^ 5 **=**
(((A rem 10 ^ 5)^ B)rem 10 ^ 5 **=**
(34567 ^ 98787878)rem 10 ^ 5 =
45009`
A^B rem 10^5**=**
((A rem 10^5)^B) rem 10^5**=**
(34567^98787878) rem 10^5=
45009`
最后五位数字为 45009
您可能会在这里发现BigInteger.ModPow
(C#)非常有用
You may find BigInteger.ModPow
(C#) very useful here
最后
1234567 ^ 98787878 = 29577 ... 45009(601767808位)
这篇关于计算机如何评估大量数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!