在 C 中实现 bigint 的最简单方法是什么?

发布于 2024-09-11 16:50:13 字数 124 浏览 7 评论 0原文

我正在尝试计算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 技术交流群。

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

发布评论

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

评论(5

挽容 2024-09-18 16:50:13

如果您正在寻找一个简单的库,libtommath(来自 libtomcrypt)可能就是您想要的。

如果您想自己编写一个简单的实现(无论是作为学习练习,还是因为您只需要 bigint 功能的一个非常有限的子集,并且不想依赖于大型库、命名空间污染等) ,那么我可能会针对您的问题提出以下建议:

由于您可以根据 n 限制结果的大小,因此只需预先分配所需大小的 uint32_t 数组保持结果。我猜您会想要打印结果,因此使用 10 的幂(即以 1000000000 为底)而不是 2 的幂是有意义的。也就是说,数组的每个元素都可以保存 0 到 999999999 之间的值。

要将此数字乘以(正常、非大)整数 n,请执行以下操作:

uint32_t carry=0;
for(i=0; i<len; i++) {
    uint64_t tmp = n*(uint64_t)big[i] + carry;
    big[i] = tmp % 1000000000;
    carry = tmp / 1000000000;
}
if (carry) big[len++] = carry;

如果您知道 n 永远不会大于 100(或其他一些小数字),并且希望避免进入 64 位范围(或者如果您在 64 位平台上并且想要使用 uint64_t for your bigint array),然后将基数设置为 10 的较小幂,以便乘法结果始终适合该类型。

现在,打印结果就像这样:

printf("%lu", (long)big[len-1]);
for(i=len-1; i; i--) printf("%.9lu", (long)big[i-1]);
putchar('\n');

如果您想使用 2 的幂作为基数,而不是 10 的幂,乘法会变得更快:

uint32_t carry=0;
for(i=0; i<len; i++) {
    uint64_t tmp = n*(uint64_t)big[i] + carry;
    big[i] = tmp;
    carry = tmp >> 32;
}
if (carry) big[len++] = carry;

但是,以十进制打印结果就不那么令人愉快了。 :-) 当然,如果您想要十六进制结果,那么很简单:

printf("%lx", (long)big[len-1]);
for(i=len-1; i; i--) printf("%.8lx", (long)big[i-1]);
putchar('\n');

希望这有帮助!我将把实现其他事情(例如加法、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 of uint32_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:

uint32_t carry=0;
for(i=0; i<len; i++) {
    uint64_t tmp = n*(uint64_t)big[i] + carry;
    big[i] = tmp % 1000000000;
    carry = tmp / 1000000000;
}
if (carry) big[len++] = carry;

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 use uint64_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:

printf("%lu", (long)big[len-1]);
for(i=len-1; i; i--) printf("%.9lu", (long)big[i-1]);
putchar('\n');

If you want to use a power of 2 as the base, rather than a power of 10, the multiplication becomes much faster:

uint32_t carry=0;
for(i=0; i<len; i++) {
    uint64_t tmp = n*(uint64_t)big[i] + carry;
    big[i] = tmp;
    carry = tmp >> 32;
}
if (carry) big[len++] = carry;

However, printing your result in decimal will not be so pleasant... :-) Of course if you want the result in hex, then it's easy:

printf("%lx", (long)big[len-1]);
for(i=len-1; i; i--) printf("%.8lx", (long)big[i-1]);
putchar('\n');

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.

獨角戲 2024-09-18 16:50:13

如果您愿意使用库实现,标准库实现似乎是 GMP

mpz_t out;
mpz_init(out);
mpz_fac_ui(out,100);
mpz_out_str(stdout,10,out);

应该计算 100!通过查看文档。

If you're willing to use a library implementation the standard one seems to be GMP

mpz_t out;
mpz_init(out);
mpz_fac_ui(out,100);
mpz_out_str(stdout,10,out);

should calculate 100! from looking at the docs.

吖咩 2024-09-18 16:50:13

您要求使用最简单的方法来执行此操作。所以,你开始吧:

#include <gmp.h>
#include <stdio.h>

