GMP 任意精度运算
我正在使用 GMP 库制作一个 Pi 程序,它将计算大约 7 万亿位的 Pi。问题是,我无法弄清楚需要多少位来保存那么多小数位。
I'm using the GMP library to make a Pi program, that will calculate about 7 trillion digits of Pi. Problem is, I can't figure out how many bits are needed to hold that many decimal places.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
7 万亿位数字可以表示 10^(7 万亿) 个不同数字中的任何一个。
x 位可以表示 2^x 个不同的数字。
所以你想解决:
取两边的 log-base-2:
回想一下
log(a^b)
=b * log(a)
:我得到 <代码>23253496664212位。为了安全起见,我会再添加一两个。不过,祝你好运,找到 PB 来容纳它们。
我怀疑您将需要一个更有趣的算法。
7 trillion digits can represent any of 10^(7 trillion) distinct numbers.
x bits can represent 2^x distinct numbers.
So you want to solve:
Take the log-base-2 of both sides:
Recall that
log(a^b)
=b * log(a)
:I get
23253496664212
bits. I would add one or two more just to be safe. Good luck finding the petabytes to hold them, though.I suspect you are going to need a more interesting algorithm.
我想纠正一下回复中所写的一件事:
它是相反的:
I wanna just correct one thing about what was written in the response answer:
well it is the opposite :
2^10 = 1024,因此十位将代表略多于三位的数字。既然你谈论的是 7 万亿位数字,那么这大约是 23 万亿位,或者大约 3 TB,这比我上次访问 Costco 的一个驱动器所能获得的还要多。
你可能变得过于雄心勃勃。我想知道每次操作读取和写入整个磁盘的 I/O 时间。
(解决这个问题的数学方法是使用对数,因为需要 7 万亿位数字来表示的数字的对数以 10 为底,约为 7 万亿。找到该数字在现有底数中的对数,转换底数,然后就可以了。对于基数 2 和基数 10 之间的简写,请使用十位 == 三位数,因为它说 2 的对数基数 10 是 0.3,但实际上更像是。 .301。)
2^10 = 1024, so ten bits will represent slightly more than three digits. Since you're talking about 7 trillion digits, that would be something like 23 trillion bits, or about 3 terabytes, which is more than I could get on one drive from Costco last I visited.
You may be getting overambitious. I'd wonder about the I/O time to read and write entire disks for each operation.
(The mathematical way to solve it is to use logarithms, since a number that takes 7 trillion digits to represent has a log base 10 of about 7 trillion. Find the log of the number in the existing base, convert the base, and you've got your answer. For shorthand between base 2 and base 10, use ten bits==three digits, because that's not very far wrong. It says that the log base 10 of 2 is .3, when it's actually more like .301.)