<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="gmp.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.gmp.html">GMP Functions</a></div> <div class="up"><a href="book.gmp.html">GMP</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="gmp.examples" class="chapter"> <h1>Examples</h1> <p class="para"> <div class="example" id="example-3398"> <p><strong>Example #1 Factorial function using GMP</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">fact</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) <br />{<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /> for (</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">2</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">$x</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">gmp_mul</span><span style="color: #007700">(</span><span style="color: #0000BB">$return</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">);<br /> }<br /> return </span><span style="color: #0000BB">$return</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">gmp_strval</span><span style="color: #007700">(</span><span style="color: #0000BB">fact</span><span style="color: #007700">(</span><span style="color: #0000BB">1000</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> This will calculate factorial of 1000 (pretty big number) very fast. </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="gmp.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.gmp.html">GMP Functions</a></div> <div class="up"><a href="book.gmp.html">GMP</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>