C++ 中的大数

发布于 2024-07-08 02:53:10 字数 1141 浏览 10 评论 0原文

我试图在 C++ 变量中放置一个大数字。 数字是 600851475143

我尝试了 unsigned long long int 但收到错误消息,指出常量太大。 然后我尝试了一个名为 BigInt -> 的 bigInt 库 http://mattmccutchen.net/bigint/

问题是我无法编译我得到的代码关于 lib 的许多错误。

对 `BigInteger::BigInteger(int)' 的未定义引用 <-- 很多这样的。

到目前为止,这是我的代码:

#include "string"
#include "iostream"       
#include "bigint/NumberlikeArray.hh"
#include "bigint/BigUnsigned.hh"
#include "bigint/BigInteger.hh"
#include "bigint/BigIntegerAlgorithms.hh"
#include "bigint/BigUnsignedInABase.hh"
#include "bigint/BigIntegerUtils.hh"
using namespace std;

int main() {

    //unsigned long int num = 13195;
    //unsigned long long int num = 600851475143;
    BigInteger num = 13195;
    int divider = 2;

    //num = 600851475143;

    while (1) {
        if ((num % divider) == 0) {
            cout << divider << '\n';
            num /= divider;
        }
        else
            divider++;

        if (num == 1)
            break;
    }
}

如果我输入较小的数字并且不使用 BigInt 库,则该程序运行良好。 任何帮助将不胜感激:D

I am trying to place a big number in a C++ variable. The number is 600851475143

I tried unsigned long long int but got an error saying it the constant was too big.
I then tried a bigInt library called BigInt -> http://mattmccutchen.net/bigint/

The problem is I can't compile the code as I get many errors regarding the lib.

undefined reference to `BigInteger::BigInteger(int)' <-- lot's of these.

Here is my code so far:

#include "string"
#include "iostream"       
#include "bigint/NumberlikeArray.hh"
#include "bigint/BigUnsigned.hh"
#include "bigint/BigInteger.hh"
#include "bigint/BigIntegerAlgorithms.hh"
#include "bigint/BigUnsignedInABase.hh"
#include "bigint/BigIntegerUtils.hh"
using namespace std;

int main() {

    //unsigned long int num = 13195;
    //unsigned long long int num = 600851475143;
    BigInteger num = 13195;
    int divider = 2;

    //num = 600851475143;

    while (1) {
        if ((num % divider) == 0) {
            cout << divider << '\n';
            num /= divider;
        }
        else
            divider++;

        if (num == 1)
            break;
    }
}

If I put a smaller number and don't use the BigInt lib this program runs fine.
Any help will be appreciated :D

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

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

发布评论

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

