Big number in C++

我与影子孤独终老i 提交于 2019-11-30 07:29:13

问题


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


回答1:


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;
}



回答2:


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;



回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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/




回答9:


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.



来源:https://stackoverflow.com/questions/238343/big-number-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!