You are using a version without Ads of this website. Please, consider donating:

### Usage of Bignum Libraries

Hey, I have a factorial program, and I need to use Bignum libraries, but I can't figure them out. It would be awesome if somebody could help out. Here's my code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include using namespace std; int factor = 1; int write (int n) { ofstream fact; fact.open ("factorial.txt"); fact << n; fact.close(); return 0; } int main () { int factor = 1; int n; cout << "Enter a number to calculate its factorial value :\n"; cin >> n; while (n >1) { factor *= n--; } cout<< factor; write(factor); } ``````
I do not see any bignum libraries being used in that code...
Exactly. I need to know how to use the libraries. I just put that there in case you were curious.
Ok, so I found a library: https://mattmccutchen.net/bigint/
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include #include #include "BigIntegerLibrary.hh" using namespace std; double diffclock(clock_t clock1,clock_t clock2) { double diffticks=clock1-clock2; double diffms=(diffticks*1000)/CLOCKS_PER_SEC; return diffms; } int main (){ clock_t begin=clock(); int n; BigInteger factor = 1; cout << "Enter a number to calculate its factorial value :\n"; cin >> n; while (n >1) { factor *= n--; } cout<< factor<

But I get a bunch of "Undefined reference" errors.
Last edited on
Well it looks like it's a library error, I'll try a different one.
A library error, or an error in using the library?

I would expect there to be two parts to using a library:
1. include the appropriate header. The compiler will use this.
2. add the corresponding library file to the project. The linker will use this.

I'm guessing that you may have got linker errors because the file was either not added properly to the project, or the linker looked for it but not in the correct path.

I've actually just tried using the above library. The correct procedure would be to first build the library, and then use the pre-built version in actual projects.

However, for my quick test, I instead added all the files to my project.
That means I have `#include "BigIntegerLibrary.hh" ` in my source code.
I included all of these files as part of my project:
BigInteger.cc
BigIntegerAlgorithms.cc
BigIntegerUtils.cc
BigUnsigned.cc
BigUnsignedInABase.cc

So far I'm not very impressed. The library does seem to work, but takes forever to actually output the result. Maybe that's normal for this type of bignum.
Ok, does anyone have suggestions for libraries?
The GNU Bignum library.
Chervil wrote:
The library does seem to work, but takes forever to actually output the result.

I may have been a bit harsh in my comments here. It really depends on the type of problems for which the library is used.

For example, doing calculations with numbers of say 100 or 1000 digits is one thing. But when doing factorial calculations, the number of digits can rapidly grow very large.

As an example, 108654 factorial has half a million digits. Some libraries may calculate this in a moderate amount of time, but struggle with output of the result.

The reason for that is the internal operation may be binary, but the output is required in decimal format.

Anyway, one of my favourite libraries is Michael C. Ring's MAPM which calculates 108654! in about 9 seconds (on my old laptop). Output of the entire 500002 digit result (to a text file) took 30 milliseconds.

Some of the other libraries may be somewhat faster in the calculation, but could take minutes or perhaps hours to produce the output.

So - choice of library may depend on the goals.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: