Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext
Factorials

In this simple example, we'll write a routine to print out all of the factorials which will fit into a 128-bit integer. At the end of the routine we do some fancy iostream formatting of the results:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <iomanip>
#include <vector>


void print_factorials()
{
   using boost::multiprecision::cpp_int;
   //
   // Print all the factorials that will fit inside a 128-bit integer.
   //
   // Begin by building a big table of factorials, once we know just how 
   // large the largest is, we'll be able to "pretty format" the results.
   //
   // Calculate the largest number that will fit inside 128 bits, we could
   // also have used numeric_limits<int128_t>::max() for this value:
   cpp_int limit = (cpp_int(1) << 128) - 1;
   // 
   // Our table of values:
   std::vector<cpp_int> results;
   //
   // Initial values:
   unsigned i = 1;
   cpp_int factorial = 1;
   //
   // Cycle through the factorials till we reach the limit:
   while(factorial < limit)
   {
      results.push_back(factorial);
      ++i;
      factorial *= i;
   }
   //
   // Lets see how many digits the largest factorial was:
   unsigned digits = results.back().str().size();
   //
   // Now print them out, using right justification, while we're at it
   // we'll indicate the limit of each integer type, so begin by defining
   // the limits for 16, 32, 64 etc bit integers:
   cpp_int limits[] = {
      (cpp_int(1) << 16) - 1,
      (cpp_int(1) << 32) - 1,
      (cpp_int(1) << 64) - 1,
      (cpp_int(1) << 128) - 1,
   };
   std::string bit_counts[] = { "16", "32", "64", "128" };
   unsigned current_limit = 0;
   for(unsigned j = 0; j < results.size(); ++j)
   {
      if(limits[current_limit] < results[j])
      {
         std::string message = "Limit of " + bit_counts[current_limit] + " bit integers";
         std::cout << std::setfill('.') << std::setw(digits+1) << std::right << message << std::setfill(' ') << std::endl;
         ++current_limit;
      }
      std::cout << std::setw(digits + 1) << std::right << results[j] << std::endl;
   }
}

The output from this routine is:

                                       1
                                       2
                                       6
                                      24
                                     120
                                     720
                                    5040
                                   40320
................Limit of 16 bit integers
                                  362880
                                 3628800
                                39916800
                               479001600
................Limit of 32 bit integers
                              6227020800
                             87178291200
                           1307674368000
                          20922789888000
                         355687428096000
                        6402373705728000
                      121645100408832000
                     2432902008176640000
................Limit of 64 bit integers
                    51090942171709440000
                  1124000727777607680000
                 25852016738884976640000
                620448401733239439360000
              15511210043330985984000000
             403291461126605635584000000
           10888869450418352160768000000
          304888344611713860501504000000
         8841761993739701954543616000000
       265252859812191058636308480000000
      8222838654177922817725562880000000
    263130836933693530167218012160000000
   8683317618811886495518194401280000000
 295232799039604140847618609643520000000

PrevUpHomeNext