document.write('<div class="java5" style="font-family: monospace;"><br />');
document.write('<span style="color: #808080; font-style: italic;">/**<br />');
document.write('&nbsp;* FastExpon is a fast computational algorithm for computing large exponents. x^e % m<br />');
document.write('&nbsp;* This algorithm was used for a cryptography course dealing with RSA.<br />');
document.write('&nbsp;* Feel free to slice it up for yourself. :)<br />');
document.write('&nbsp;* @author Alan Lambkin<br />');
document.write('&nbsp;* @version 1.0 September 2006<br />');
document.write('&nbsp;*/</span><br />');
document.write('<br />');
document.write('<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FastExpon<br />');
document.write('<span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">long</span> y = <span style="color: #cc66cc;">1</span>;<br />');
document.write('<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Performs the calculation...<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * While the exponent is not zero:<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * If the exponent is even, square the base, mod m, and divide the exponent by 2.<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * If the exponent is odd, mulitply the base times y (initially set to 1) mod m,<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * now place that value into y, and subtract 1 from the exponent.<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @parm &nbsp; &nbsp; &nbsp; &nbsp;x&nbsp; &nbsp; &nbsp; the base<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @parm &nbsp; &nbsp; &nbsp; &nbsp;e&nbsp; &nbsp; &nbsp; the exponent<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @parm &nbsp; &nbsp; &nbsp; &nbsp;m&nbsp; &nbsp; &nbsp; the mod<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @return &nbsp; &nbsp; &nbsp;The answer, x raised to e, mod m.<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">long</span> fex<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">long</span> x, <span style="color: #993333;">long</span> e, <span style="color: #993333;">long</span> m<span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>e != <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>e%<span style="color: #cc66cc;">2</span>==<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x=x*x%m;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e=e/<span style="color: #cc66cc;">2</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y=x*y%m;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e=e<span style="color: #cc66cc;">-1</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> y;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Just a simple main program so I can test before posting.<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Prints out the answer.<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a target="_blank"&nbsp; href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">long</span> x = <span style="color: #cc66cc;">6916502</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">long</span> e = <span style="color: #cc66cc;">2773637</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">long</span> m = <span style="color: #cc66cc;">8326363</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FastExpon f = <span style="color: #000000; font-weight: bold;">new</span> FastExpon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>x + <span style="color: #ff0000;">&quot; raised to the &quot;</span> + e + <span style="color: #ff0000;">&quot; power, mod &quot;</span> + m + <span style="color: #ff0000;">&quot; = &quot;</span> + f.<span style="color: #006600;">fex</span><span style="color: #66cc66;">&#40;</span>x,e,m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('<span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp;</div>');
document.write('<br />&nbsp;<br /><div style="font-size: 12px">Brought to you by the community at <a href="http://www.bytemycode.com/snippets/snippet/374/1/" target="_blank">byteMyCode</a>.</div>');
