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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>

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/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#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<<endl<<endl;

ofstream fact;
  fact.open ("factorial.txt");
  fact << factor;
  fact.close();



clock_t end=clock();
	cout << "Time elapsed: " << double(diffclock(end,begin)) << " ms"<< endl;

return 0;


}



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.