在 C 中实现 bigint 的最简单方法是什么?
我正在尝试计算100! (即 100 的阶乘)。
我正在寻找使用 C 来完成此任务的最简单方法。我已经阅读过但尚未找到具体的答案。
如果你一定知道的话,我在 Mac os X 中使用 Xcode 进行编程。
I am trying to calculate 100! (that is, the factorial of 100).
I am looking for the simplest way to accomplish this using C. I have read around but have not found a concrete answer.
If you must know, I program in Xcode in Mac os X.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找一个简单的库,libtommath(来自 libtomcrypt)可能就是您想要的。
如果您想自己编写一个简单的实现(无论是作为学习练习,还是因为您只需要 bigint 功能的一个非常有限的子集,并且不想依赖于大型库、命名空间污染等) ,那么我可能会针对您的问题提出以下建议:
由于您可以根据
n
限制结果的大小,因此只需预先分配所需大小的uint32_t
数组保持结果。我猜您会想要打印结果,因此使用 10 的幂(即以 1000000000 为底)而不是 2 的幂是有意义的。也就是说,数组的每个元素都可以保存 0 到 999999999 之间的值。要将此数字乘以(正常、非大)整数
n
,请执行以下操作:如果您知道
n
永远不会大于 100(或其他一些小数字),并且希望避免进入 64 位范围(或者如果您在 64 位平台上并且想要使用uint64_t for your bigint array),然后将基数设置为 10 的较小幂,以便乘法结果始终适合该类型。
现在,打印结果就像这样:
如果您想使用 2 的幂作为基数,而不是 10 的幂,乘法会变得更快:
但是,以十进制打印结果就不那么令人愉快了。 :-) 当然,如果您想要十六进制结果,那么很简单:
希望这有帮助!我将把实现其他事情(例如加法、2 个 bigint 的乘法等)作为练习留给您。回想一下你在小学时如何学习以 10 为基数的加法、乘法、除法等,并教计算机如何做到这一点(但以基数 10^9 或基数 2^32 代替),你应该没问题。
If you're looking for a simple library, libtommath (from libtomcrypt) is probably what you want.
If you're looking to write a simple implementation yourself (either as a learning exercise or because you only need a very limited subset of bigint functionality and don't want to tack on a dependency to a large library, namespace pollution, etc.), then I might suggest the following for your problem:
Since you can bound the size of the result based on
n
, simply pre-allocate an array ofuint32_t
of the required size to hold the result. I'm guessing you'll want to print the result, so it makes sense to use a base that's a power of 10 (i.e. base 1000000000) rather than a power of 2. That is to say, each element of your array is allowed to hold a value between 0 and 999999999.To multiply this number by a (normal, non-big) integer
n
, do something like:If you know
n
will never be bigger than 100 (or some other small number) and want to avoid going into the 64-bit range (or if you're on a 64-bit platform and want to useuint64_t
for your bigint array), then make the base a smaller power of 10 so that the multiplication result will always fit in the type.Now, printing the result is just something like:
If you want to use a power of 2 as the base, rather than a power of 10, the multiplication becomes much faster:
However, printing your result in decimal will not be so pleasant... :-) Of course if you want the result in hex, then it's easy:
Hope this helps! I'll leave implementing other things (like addition, multiplication of 2 bigints, etc) as an exercise for you. Just think back to how you learned to do base-10 addition, multiplication, division, etc. in grade school and teach the computer how to do that (but in base-10^9 or base-2^32 instead) and you should have no problem.
如果您愿意使用库实现,标准库实现似乎是 GMP
应该计算 100!通过查看文档。
If you're willing to use a library implementation the standard one seems to be GMP
should calculate 100! from looking at the docs.
您要求使用最简单的方法来执行此操作。所以,你开始吧:
我测试了这段代码,它给出了正确的答案。
You asked for the simplest way to do this. So, here you go:
I tested this code and it gives the correct answer.
您还可以使用 OpenSSL bn;它已经安装在 Mac OS X 中。
You can also use OpenSSL bn; it is already installed in Mac OS X.
您只需 30 行代码即可在 C 中打印阶乘 1000,
和 char 类型:您会发现速度更快,但“小”
Factorial(10000)
在这里立即计算出来。您可以将其放入
fact.c
文件中,然后编译+执行:如果您想执行一些基本转换,可以使用 解决方案,另请参阅斐波那契(10000),谢谢。
You can print factorial 1000 in C with just 30 lines of code,
<stdio.h>
and char type :You will find faster but the “little”
factorial(10000)
is here computed ≈ instantly.You can put it into a
fact.c
file then compile + execute :If you want to execute some base conversion there is a solution, see also Fibonacci(10000), Thank You.