<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wojciech Jamrozy blog &#187; Algorithms</title>
	<atom:link href="http://www.wojtekrj.net/category/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wojtekrj.net</link>
	<description>Blog about computer science, programming and linux</description>
	<lastBuildDate>Sun, 23 Oct 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Python,Algorithms] Fast Modular exponentiation script</title>
		<link>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/</link>
		<comments>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 19:26:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[exponentiation]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=125</guid>
		<description><![CDATA[If we want to know the last ten digits of number an – we have to evaluate expression an mod 1010. Using brute force approach, we have to do O(n) ( If you don&#8217;t understand big „O” notation, visit: Big O notation &#8211; it&#8217;s VERY important) multiplications and modulo divisions. When n is greater than [...]]]></description>
			<content:encoded><![CDATA[<p>If we want to know the last ten digits of number <em>a<sup>n</sup></em> – we have to evaluate expression <em>a<sup>n</sup> mod 10<sup>10</sup></em>. Using brute force approach, we have to do<em> O(n) </em>( If you don&#8217;t understand big „O” notation, visit: <a href="http://en.wikipedia.org/wiki/Big_O_notation" target="_blank">Big O notation</a> &#8211; it&#8217;s VERY important) multiplications and modulo divisions. When<em> n</em> is greater than <em>10<sup>9</sup></em>, it will take a lot of time. Fortunately, we can use more „mathematical” approach, which has<em> O(log n) </em>complexity.<span id="more-125"></span><br />
Firstly, we have to notice, that:<br />
(1) a<sup>2c</sup> mod r =  (a<sup>c</sup>)<sup>2</sup> mod r<br />
(2) a<sup>2c+1</sup> mod r =  a*(a<sup>c</sup>)<sup>2</sup> mod r</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Author: Wojtek Jamrozy (www.wojtekrj.net)</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> modexp<span style="color: black;">&#40;</span>a, n, m<span style="color: black;">&#41;</span>:
	bits = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> n:
		bits.<span style="color: black;">append</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">%</span>2<span style="color: black;">&#41;</span>
		n /= <span style="color: #ff4500;">2</span>
	solution = <span style="color: #ff4500;">1</span>
	bits.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> bits:
		solution = <span style="color: black;">&#40;</span>solution<span style="color: #66cc66;">*</span>solution<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>m
		<span style="color: #ff7700;font-weight:bold;">if</span> x:
			solution = <span style="color: black;">&#40;</span>solution<span style="color: #66cc66;">*</span>a<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>m
	<span style="color: #ff7700;font-weight:bold;">return</span> solution
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> modexp<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">34</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> modexp<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">7179869184</span>
<span style="color: #000000;">1787109376</span></pre></div></div>

<p>Function <em>pow(a,b)</em> is Python&#8217;s function, which computes <em>a<sup>b</sup></em><br />
Function <em>modexp(a, n, m)</em> computes <em>a<sup>n</sup> mod m</em>. Here is example, which shows running of the function a=2, n=34, m=10<sup>10</sup><br />
Firstly, we want to have bits of binary notation of <em>n</em> in list <em>bits</em>.<br />
Before reversion,<em> bits </em>have following content: [0, 1, 0, 0, 0, 1] (2<sup>1</sup> + 2<sup>5</sup> = 33)<br />
After reversion of list: <em>bits</em>= [1, 0, 0, 0, 1, 0]<br />
Bits of binary notation of <em>n</em> are in order from &#8220;bigger&#8221; powers to &#8220;lower&#8221; ones.<br />
I will use (b) for binary notation of number: 33 = 100001(b)<br />
At the beggining, <em>solution</em> = 2<sup>0</sup> mod 10<sup>10</sup> = 1. Then, during execution of <em>for</em> loop,<em> solution </em>has following values: 2<sup>1(b)</sup> mod 10<sup>10</sup>, 2<sup>10(b)</sup> mod 10<sup>10</sup>, 2<sup>100(b)</sup> mod 10<sup>10</sup>, 2<sup>1000(b)</sup> mod 10<sup>10</sup>, 2<sup>10001(b)</sup> mod 10<sup>10</sup> and finally  2<sup>100010(b)</sup> mod 10<sup>10</sup>.<br />
Using (1) and (2) we can calcualte every <em>solution&#8217;s</em> value if we know previous one.<br />
Loop <em>for</em> is executed for each element of <em>bits</em>. <em>Bits</em> size is <em>log<sub>2</sub>n</em>, so the complexity of whole alghorithm is <em>O(log n)</em></p>
<p>You can download code here:<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;title=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" id="wpa2a_2">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[Algorithms] List of online judges/algorithmical contests</title>
		<link>http://www.wojtekrj.net/2008/09/algorithms-list-of-online-judgesalgorithmical-contests/</link>
		<comments>http://www.wojtekrj.net/2008/09/algorithms-list-of-online-judgesalgorithmical-contests/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 16:42:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[online judge]]></category>
		<category><![CDATA[programming contest]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=104</guid>
		<description><![CDATA[Here is a list of websites, which are sets of algorithmical tasks or online contests: http://www.topcoder.com http://www.spoj.pl http://icpcres.ecs.baylor.edu/onlinejudge/ http://acm.timus.ru http://acm.mipt.ru/judge http://www.uwp.edu/sws/usaco/ http://acm.sgu.ru/ http://projecteuler.net/]]></description>
			<content:encoded><![CDATA[<p>Here is a list of websites, which are sets of algorithmical tasks or online contests:<br />
<a href="http://www.topcoder.com" target="_blank">http://www.topcoder.com</a><br />
<a href="http://www.spoj.pl" target="_blank">http://www.spoj.pl</a><br />
<a href="http://icpcres.ecs.baylor.edu/onlinejudge/" target="_blank">http://icpcres.ecs.baylor.edu/onlinejudge/</a><br />
<a href="http://acm.timus.ru" target="_blank">http://acm.timus.ru</a><br />
<a href="http://acm.mipt.ru/judge" target="_blank">http://acm.mipt.ru/judge</a><br />
<a href="http://www.uwp.edu/sws/usaco/" target="_blank">http://www.uwp.edu/sws/usaco/</a><br />
<a href="http://acm.sgu.ru/" target="_blank">http://acm.sgu.ru/</a><br />
<a href="http://projecteuler.net/" target="_blank">http://projecteuler.net/</a></p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;linkname=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Falgorithms-list-of-online-judgesalgorithmical-contests%2F&amp;title=%5BAlgorithms%5D%20List%20of%20online%20judges%2Falgorithmical%20contests" id="wpa2a_4">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/09/algorithms-list-of-online-judgesalgorithmical-contests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

