Big number in C++

后端 未结 9 1516
暖寄归人
暖寄归人 2020-12-16 19: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.

相关标签:
9条回答
  • 2020-12-16 20:12

    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/

    0 讨论(0)
  • 2020-12-16 20:16

    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.

    0 讨论(0)
  • 2020-12-16 20:18

    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.

    0 讨论(0)
提交回复
热议问题