Cannot calculate factorials bigger than 20! ! How to do so?

The limit on an unsigned long long is 18446744073709551615, or about 1.8e+19. 20! is about 2.4e+18, so within range, however 21! is about 5.1e+19, exceeding the maximum size of an unsigned long long.

You may find this helpful: Are there types bigger than long long int in C++?


You're overflowing your integer type. It's likely that unsigned long long is 64 bits long for you.

  • 20! is 0x21c3_677c_82b4_0000 which fits.
  • 21! is 0x2_c507_7d36_b8c4_0000 which does not fit.

You can look into libraries like GMP which enable arbitrarily large integers.


To extend the GMP comment. Here's some code that will calculate the factorial using GMP:

void factorial(unsigned long long n, mpz_t result) {
    mpz_set_ui(result, 1);

    while (n > 1) {
        mpz_mul_ui(result, result, n);
        n = n-1;
    }
}

int main() {
    mpz_t fact;
    mpz_init(fact);

    factorial(100, fact);

    char *as_str = mpz_get_str(NULL, 16, fact);
    printf("%s\n", as_str);

    mpz_clear(fact);
    free(as_str);
}

This will calculate factorial(100), and results in:

0x1b30964ec395dc24069528d54bbda40d16e966ef9a70eb21b5b2943a321cdf10391745570cca9420c6ecb3b72ed2ee8b02ea2735c61a000000000000000000000000

And just for fun, here's the C++ version. Constructors, destructors and operator overloading tend to make the C++ version of these things look a bit cleaner. The result is the same as before.

#include <gmpxx.h>
#include <iostream>

int main() {
    mpz_class fact = 1;

    for (int i=2; i<=100; ++i)
        fact *= i;

    std::cout << "0x" << fact.get_str(16) << "\n";
}

Range of unsigned long long is usually 0 to 2^64 - 1 (18,446,744,073,709,551,615). 21! goes out of this range.


Indeed:

2^64 = 18446744073709551616
21!  = 51090942171709440000
20!  =  2432902008176640000

By the way, to calculate the result of a series (e.g., Taylor) you should not calculate each term separately; this will definitely bring you problems such as this one. Instead, try to calculate each term by reusing the previous one.

For example, the Taylor series for cos requires the summation of terms like:

(-1)^i * (x^(2*i)) / (2i)!

It is easy to see that each term can be calculated easily from the previous one:

newterm = - oldterm * x^2 / ((2i+1)*(2i+2))

So, I believe that you don't need to calculate large factorials, for what you're trying to do. On the other hand, if you need to, you'll have to use a library for big numbers, such as gmp.