This is a multi-part question. I am writing a computationally intensive program that will perform computations on very large numbers, on the scale of factorial(100) . I'm considering using Java or C++ (that's all I know, and C++ only slightly) but I'm not sure which would be better to use in this context.

I know C++ will be faster, but Java has a built-in utility for large numbers, the BigInteger class, and I don't know of any equivalent in C++. So here are the questions...

  1. Is C++ so much faster than java as to make it worth it to learn and find a way to handle large numbers?
  2. If I should use C++, how would I handle the large numbers?
  3. Is it possible to just specify a new data type in C++ that represents a number, but with larger bounds than the int?

closed as off-topic by Robert Harvey, Dan Pichelman, Dynamic, user40980, Tulains Córdova Jul 18 '13 at 11:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about what language, technology, or project one should take up next are off topic on Programmers, as they can only attract subjective opinions for answers. There are too many individual factors behind the question to create answers that will have lasting value. You may be able to get help in The Whiteboard, our chat room." – Robert Harvey, Dan Pichelman, Dynamic, Community, Tulains Córdova
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I've heard of this C++ library for large numbers: gmplib.org – FrustratedWithFormsDesigner Jul 11 '11 at 20:51
  • I hope you mean 100 digits :-) – Karl Bielefeldt Jul 11 '11 at 20:57
  • 1
    @Karl Bielefeldt: Maybe "100!" should be read as "The factorial of one hundred"? – FrustratedWithFormsDesigner Jul 11 '11 at 21:03
  • Doesn't this depend quite bit on the constraints of your project? For example, if you need to have this done tomorrow, Python might be a better language than either of those. If you have many concurrent tasks, Go might be better, etc? – Jack M. Jul 11 '11 at 21:21
  • 2
    Missed that one, @Frustrated. I've been outside a math classroom too long, I guess. That does make a lot more sense than being really excited about numbers as high as 100 :-) – Karl Bielefeldt Jul 11 '11 at 21:22

Check out GNU MP. It has a C++ class based interface if the C one is too scary. As far as the speed comparison goes, I would seriously take into account programmer speed here. If it's going to take an extra month to do it "the fast way" for a one-shot program because of unfamiliarity with the language, I wouldn't bother.

  • Thanks I decided to stick with Java, you were right, I would've taken me an extra month just to learn the c/c++ needed to use GNU MP, however thanks for your suggestion. – Ryan Stull Aug 17 '11 at 3:44

Is c++ so much faster than java as to make it worth it to learn and find a way to handle large numbers?

Is performance a critical concern? Do you need to perform computations with hard timing requirements? If so, then C or C++ would be the obvious choice.

Otherwise, given your familiarity with Java, it might be a good idea to just use that.

You might also want to consider something like OCaml, which I understand has a fast bigint library. It and other functional languages are worth learning anyway, so this is as good an excuse as any. :)

If I should use c++, how would I handle the large numbers?

Most people will just use GNU MP. It's a very solid library, and it's quite fast.

Is it possible to just specify a new data type in c++ that represents a number, but with larger bounds than the int?

Yes. In fact, you can define a class in C++ that looks and behaves identically to a native type (with a few limitations) rather easily. Almost all of the operators you would use with it can be overloaded and you have a great deal of control over how those operators behave.

Of course, with that power comes a number of gotchas, so you should prefer to rely on an existing library.


Is this a one-off or a program that will be used over and over by many people?

If the former, you might consider wsMaxima or Lisp (R or Matlab maybe?), a language that handles bigints naturally. It's built-in support should be plenty fast.

Otherwise Java or C++ would be better.

I'm basing this on the notion that the most important thing will be the performance of the bigint package itself. Doing basic arithmetic on those things could well be your biggest time-hog, so you'd want the bigint package to be as fast as possible. I would suppose the "wrapper" code outside the bigint class would not be spending such a high percent of time, compared to time spent in the class.

Whether the Java class or the C++ class is faster would need to be tested, with things like your 100!.


IMO, it depends primarily on the language(s) you already know. If you already know something like Lisp that supports large integers natively, then that's an obvious choice.

Otherwise, getting and using a large integer library is generally easy enough that it's going to be easier to download/use one than to switch to a different language just for that feature. For example, if you're accustomed to using C++, computing 100! with Victor Shoup's NTL looks something like this:

#include <iostream>
#include "NTL/zz.h"

int main() { 
    NTL::ZZ result, i;

    for (result = 1, i=2; i<=100; i++)
        result *= i;
    std::cout << result;
    return 0;

In short, you add one header, and change the data type, but otherwise the code can be written essentially as if you were using int's.

Between C++ and Java, I would (predictably, I suppose) advise C++. As the code above demonstrates, most bigint libraries for C++ use operator overloading to make working with them similar to working with normal ints, etc.

Despite being provided as part of the normal Java distribution, the same is definitely not true of Java's BigInteger class. In Java, you have to use member function notation for everything, so result *= i becomes something like result.assign(result.multiply(i)); (it's been a while since I used it, so I'm probably remembering names a bit wrong, but you get the general idea).


Keep in mind that the Java numeric classes are immutable, which means that you may be creating and discarding large numbers of them, which will then have to be garbage-collected. If they're all short-lived and you give your program enough heap space then it probably won't be an issue, but it's a potential problem area. But if you're more comfortable with Java, and your computational code will get called often enough to get JITted, then it's possible Java would be fast enough (for suitable values of "enough", of course).

Not the answer you're looking for? Browse other questions tagged or ask your own question.