评论(9

心奴独伤 2024-07-15 02:53:10

您可以通过后缀 L 指定一个整数字面量。
您可以通过后缀 LL 指定 long long 整数文字。

#include <iostream>

int main()
{
    long long num = 600851475143LL;

    std::cout << num;
}

You can specify an integer literal as long by the suffix L.
You can specify an integer literal as long long by the suffix LL.

#include <iostream>

int main()
{
    long long num = 600851475143LL;

    std::cout << num;
}
听不够的曲调 2024-07-15 02:53:10

数字 600851475143 对于 long long int 来说并不算太大,但在使用 long long 常量时需要使用 LL 后缀(ULL 表示 unsigned long long int):

unsigned long long int num = 600851475143ULL;

The number is 600851475143 isn't too large for a long long int but you need to use the LL suffix when using a long long constants (ULL for unsigned long long int):

unsigned long long int num = 600851475143ULL;
独守阴晴ぅ圆缺 2024-07-15 02:53:10

大整数库存在的理由是表示您的语言本身无法处理的整数。 这意味着,您甚至无法将其写成文字。 也许该库有一种将字符串解析为大数字的方法。

Raison d'etre of a big integer library is to represent integers which your language cannot handle natively. That means, you cannot even write it down as a literal. Probably, that library has a way to parse a string as a big number.

明月夜 2024-07-15 02:53:10

在更一般的情况下,当您无法将您的号码放入很长的长度,并且可以使用 GNU LGPL 许可证 (http://www.gnu.org/copyleft/lesser.html),我建议尝试 GNU 多精度库(http://gmplib.org/)。

它速度非常快,用 C 语言编写,并附带一个非常酷的 C++ 包装库。

In a more general case when you cannot fit your number in a long long, and can live with the GNU LGPL license (http://www.gnu.org/copyleft/lesser.html), I would suggest trying the GNU Multiprecision Library (http://gmplib.org/).

It is extremely fast, written in C and comes with a very cool C++-wrapper-library.

云醉月微眠 2024-07-15 02:53:10

是否有要链接的 bigint lib 或要编译的 bigint.cpp?

Is there a bigint lib to link in or a bigint.cpp to compile?

故事灯 2024-07-15 02:53:10

如果您收到 bignum 库的未定义引用错误,则您可能没有链接它。 在 Unix 上,您必须传递像 -lbigint 这样的选项。 如果您使用 IDE,则必须找到链接器设置并添加库。

至于数字,正如已经说过的,自然常量默认为 int 类型。 您必须使用 LL/ll 才能获得 long long。

If you are getting undefined reference errors for the bignum library, you probably didn't link it. On Unix, you will have to pass an option like -lbigint. If you are using an IDE, you will have to find the linker settings and add the library.

As for the numbers, as has already been said, a natural constant defaults to int type. You must use LL/ll to get a long long.

野心澎湃 2024-07-15 02:53:10

在这种情况下,要做的第一件事是找出 unsigned long long 可以容纳的最大数字是多少。 由于它是64位的,所以最大的数字是2^64-1 = 18446744073709551615,它比你的数字大。 然后你知道你做错了什么,然后你查看 Martin York 的答案,看看如何解决它。

The first thing to do in this case is to figure out what is the largest number that you can fit into an unsigned long long. Since it is 64 bit, the largest number would be 2^64-1 = 18446744073709551615, which is larger than your number. Then you know that you are doing something wrong, and you look at the answer by Martin York to see how to fix it.

半城柳色半声笛 2024-07-15 02:53:10

试试这个。
基本上你可以拥有自己的自定义类,它使用链表来存储无限大小的数字。 (内存是限制)
试试这个
https://mattmccutchen.net/bigint/

Try this one.
Basically you can have your own custom class which uses linked list to store the number of infinite size. ( RAM is the restriction )
Try this one
https://mattmccutchen.net/bigint/

☆獨立☆ 2024-07-15 02:53:10

对于在这个问题提出五年后对这个库有问题的其他人来说,这就是你的答案。
你不能仅仅编译你的程序,它将无法链接并出现丑陋的难以理解的错误!
该库是 c++ 文件的集合,您应该将其编译为 .o 文件并链接。 如果您查看示例程序附带的 make 文件的输出,您将看到以下内容:

g++ -c -O2 -Wall -Wextra -pedantic BigUnsigned.cc
g++ -c -O2 -Wall -Wextra -pedantic BigInteger.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerAlgorithms.cc
g++ -c -O2 -Wall -Wextra -pedantic BigUnsignedInABase.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerUtils.cc
g++ -c -O2 -Wall -Wextra -pedantic sample.cc
g++ sample.o BigUnsigned.o BigInteger.o BigIntegerAlgorithms.o BigUnsignedInABase.o BigIntegerUtils.o -o sample

sample 替换为您的程序名称,将这些行粘贴到 makefile 或脚本中,然后就可以开始了。

For anyone else having problems with this library five years after this question was asked, this is the answer for you.
You cannot just compile your program, it will fail to link with an ugly impenetrable error!
This library is a collection of c++ files which you are supposed to compile to .o files and link against. If you look at the output of the make file provided with the sample program you will see this:

g++ -c -O2 -Wall -Wextra -pedantic BigUnsigned.cc
g++ -c -O2 -Wall -Wextra -pedantic BigInteger.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerAlgorithms.cc
g++ -c -O2 -Wall -Wextra -pedantic BigUnsignedInABase.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerUtils.cc
g++ -c -O2 -Wall -Wextra -pedantic sample.cc
g++ sample.o BigUnsigned.o BigInteger.o BigIntegerAlgorithms.o BigUnsignedInABase.o BigIntegerUtils.o -o sample

Replace sample with the name of your program, paste these lines in a makefile or script, and away you go.

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