int main(int argc, char** argv) {
    mpz_t mynum;
    mpz_init(mynum);
    mpz_add_ui(mynum, 100);
    int i;
    for (i = 99; i > 1; i--) {
        mpz_mul_si(mynum, mynum, (long)i);
    }
    mpz_out_str(stdout, 10, mynum);
    return 0;
}

我测试了这段代码,它给出了正确的答案。

You asked for the simplest way to do this. So, here you go:

#include <gmp.h>
#include <stdio.h>

int main(int argc, char** argv) {
    mpz_t mynum;
    mpz_init(mynum);
    mpz_add_ui(mynum, 100);
    int i;
    for (i = 99; i > 1; i--) {
        mpz_mul_si(mynum, mynum, (long)i);
    }
    mpz_out_str(stdout, 10, mynum);
    return 0;
}

I tested this code and it gives the correct answer.

若言繁花未落 2024-09-18 16:50:13

您还可以使用 OpenSSL bn;它已经安装在 Mac OS X 中。

You can also use OpenSSL bn; it is already installed in Mac OS X.

淡淡離愁欲言轉身 2024-09-18 16:50:13

您只需 30 行代码即可在 C 中打印阶乘 1000,char 类型:

#include <stdio.h>
#define B_SIZE 3000 // number of buffered digits

struct buffer {
    size_t index;
    char data[B_SIZE];
};

void init_buffer(struct buffer *buffer, int n) {
    for (buffer->index = B_SIZE; n; buffer->data[--buffer->index] = (char) (n % 10), n /= 10);
}

void print_buffer(const struct buffer *buffer) {
    for (size_t i = buffer->index; i < B_SIZE; ++i) putchar('0' + buffer->data[i]);
}

void natural_mul_buffer(struct buffer *buffer, const int n) {
    int a, b = 0;
    for (size_t i = (B_SIZE - 1); i >= buffer->index; --i) {
        a = n * buffer->data[i] + b;
        buffer->data[i] = (char) (a % 10);
        b = a / 10;
    }
    for (; b; buffer->data[--buffer->index] = (char) (b % 10), b /= 10);
}

int main() {
    struct buffer number_1 = {0};
    init_buffer(&number_1, 1);
    for (int i = 2; i <= 100; ++i)
        natural_mul_buffer(&number_1, i);
    print_buffer(&number_1);
}

您会发现速度更快,但“小”Factorial(10000) 在这里立即计算出来。

您可以将其放入 fact.c 文件中,然后编译+执行:

gcc -O3 -std=c99 -Wall -pedantic fact.c ; ./a.out ;

如果您想执行一些基本转换,可以使用 解决方案,另请参阅斐波那契(10000),谢谢。

You can print factorial 1000 in C with just 30 lines of code, <stdio.h> and char type :

#include <stdio.h>
#define B_SIZE 3000 // number of buffered digits

struct buffer {
    size_t index;
    char data[B_SIZE];
};

void init_buffer(struct buffer *buffer, int n) {
    for (buffer->index = B_SIZE; n; buffer->data[--buffer->index] = (char) (n % 10), n /= 10);
}

void print_buffer(const struct buffer *buffer) {
    for (size_t i = buffer->index; i < B_SIZE; ++i) putchar('0' + buffer->data[i]);
}

void natural_mul_buffer(struct buffer *buffer, const int n) {
    int a, b = 0;
    for (size_t i = (B_SIZE - 1); i >= buffer->index; --i) {
        a = n * buffer->data[i] + b;
        buffer->data[i] = (char) (a % 10);
        b = a / 10;
    }
    for (; b; buffer->data[--buffer->index] = (char) (b % 10), b /= 10);
}

int main() {
    struct buffer number_1 = {0};
    init_buffer(&number_1, 1);
    for (int i = 2; i <= 100; ++i)
        natural_mul_buffer(&number_1, i);
    print_buffer(&number_1);
}

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 :

gcc -O3 -std=c99 -Wall -pedantic fact.c ; ./a.out ;

If you want to execute some base conversion there is a solution, see also Fibonacci(10000), Thank You.

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