В этом простом примере мы напишем рутину, чтобы распечатать все факториалы, которые впишутся в 128-битное целое число. В конце рутины мы делаем некоторые причудливые iostream форматирование результатов:
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <iomanip>
#include <vector>
void print_factorials()
{
using boost::multiprecision::cpp_int;
cpp_int limit = (cpp_int(1) << 128) - 1;
std::vector<cpp_int> results;
unsigned i = 1;
cpp_int factorial = 1;
while(factorial < limit)
{
results.push_back(factorial);
++i;
factorial *= i;
}
unsigned digits = results.back().str().size();
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;
}
}
Результатом этой рутины является:
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