1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Factorials</title> 5<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision"> 8<link rel="up" href="../egs.html" title="Examples"> 9<link rel="prev" href="../egs.html" title="Examples"> 10<link rel="next" href="bitops.html" title="Bit Operations"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../egs.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../egs.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bitops.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="boost_multiprecision.tut.ints.egs.factorials"></a><a class="link" href="factorials.html" title="Factorials">Factorials</a> 28</h5></div></div></div> 29<p> 30 In this simple example, we'll write a routine to print out all of the 31 factorials which will fit into a 128-bit integer. At the end of the routine 32 we do some fancy iostream formatting of the results: 33 </p> 34<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 35<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 36<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iomanip</span><span class="special">></span> 37<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span> 38 39 40<span class="keyword">void</span> <span class="identifier">print_factorials</span><span class="special">()</span> 41<span class="special">{</span> 42 <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_int</span><span class="special">;</span> 43 <span class="comment">//</span> 44 <span class="comment">// Print all the factorials that will fit inside a 128-bit integer.</span> 45 <span class="comment">//</span> 46 <span class="comment">// Begin by building a big table of factorials, once we know just how</span> 47 <span class="comment">// large the largest is, we'll be able to "pretty format" the results.</span> 48 <span class="comment">//</span> 49 <span class="comment">// Calculate the largest number that will fit inside 128 bits, we could</span> 50 <span class="comment">// also have used numeric_limits<int128_t>::max() for this value:</span> 51 <span class="identifier">cpp_int</span> <span class="identifier">limit</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">128</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span> 52 <span class="comment">//</span> 53 <span class="comment">// Our table of values:</span> 54 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">cpp_int</span><span class="special">></span> <span class="identifier">results</span><span class="special">;</span> 55 <span class="comment">//</span> 56 <span class="comment">// Initial values:</span> 57 <span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 58 <span class="identifier">cpp_int</span> <span class="identifier">factorial</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 59 <span class="comment">//</span> 60 <span class="comment">// Cycle through the factorials till we reach the limit:</span> 61 <span class="keyword">while</span><span class="special">(</span><span class="identifier">factorial</span> <span class="special"><</span> <span class="identifier">limit</span><span class="special">)</span> 62 <span class="special">{</span> 63 <span class="identifier">results</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">);</span> 64 <span class="special">++</span><span class="identifier">i</span><span class="special">;</span> 65 <span class="identifier">factorial</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span> 66 <span class="special">}</span> 67 <span class="comment">//</span> 68 <span class="comment">// Lets see how many digits the largest factorial was:</span> 69 <span class="keyword">unsigned</span> <span class="identifier">digits</span> <span class="special">=</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">back</span><span class="special">().</span><span class="identifier">str</span><span class="special">().</span><span class="identifier">size</span><span class="special">();</span> 70 <span class="comment">//</span> 71 <span class="comment">// Now print them out, using right justification, while we're at it</span> 72 <span class="comment">// we'll indicate the limit of each integer type, so begin by defining</span> 73 <span class="comment">// the limits for 16, 32, 64 etc bit integers:</span> 74 <span class="identifier">cpp_int</span> <span class="identifier">limits</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> 75 <span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">16</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">,</span> 76 <span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">32</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">,</span> 77 <span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">64</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">,</span> 78 <span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">128</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">,</span> 79 <span class="special">};</span> 80 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">bit_counts</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="string">"16"</span><span class="special">,</span> <span class="string">"32"</span><span class="special">,</span> <span class="string">"64"</span><span class="special">,</span> <span class="string">"128"</span> <span class="special">};</span> 81 <span class="keyword">unsigned</span> <span class="identifier">current_limit</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 82 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special"><</span> <span class="identifier">results</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> 83 <span class="special">{</span> 84 <span class="keyword">if</span><span class="special">(</span><span class="identifier">limits</span><span class="special">[</span><span class="identifier">current_limit</span><span class="special">]</span> <span class="special"><</span> <span class="identifier">results</span><span class="special">[</span><span class="identifier">j</span><span class="special">])</span> 85 <span class="special">{</span> 86 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">message</span> <span class="special">=</span> <span class="string">"Limit of "</span> <span class="special">+</span> <span class="identifier">bit_counts</span><span class="special">[</span><span class="identifier">current_limit</span><span class="special">]</span> <span class="special">+</span> <span class="string">" bit integers"</span><span class="special">;</span> 87 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setfill</span><span class="special">(</span><span class="char">'.'</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setw</span><span class="special">(</span><span class="identifier">digits</span><span class="special">+</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">right</span> <span class="special"><<</span> <span class="identifier">message</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setfill</span><span class="special">(</span><span class="char">' '</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 88 <span class="special">++</span><span class="identifier">current_limit</span><span class="special">;</span> 89 <span class="special">}</span> 90 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setw</span><span class="special">(</span><span class="identifier">digits</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">right</span> <span class="special"><<</span> <span class="identifier">results</span><span class="special">[</span><span class="identifier">j</span><span class="special">]</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 91 <span class="special">}</span> 92<span class="special">}</span> 93</pre> 94<p> 95 The output from this routine is: 96 </p> 97<pre class="programlisting"> 1 98 2 99 6 100 24 101 120 102 720 103 5040 104 40320 105................Limit of 16 bit integers 106 362880 107 3628800 108 39916800 109 479001600 110................Limit of 32 bit integers 111 6227020800 112 87178291200 113 1307674368000 114 20922789888000 115 355687428096000 116 6402373705728000 117 121645100408832000 118 2432902008176640000 119................Limit of 64 bit integers 120 51090942171709440000 121 1124000727777607680000 122 25852016738884976640000 123 620448401733239439360000 124 15511210043330985984000000 125 403291461126605635584000000 126 10888869450418352160768000000 127 304888344611713860501504000000 128 8841761993739701954543616000000 129 265252859812191058636308480000000 130 8222838654177922817725562880000000 131 263130836933693530167218012160000000 132 8683317618811886495518194401280000000 133 295232799039604140847618609643520000000 134</pre> 135</div> 136<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 137<td align="left"></td> 138<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 139 Maddock and Christopher Kormanyos<p> 140 Distributed under the Boost Software License, Version 1.0. (See accompanying 141 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 142 </p> 143</div></td> 144</tr></table> 145<hr> 146<div class="spirit-nav"> 147<a accesskey="p" href="../egs.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../egs.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bitops.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 148</div> 149</body> 150</html> 151