<?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>Eli Bendersky's website &#187; Math</title>
	<atom:link href="http://eli.thegreenplace.net/category/science/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Fri, 30 Jul 2010 12:30:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Horner&#8217;s rule: efficient evaluation of polynomials</title>
		<link>http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/</link>
		<comments>http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 13:10:32 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=2140</guid>
		<description><![CDATA[Here&#8217;s a general degree-n polynomial:

To evaluate such a polynomial using a computer program, several approaches can be employed.
The simplest, naive method is to compute each term of the polynomial separately and then add them up. Here&#8217;s the Python code for it:

def poly_naive(A, x):
    p = 0
    for i, a [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient integer exponentiation algorithms'>Efficient integer exponentiation algorithms</a> <small>Did you ever think about the most efficient method to...</small></li><li><a href='http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient modular exponentiation algorithms'>Efficient modular exponentiation algorithms</a> <small>Earlier this week I&#8217;ve discussed efficient algorithms for exponentiation. However,...</small></li><li><a href='http://eli.thegreenplace.net/2004/05/12/the-rule-of-72/' rel='bookmark' title='Permanent Link: the rule of 72'>the rule of 72</a> <small>Say you invest some amount of money, and you get...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a general degree-n polynomial:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f8564836f4f03d587c80450d3db13f64.gif" alt="p(x) = \sum_{i=0}^n a_i x^i = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots + a_n x^n" title="p(x) = \sum_{i=0}^n a_i x^i = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots + a_n x^n" style="vertical-align: -5px; border: none;"/></p>
<p>To evaluate such a polynomial using a computer program, several approaches can be employed.</p>
<p>The simplest, naive method is to compute each term of the polynomial separately and then add them up. Here&#8217;s the Python code for it:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">poly_naive</span>(A, x):
    p = <span style="color: #007f7f">0</span>
    <span style="color: #00007f; font-weight: bold">for</span> i, a <span style="color: #0000aa">in</span> <span style="color: #00007f">enumerate</span>(A):
        p += (x ** i) * a
    <span style="color: #00007f; font-weight: bold">return</span> p
</pre>
</div>
<p><tt class="docutils literal"><span class="pre">A</span></tt> is an array of coefficients, lowest first, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6a9275b7f966e45ffb33492e358c8dff.gif" alt="a_0" title="a_0" style="vertical-align: -3px; border: none;"/> until <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-825b3fd5bafbc46b9a560ea9f16b21dd.gif" alt="a_n" title="a_n" style="vertical-align: -3px; border: none;"/>.</p>
<p>This method is quite inefficient. It requires <tt class="docutils literal"><span class="pre">n</span></tt> additions (since there are <tt class="docutils literal"><span class="pre">n+1</span></tt> terms to be added) and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3774ed8ed8a0304b7005a434c796d154.gif" alt="(n^2 + n)/2" title="(n^2 + n)/2" style="vertical-align: -5px; border: none;"/> multiplications.</p>
<div class="section" id="iterative-method">
<h3>Iterative method</h3>
<p>It&#8217;s obvious that there&#8217;s a lot of repetitive computations being done by raising <tt class="docutils literal"><span class="pre">x</span></tt> to successive powers. We can make things much more efficient by simply keeping the previous power of <tt class="docutils literal"><span class="pre">x</span></tt> between iterations. This is the &quot;iterative method&quot;:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">poly_iter</span>(A, x):
    p = <span style="color: #007f7f">0</span>
    xn = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> a <span style="color: #0000aa">in</span> A:
        p += xn * a
        xn *= x
    <span style="color: #00007f; font-weight: bold">return</span> p
</pre>
</div>
<p>In this code <tt class="docutils literal"><span class="pre">xn</span></tt> is the current power of <tt class="docutils literal"><span class="pre">x</span></tt>. We don&#8217;t need to raise <tt class="docutils literal"><span class="pre">x</span></tt> to a power on each iteration of the loop, a single multiplication suffices. It&#8217;s easy to see that there are <tt class="docutils literal"><span class="pre">2n</span></tt> multiplications and <tt class="docutils literal"><span class="pre">n</span></tt> additions for each computation. The algorithm is now linear instead of quadratic.</p>
</div>
<div class="section" id="horner-s-rule">
<h3>Horner&#8217;s rule</h3>
<p>It can be further improved, however. Take a look at this polynomial:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7bc59321df22ede087d3db24cfbd147e.gif" alt="6 x^3 + 4 x^2 + 7 x + 19" title="6 x^3 + 4 x^2 + 7 x + 19" style="vertical-align: -1px; border: none;"/></p>
<p>It can be rewritten as follows:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d2e3363e41019878d3c7e30cfac96016.gif" alt="((6 x + 4) x + 7) x + 19" title="((6 x + 4) x + 7) x + 19" style="vertical-align: -4px; border: none;"/></p>
<p>And in general, we can always rewrite the polynomial:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-dd44c9287e5e4e948781c66c36378ca6.gif" alt="a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots + a_n x^n" title="a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots + a_n x^n" style="vertical-align: -4px; border: none;"/></p>
<p>As:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f81cf7d037ea68d49eb520eb41164504.gif" alt="a_0 + x (a_1 + x ( \cdots + x (a_{n-1} + a_n x))" title="a_0 + x (a_1 + x ( \cdots + x (a_{n-1} + a_n x))" style="vertical-align: -4px; border: none;"/></p>
<p>This rearrangement is usually called &quot;Horner&#8217;s rule&quot;. We can write the code to implement it as follows:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">poly_horner</span>(A, x):
    p = A[-<span style="color: #007f7f">1</span>]
    i = <span style="color: #00007f">len</span>(A) - <span style="color: #007f7f">2</span>
    <span style="color: #00007f; font-weight: bold">while</span> i &gt;= <span style="color: #007f7f">0</span>:
        p = p * x + A[i]
        i -= <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">return</span> p
</pre>
</div>
<p>Here we start by assigning <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-825b3fd5bafbc46b9a560ea9f16b21dd.gif" alt="a_n" title="a_n" style="vertical-align: -3px; border: none;"/> to <cite>p</cite> and then successively multiplying by <cite>x</cite> and adding the next coefficient. This code requires <cite>n</cite> multiplications and <cite>n</cite> additions (I&#8217;m ignoring here the modification of the loop variable <tt class="docutils literal"><span class="pre">i</span></tt>, as I ignored it in all other algorithms, where it was implicit in the Python <tt class="docutils literal"><span class="pre">for</span></tt> loop).</p>
<p>While asymptotically similar to the iterative method, Horner&#8217;s method has better constants and thus is faster.</p>
<p>Curiously, Horner&#8217;s rule was discovered in the early 19th century, far before the advent of computers. It&#8217;s obviously useful for manual computation of polynomials as well, for the same reason: it requires less operations.</p>
<p>I&#8217;ve timed the 3 algorithms on a random polynomial of degree 500. The one using Horner&#8217;s rule is about 5 times faster than the naive approach, and 15% faster than the iterative method.</p>
</div>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=2140&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient integer exponentiation algorithms'>Efficient integer exponentiation algorithms</a> <small>Did you ever think about the most efficient method to...</small></li><li><a href='http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient modular exponentiation algorithms'>Efficient modular exponentiation algorithms</a> <small>Earlier this week I&#8217;ve discussed efficient algorithms for exponentiation. However,...</small></li><li><a href='http://eli.thegreenplace.net/2004/05/12/the-rule-of-72/' rel='bookmark' title='Permanent Link: the rule of 72'>the rule of 72</a> <small>Say you invest some amount of money, and you get...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A group-theoretic proof of Euler&#8217;s theorem</title>
		<link>http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/</link>
		<comments>http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 06:00:43 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1787</guid>
		<description><![CDATA[A very important and useful theorem in number theory is named after Leonhard Euler:

Where  is Euler&#8217;s totient function &#8211; the amount of numbers smaller than n that are coprime to it.
Here I want to present a nice proof of this theorem, based on group theory. I begin with some preliminary definitions and gradually move [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/' rel='bookmark' title='Permanent Link: Equivalence classes and group partitions'>Equivalence classes and group partitions</a> <small>In this post I want to show some interesting definitions...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2008/01/17/pythagorean-the-theorem-with-most-proofs/' rel='bookmark' title='Permanent Link: Pythagorean &#8211; the theorem with most proofs ?'>Pythagorean &#8211; the theorem with most proofs ?</a> <small>This page shows 76 different proofs of the Pythagorean theorem....</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>A very important and useful theorem in number theory is named after Leonhard Euler:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c0757b340f85083e73f05fd0813d29f9.gif" alt="a^{\varphi (n)} \equiv 1 \pmod{n}" title="a^{\varphi (n)} \equiv 1 \pmod{n}" style="vertical-align: -4px; border: none;"/></p>
<p>Where <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-40ba55cd3c58225334c65204b80c6ca3.gif" alt="\varphi (n)" title="\varphi (n)" style="vertical-align: -4px; border: none;"/> is <a class="reference external" href="http://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler&#8217;s totient</a> function &#8211; the amount of numbers smaller than <tt class="docutils literal"><span class="pre">n</span></tt> that are coprime to it.</p>
<p>Here I want to present a nice proof of this theorem, based on group theory. I begin with some preliminary definitions and gradually move towards the final goal.</p>
<p><strong>(I) Congruence class</strong>: Let <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">n</span> <span class="pre">&gt;</span> <span class="pre">0</span></tt> be integers. The set of all integers that have the same remainder as <tt class="docutils literal"><span class="pre">a</span></tt> when divided by <tt class="docutils literal"><span class="pre">n</span></tt> is called the congruence class of a modulo <tt class="docutils literal"><span class="pre">n</span></tt> and is denoted by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82220507d8eb630f4abc9fb26d4d82f3.gif" alt="[a]_n" title="[a]_n" style="vertical-align: -5px; border: none;"/>, where:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3a9631173e7ba72372ecbd874dfb280b.gif" alt="[a]_n=\{x\in \mathbb{Z}|x\equiv a(mod n)\}" title="[a]_n=\{x\in \mathbb{Z}|x\equiv a(mod n)\}" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/> is the set of all congruence classes modulo <tt class="docutils literal"><span class="pre">n</span></tt>.</p>
<p><strong>(II) Units of</strong> <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/>: If for <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82220507d8eb630f4abc9fb26d4d82f3.gif" alt="[a]_n" title="[a]_n" style="vertical-align: -5px; border: none;"/> we find some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-aa601d4f63db4c9f8417b2deb233f4a4.gif" alt="[b]_n" title="[b]_n" style="vertical-align: -5px; border: none;"/> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-733610989b53788a6f6eb587fc0eb955.gif" alt="[a]_n[b]_n=[1]_n" title="[a]_n[b]_n=[1]_n" style="vertical-align: -5px; border: none;"/>, we call <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82220507d8eb630f4abc9fb26d4d82f3.gif" alt="[a]_n" title="[a]_n" style="vertical-align: -5px; border: none;"/> a unit of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/>. The set of units of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/> is denoted by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-97af68798b90b52ed28e9787302c11a3.gif" alt="\mathbb{Z}^{\times}_{n}" title="\mathbb{Z}^{\times}_{n}" style="vertical-align: -4px; border: none;"/></p>
<p>For example, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-9b44b5d20b9b5a38fa411a4325945b64.gif" alt="[2]_n" title="[2]_n" style="vertical-align: -5px; border: none;"/> is a unit of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-31ca03ce0da693f26441616fa9b184a5.gif" alt="\mathbb{Z}_7" title="\mathbb{Z}_7" style="vertical-align: -3px; border: none;"/>, because <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d1220ed716ed5c4af1f4fb77f5130187.gif" alt=" [2]_7[4]_7=[1]_7 " title=" [2]_7[4]_7=[1]_7 " style="vertical-align: -5px; border: none;"/>.</p>
<p><strong>(III)</strong> The congruence class <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82220507d8eb630f4abc9fb26d4d82f3.gif" alt="[a]_n" title="[a]_n" style="vertical-align: -5px; border: none;"/> is a unit of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/> if and only if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-34725615e6d3999f90f330599f725f08.gif" alt="(a,n) = 1" title="(a,n) = 1" style="vertical-align: -4px; border: none;"/> (the GCD of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">n</span></tt> is 1, in other words they&#8217;re co-prime).</p>
<p>Proof: By definition of units, there exists some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-aa601d4f63db4c9f8417b2deb233f4a4.gif" alt="[b]_n" title="[b]_n" style="vertical-align: -5px; border: none;"/> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-733610989b53788a6f6eb587fc0eb955.gif" alt="[a]_n[b]_n=[1]_n" title="[a]_n[b]_n=[1]_n" style="vertical-align: -5px; border: none;"/>. Therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-12773a69756832e569ee0b017be8b7ef.gif" alt="ab\equiv 1(mod n)" title="ab\equiv 1(mod n)" style="vertical-align: -4px; border: none;"/>, which implies that for some <tt class="docutils literal"><span class="pre">q</span></tt>, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3efad800451f2ff8ce5efda715bb7cf3.gif" alt="ab=1+qn" title="ab=1+qn" style="vertical-align: -4px; border: none;"/>. Thus <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2f8d87a8e39394bab684a4241553d3f2.gif" alt="ab+(-q)n=1" title="ab+(-q)n=1" style="vertical-align: -4px; border: none;"/>. So 1 is a linear combination of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">n</span></tt>. <a class="reference external" href="http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/">Therefore</a> <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1e9062a787a00e84d12f9c7c1f631b9c.gif" alt="(a,n)=1" title="(a,n)=1" style="vertical-align: -4px; border: none;"/>.<br />
On the other hand, if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1e9062a787a00e84d12f9c7c1f631b9c.gif" alt="(a,n)=1" title="(a,n)=1" style="vertical-align: -4px; border: none;"/>, there exist <tt class="docutils literal"><span class="pre">q</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a6b5539a8c2c82dfa2054ad86c2e2936.gif" alt="ab+qn=1" title="ab+qn=1" style="vertical-align: -4px; border: none;"/>, or <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-12773a69756832e569ee0b017be8b7ef.gif" alt="ab\equiv 1(mod n)" title="ab\equiv 1(mod n)" style="vertical-align: -4px; border: none;"/>, so <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-733610989b53788a6f6eb587fc0eb955.gif" alt="[a]_n[b]_n=[1]_n" title="[a]_n[b]_n=[1]_n" style="vertical-align: -5px; border: none;"/>.</p>
<p><strong>(IV)</strong> By definition, since every unit of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/> is coprime to <tt class="docutils literal"><span class="pre">n</span></tt>, the amount of units of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c4181b39fa51ad9a30657febc31540b2.gif" alt="\mathbb{Z}_n" title="\mathbb{Z}_n" style="vertical-align: -3px; border: none;"/> (or, the number of elements of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-97af68798b90b52ed28e9787302c11a3.gif" alt="\mathbb{Z}^{\times}_{n}" title="\mathbb{Z}^{\times}_{n}" style="vertical-align: -4px; border: none;"/>) is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-40ba55cd3c58225334c65204b80c6ca3.gif" alt="\varphi (n)" title="\varphi (n)" style="vertical-align: -4px; border: none;"/>.</p>
<p>Let&#8217;s keep this result in mind and prepare some more theorems in order to attack the proof.</p>
<p><strong>(V) Lagrange&#8217;s theorem:</strong> If <em>H</em> is a subgroup of the finite group <em>G</em>, then the order of <em>H</em> is a divisor of the order of <em>G</em>.</p>
<p>Proof: Let&#8217;s first define <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-63c8601794f0ada16e91d245f5ebe434.gif" alt="|G|=n" title="|G|=n" style="vertical-align: -5px; border: none;"/> and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-9b23d18e2ba1b6400fa17004c2bcf34f.gif" alt="|H|=m" title="|H|=m" style="vertical-align: -5px; border: none;"/>. Also, let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> be the equivalence relation defined in example (III) of the <a class="reference external" href="http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/">previous post</a>. Since it&#8217;s an equivalence relation, it partitions <em>G</em> into equivalence classes. Define <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-88164354652817ffee36d596a683a2d4.gif" alt="[a]" title="[a]" style="vertical-align: -5px; border: none;"/> as the equivalence class of <em>a</em> with <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/>, for any <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cfea15a8db96e0f4fbf52a172a6ad737.gif" alt="a\in G" title="a\in G" style="vertical-align: -1px; border: none;"/>.</p>
<p>To prove Lagrange&#8217;s theorem, we&#8217;re going to show that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-88164354652817ffee36d596a683a2d4.gif" alt="[a]" title="[a]" style="vertical-align: -5px; border: none;"/> has the same amount of elements as <em>H</em>. For this purpose, let&#8217;s define a function <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4e25e466e0877d3e9c0e883d626c4060.gif" alt="p_a:H\rightarrow [a]" title="p_a:H\rightarrow [a]" style="vertical-align: -5px; border: none;"/> by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7025268be26409633dda354caa4c0b63.gif" alt="p_a(x)=xa" title="p_a(x)=xa" style="vertical-align: -4px; border: none;"/> for all <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-9f18fd44308ae8059634770fc82424a0.gif" alt="x\in H" title="x\in H" style="vertical-align: -1px; border: none;"/> and prove that it&#8217;s an isomorphism. To do that, we&#8217;ll have to separately prove that it&#8217;s onto and one-to-one.</p>
<p>But first, let&#8217;s verify that the stated codomain of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a26f2c99a9402631298bab62ef752b21.gif" alt="p_a" title="p_a" style="vertical-align: -4px; border: none;"/> is correct. If <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3675c32b8a780f2e55bc4c046f1acfd3.gif" alt="h\in H" title="h\in H" style="vertical-align: -1px; border: none;"/> then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cda3ea7aa7d49c16e58e69c2aa67e522.gif" alt="p_a(h)=ha\in [a]" title="p_a(h)=ha\in [a]" style="vertical-align: -5px; border: none;"/> because <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-63a5dad7edbec1f05de27cbedd67864b.gif" alt="(ha)a^{-1}=h\in H" title="(ha)a^{-1}=h\in H" style="vertical-align: -4px; border: none;"/>, so by definition of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> we have <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d2ab13096f89785c650f19d2e4d9905b.gif" alt="a\sim ha" title="a\sim ha" style="vertical-align: 0px; border: none;"/>. So indeed the codomain of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a26f2c99a9402631298bab62ef752b21.gif" alt="p_a" title="p_a" style="vertical-align: -4px; border: none;"/> is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-88164354652817ffee36d596a683a2d4.gif" alt="[a]" title="[a]" style="vertical-align: -5px; border: none;"/>.</p>
<ol class="arabic simple">
<li>Let&#8217;s pick some <em>y</em> in <em>G</em> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-10cd463bb8e4dc5b6e822363e2f09c19.gif" alt=" y\sim a " title=" y\sim a " style="vertical-align: -4px; border: none;"/>. By definition of our <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> it means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f05d214afa7ad3f6bbc0c9d743494731.gif" alt="ya^{-1}=h" title="ya^{-1}=h" style="vertical-align: -4px; border: none;"/> for some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3675c32b8a780f2e55bc4c046f1acfd3.gif" alt="h\in H" title="h\in H" style="vertical-align: -1px; border: none;"/>. So <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1f749fc800a123abc60c5ee701fd8e66.gif" alt="p_a(x)=y" title="p_a(x)=y" style="vertical-align: -4px; border: none;"/> has a solution <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-9db38bb5171679dbaf65053e9e5f9594.gif" alt="x=h" title="x=h" style="vertical-align: 0px; border: none;"/> (since <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-022cc4d59d8f91ed6c9df9a53f0770fc.gif" alt="ha=(ya^{-1})a=y" title="ha=(ya^{-1})a=y" style="vertical-align: -4px; border: none;"/>). Therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a26f2c99a9402631298bab62ef752b21.gif" alt="p_a" title="p_a" style="vertical-align: -4px; border: none;"/> is onto.</li>
<li>Suppose that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4a49979a5f165f718be78e75cba89fb3.gif" alt="h,k\in H" title="h,k\in H" style="vertical-align: -4px; border: none;"/> with <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e01414a608583032be98cb2078d58eb7.gif" alt="p_a(h)=p_a(k)" title="p_a(h)=p_a(k)" style="vertical-align: -4px; border: none;"/>. Then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d063ccc605ee4db90e25d71c967f20d1.gif" alt="ha=hk" title="ha=hk" style="vertical-align: 0px; border: none;"/> and by cancellation in groups we have <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-735517b6b7dea167c3ada05e02b6958a.gif" alt="h=k" title="h=k" style="vertical-align: 0px; border: none;"/>, which proves that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a26f2c99a9402631298bab62ef752b21.gif" alt="p_a" title="p_a" style="vertical-align: -4px; border: none;"/> is one-to-one.</li>
</ol>
<p>So we&#8217;ve proved that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a26f2c99a9402631298bab62ef752b21.gif" alt="p_a" title="p_a" style="vertical-align: -4px; border: none;"/> is an isomorphism, which means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d081a0731d0068e5a74d4bdb6c709502.gif" alt="|[a]|=|H|" title="|[a]|=|H|" style="vertical-align: -5px; border: none;"/> (we can map each element of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-88164354652817ffee36d596a683a2d4.gif" alt="[a]" title="[a]" style="vertical-align: -5px; border: none;"/> to one and only one element of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-744c214c91ecd5f770c4443f4db58df8.gif" alt="|H|" title="|H|" style="vertical-align: -5px; border: none;"/>).</p>
<p>We&#8217;ve <a class="reference external" href="http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/">previously shown</a> that the equivalence classes of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> partition <em>G</em>. But now we see that the size of each equivalence class is equal to <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-744c214c91ecd5f770c4443f4db58df8.gif" alt="|H|" title="|H|" style="vertical-align: -5px; border: none;"/>. Therefore, all the equivalence classes are of the same size, and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4d9af282782126a0a6b55e272f0d9209.gif" alt="n=mt" title="n=mt" style="vertical-align: 0px; border: none;"/> where <em>t</em> is the amount of equivalence classes. This proves Lagrange&#8217;s theorem.</p>
<p>We&#8217;re almost there. To see how all of this relates to Euler&#8217;s theorem, let&#8217;s first define the order of an element of a group.</p>
<p><strong>(VI) Order of group element:</strong> Let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cfea15a8db96e0f4fbf52a172a6ad737.gif" alt="a\in G" title="a\in G" style="vertical-align: -1px; border: none;"/>. If there exists a positive integer <em>n</em> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e5bf1704317a21d03373bdd51124e57a.gif" alt="a^n=e" title="a^n=e" style="vertical-align: 0px; border: none;"/>, then a is said to have <strong>finite order</strong> and the smallest such positive integer is called the <strong>order</strong> of <em>a</em>, denoted by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4cf0b97dc00df35b0e15d6813bbab744.gif" alt="o(a)" title="o(a)" style="vertical-align: -4px; border: none;"/>.</p>
<p>We&#8217;ll also define the subgroup <strong>generated</strong> by an element:</p>
<p><strong>(VII) Cyclic subgroup:</strong> <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d2dc6124bc99a90303f03d1554ea3343.gif" alt="\langle a \rangle =\{a^k:k\in \mathbb{Z}\}" title="\langle a \rangle =\{a^k:k\in \mathbb{Z}\}" style="vertical-align: -5px; border: none;"/> is a cyclic subgroup of <em>G</em> generated by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cfea15a8db96e0f4fbf52a172a6ad737.gif" alt="a\in G" title="a\in G" style="vertical-align: -1px; border: none;"/>. For a finite <em>G</em> this subgroup is also finite, and its size is: <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5239b3ebc3872153fcadea41c9941d77.gif" alt="|\langle a \rangle| = o(a)" title="|\langle a \rangle| = o(a)" style="vertical-align: -5px; border: none;"/>.</p>
<p>Armed with these definitions, we&#8217;re now ready for the following corollary of Lagrange&#8217;s theorem:</p>
<p><strong>(VIII) Lagrange theorem corollary:</strong> Let <em>G</em> be a finite group of order <em>n</em>. Then:</p>
<ol class="arabic simple">
<li>For any <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cfea15a8db96e0f4fbf52a172a6ad737.gif" alt="a\in G" title="a\in G" style="vertical-align: -1px; border: none;"/>, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4cf0b97dc00df35b0e15d6813bbab744.gif" alt="o(a)" title="o(a)" style="vertical-align: -4px; border: none;"/> divides <em>n</em></li>
<li>For any <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cfea15a8db96e0f4fbf52a172a6ad737.gif" alt="a\in G" title="a\in G" style="vertical-align: -1px; border: none;"/>, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e5bf1704317a21d03373bdd51124e57a.gif" alt="a^n=e" title="a^n=e" style="vertical-align: 0px; border: none;"/></li>
</ol>
<p>Proof: As we&#8217;ve seen, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5239b3ebc3872153fcadea41c9941d77.gif" alt="|\langle a \rangle| = o(a)" title="|\langle a \rangle| = o(a)" style="vertical-align: -5px; border: none;"/> and by Lagrange&#8217;s theorem <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5ea88fac660b636a8a7b7cad3e605ff4.gif" alt="|\langle a \rangle|" title="|\langle a \rangle|" style="vertical-align: -5px; border: none;"/> divides <em>n</em> (since <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-062314ee4515e21c160b657d3f3763b0.gif" alt="\langle a \rangle" title="\langle a \rangle" style="vertical-align: -5px; border: none;"/> is a subgroup of <em>G</em>). Therefore (1) is proven.<br />
For (2), note that if <em>a</em> has order <em>m</em>, then by (1) we have <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d447fb92251d71c0e4d8b7a52afd445c.gif" alt="n=mq" title="n=mq" style="vertical-align: -4px; border: none;"/> for some integer <em>q</em>. Thus <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-97e3d3e266197b206a1e340a3771c08d.gif" alt="a^n=a^{mq}=(a^m)^q" title="a^n=a^{mq}=(a^m)^q" style="vertical-align: -4px; border: none;"/>. But <em>a</em> has order <em>m</em>, so <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5caf1f1eaccba959a7cfb6d6bab5c82b.gif" alt="a^m=e" title="a^m=e" style="vertical-align: 0px; border: none;"/> and therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-01c68bfdec7583f3f3707d24adabe03a.gif" alt="(a^m)^q=e" title="(a^m)^q=e" style="vertical-align: -4px; border: none;"/>. <em>Q.E.D.</em></p>
<p>We now finally have all the tools required to prove Euler&#8217;s theorem.</p>
<p>Proof of Euler&#8217;s theorem: Let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-48be1c9570d04f4878c8dd84b699abc3.gif" alt="G=\mathbb{Z}^{\times}_{n}" title="G=\mathbb{Z}^{\times}_{n}" style="vertical-align: -4px; border: none;"/> the group of units modulo <em>n</em>. The order of <em>G</em> is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-40ba55cd3c58225334c65204b80c6ca3.gif" alt="\varphi (n)" title="\varphi (n)" style="vertical-align: -4px; border: none;"/> (by <strong>(IV)</strong>). Now, by <strong>(VIII)</strong> part (2), raising any congruence class to the power <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-40ba55cd3c58225334c65204b80c6ca3.gif" alt="\varphi (n)" title="\varphi (n)" style="vertical-align: -4px; border: none;"/> must give the identity element. The statement <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cb80325ffb2b707c7d96a32189f3aced.gif" alt="[a]^{\varphi (n)} = [1]" title="[a]^{\varphi (n)} = [1]" style="vertical-align: -5px; border: none;"/> is equivalent to <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f030e0e97fb8232cbad893370db01b6f.gif" alt="a^{\varphi (n)}\equiv 1(mod\: n)" title="a^{\varphi (n)}\equiv 1(mod\: n)" style="vertical-align: -4px; border: none;"/></p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-961e8cd3f4377a451a42a4c0dea2ae9e.gif" alt="Q.E.D." title="Q.E.D." style="vertical-align: -4px; border: none;"/></p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1787&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/' rel='bookmark' title='Permanent Link: Equivalence classes and group partitions'>Equivalence classes and group partitions</a> <small>In this post I want to show some interesting definitions...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2008/01/17/pythagorean-the-theorem-with-most-proofs/' rel='bookmark' title='Permanent Link: Pythagorean &#8211; the theorem with most proofs ?'>Pythagorean &#8211; the theorem with most proofs ?</a> <small>This page shows 76 different proofs of the Pythagorean theorem....</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Equivalence classes and group partitions</title>
		<link>http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/</link>
		<comments>http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 13:47:57 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1809</guid>
		<description><![CDATA[In this post I want to show some interesting definitions and theorems about equivalence relations &#38; classes, and groups.
Relations are an important topic in algebra. Conceptually, a relation is a statement aRb about two elements of a set. If the elements are integers, then  is a relation, and so is .
Here&#8217;s a formal set-theoretic [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle/' rel='bookmark' title='Permanent Link: The well-ordering principle'>The well-ordering principle</a> <small>The well-ordering principle states: The well-ordering principle: Any nonempty set...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>In this post I want to show some interesting definitions and theorems about equivalence relations &amp; classes, and groups.</p>
<p><em>Relations</em> are an important topic in algebra. Conceptually, a relation is a statement <tt class="docutils literal"><span class="pre">aRb</span></tt> about two elements of a set. If the elements are integers, then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7acaac15494e6820b1ed6d8b539af089.gif" alt="a=b" title="a=b" style="vertical-align: 0px; border: none;"/> is a relation, and so is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-edf54efd8e6c13aa1b6cae8c741185a8.gif" alt="a\geq b" title="a\geq b" style="vertical-align: -3px; border: none;"/>.</p>
<p>Here&#8217;s a formal set-theoretic definition:</p>
<p><strong>(I) Binary relation:</strong> A <em>binary relation</em> on a set <tt class="docutils literal"><span class="pre">A</span></tt> is a collection of ordered pairs of elements of <tt class="docutils literal"><span class="pre">A</span></tt>. In other words, it is the subset of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e5b68f3bd8385ca80c10e2fbb6d899ca.gif" alt="A\times A" title="A\times A" style="vertical-align: 0px; border: none;"/>. More generally, a binary relation between two sets <tt class="docutils literal"><span class="pre">A</span></tt> and <tt class="docutils literal"><span class="pre">B</span></tt> is a subset of <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-27a7579d0309d06241abeb0bccb4ac99.gif" alt="A\times B" title="A\times B" style="vertical-align: 0px; border: none;"/>.</p>
<p>Note it says <em>ordered pairs</em>. What this means is that the order of elements in a relation is important. Intuitively, given the relation <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d9362c6490cded6ecd93e0bf42a25bb.gif" alt="\geq" title="\geq" style="vertical-align: -3px; border: none;"/>  and the set of integers, it&#8217;s clear that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-edf54efd8e6c13aa1b6cae8c741185a8.gif" alt="a\geq b" title="a\geq b" style="vertical-align: -3px; border: none;"/> does not generally imply <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-37edeae14ff3db2d9bd0c72aae90101c.gif" alt="b\geq a" title="b\geq a" style="vertical-align: -3px; border: none;"/>.</p>
<p>An important sub-class of relations we&#8217;ll be most interested with is the <em>equivalence relations</em>:</p>
<p><strong>(II) Equivalence relation:</strong> A relation <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> on a set is called an <em>equivalence relation</em> if it&#8217;s reflexive, symmetric and transitive:</p>
<ul class="simple">
<li>Reflexive: for all <tt class="docutils literal"><span class="pre">a</span></tt> in <tt class="docutils literal"><span class="pre">S</span></tt> it holds that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bb51275e255dc86a0d3462ecaadc672c.gif" alt="a\sim a" title="a\sim a" style="vertical-align: 0px; border: none;"/>.</li>
<li>Symmetric: for all <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> in <tt class="docutils literal"><span class="pre">S</span></tt> it holds that if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-05073a04fe1376c3b0c45106273f9187.gif" alt="a\sim b" title="a\sim b" style="vertical-align: 0px; border: none;"/> then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-85082dfd8e22327296316020a3e74086.gif" alt="b\sim a" title="b\sim a" style="vertical-align: 0px; border: none;"/>.</li>
<li>Transitive: for all <tt class="docutils literal"><span class="pre">a</span></tt>, <tt class="docutils literal"><span class="pre">b</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt> in <tt class="docutils literal"><span class="pre">S</span></tt> it holds that if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-05073a04fe1376c3b0c45106273f9187.gif" alt="a\sim b" title="a\sim b" style="vertical-align: 0px; border: none;"/> and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d85a5ae73edc36feaca9d15bc8ef034.gif" alt="b\sim c" title="b\sim c" style="vertical-align: 0px; border: none;"/> then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3bc71c72d4aeb3dc955cef789d49e966.gif" alt="a\sim c" title="a\sim c" style="vertical-align: 0px; border: none;"/>.</li>
</ul>
<p>Examples: equality is an equivalence relation, but greater-or-equal is not, as <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-edf54efd8e6c13aa1b6cae8c741185a8.gif" alt="a\geq b" title="a\geq b" style="vertical-align: -3px; border: none;"/> doesn&#8217;t imply <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-37edeae14ff3db2d9bd0c72aae90101c.gif" alt="b\geq a" title="b\geq a" style="vertical-align: -3px; border: none;"/> &#8211; the symmetric condition doesn&#8217;t hold.</p>
<p>Another important equivalence relation is the congruence modulo an integer, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-371f6ef708626cee9ae2247d373c3d58.gif" alt="a\equiv b\: (mod n)" title="a\equiv b\: (mod n)" style="vertical-align: -4px; border: none;"/>.</p>
<p><strong>(III) Example:</strong> Let <tt class="docutils literal"><span class="pre">G</span></tt> be a group and <tt class="docutils literal"><span class="pre">H</span></tt> a subgroup of <tt class="docutils literal"><span class="pre">G</span></tt>. For <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4825820b2a257ae47442af0f56d47570.gif" alt="a,b\in G" title="a,b\in G" style="vertical-align: -4px; border: none;"/>, define <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-05073a04fe1376c3b0c45106273f9187.gif" alt="a\sim b" title="a\sim b" style="vertical-align: 0px; border: none;"/> if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1d7ab7883c461ea8e5ff31c595578e22.gif" alt="ab^{-1}\in H" title="ab^{-1}\in H" style="vertical-align: -1px; border: none;"/>. Then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> is an equivalence relation on <tt class="docutils literal"><span class="pre">G</span></tt>.</p>
<p>Proof: To prove that some relation is an equivalence relation, we have to prove the three properties of equivalence relations.</p>
<p>Reflexive: <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-58f2555cfb26745d39639636e0aaf37d.gif" alt="aa^{-1}" title="aa^{-1}" style="vertical-align: 0px; border: none;"/> is the identity element <tt class="docutils literal"><span class="pre">e</span></tt>. However, since <tt class="docutils literal"><span class="pre">H</span></tt> is a subgroup of <tt class="docutils literal"><span class="pre">G</span></tt>, it means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7d57fa3e5d5f682bbd2bb8efef97d5e5.gif" alt="e\in H" title="e\in H" style="vertical-align: -1px; border: none;"/>. Therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3f9ce50221b8f718c891a3ca9e3a936d.gif" alt="aa^{-1}\in H" title="aa^{-1}\in H" style="vertical-align: -1px; border: none;"/>, so <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bb51275e255dc86a0d3462ecaadc672c.gif" alt="a\sim a" title="a\sim a" style="vertical-align: 0px; border: none;"/>.</p>
<p>Symmetric: Assume that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1d7ab7883c461ea8e5ff31c595578e22.gif" alt="ab^{-1}\in H" title="ab^{-1}\in H" style="vertical-align: -1px; border: none;"/>. Since <tt class="docutils literal"><span class="pre">H</span></tt> is a subgroup, then this element has an inverse in <tt class="docutils literal"><span class="pre">H</span></tt>: <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-520c88c9f1ac012bd52d2d07bc1d7fd9.gif" alt="(ab^{-1})^{-1}\in H" title="(ab^{-1})^{-1}\in H" style="vertical-align: -4px; border: none;"/>. Using the associative law of groups several times it&#8217;s possible to show that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1a7ef26b7a58c5b37687cd94273f41e0.gif" alt="(ab^{-1})^{-1}=(b^{-1})^{-1}a^{-1}=ba^{-1}" title="(ab^{-1})^{-1}=(b^{-1})^{-1}a^{-1}=ba^{-1}" style="vertical-align: -4px; border: none;"/><br />
So, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-30f0796d684b9c6c40e95969d144331b.gif" alt="ba^{-1}\in H" title="ba^{-1}\in H" style="vertical-align: -1px; border: none;"/>, hence <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-85082dfd8e22327296316020a3e74086.gif" alt="b\sim a" title="b\sim a" style="vertical-align: 0px; border: none;"/>.</p>
<p>Transitive: Given <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1d7ab7883c461ea8e5ff31c595578e22.gif" alt="ab^{-1}\in H" title="ab^{-1}\in H" style="vertical-align: -1px; border: none;"/> and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e10c1426a2ec5f44704385db2e49e1c4.gif" alt="bc^{-1}\in H" title="bc^{-1}\in H" style="vertical-align: -1px; border: none;"/>.<br />
<img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-388ad4ec6b605da300e1007348ad754e.gif" alt="(ab^{-1})(bc^{-1})=ac^{-1}" title="(ab^{-1})(bc^{-1})=ac^{-1}" style="vertical-align: -4px; border: none;"/> So, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c7c49806799862f8441f40947bb46f88.gif" alt="ac^{-1}\in H" title="ac^{-1}\in H" style="vertical-align: -1px; border: none;"/>, hence <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3bc71c72d4aeb3dc955cef789d49e966.gif" alt="a\sim c" title="a\sim c" style="vertical-align: 0px; border: none;"/>.</p>
<p>Thus, we&#8217;ve proved that this <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> is an equivalence relation. Let&#8217;s see a concrete application of the result we&#8217;ve just proved:</p>
<p>Consider that if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-8d9a442b5c004413f3e5304e03e5ec1b.gif" alt="G=\mathbb{Z}" title="G=\mathbb{Z}" style="vertical-align: 0px; border: none;"/> with the operation <tt class="docutils literal"><span class="pre">+</span></tt>, and <tt class="docutils literal"><span class="pre">H</span></tt> is the subgroup consisting of all multiples of some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-eb380f3b2439960f7727e82712b46659.gif" alt="n>1" title="n>1" style="vertical-align: -1px; border: none;"/>, then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-1d7ab7883c461ea8e5ff31c595578e22.gif" alt="ab^{-1}\in H" title="ab^{-1}\in H" style="vertical-align: -1px; border: none;"/> actually means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-60d6d486b6fff7806cc4d4f036b75265.gif" alt="a+(-b)=kn" title="a+(-b)=kn" style="vertical-align: -4px; border: none;"/> for some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e4af3ae476d7990de97c92fe7c7e16c4.gif" alt="k\in \mathbb{Z}" title="k\in \mathbb{Z}" style="vertical-align: -1px; border: none;"/>. In other words <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-371f6ef708626cee9ae2247d373c3d58.gif" alt="a\equiv b\: (mod n)" title="a\equiv b\: (mod n)" style="vertical-align: -4px; border: none;"/>. This proves that congruence modulo <tt class="docutils literal"><span class="pre">n</span></tt> is an equivalence relation, since it&#8217;s a special case of (III).</p>
<p><strong>(IV) Equivalence class:</strong> If <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> is an equivalence relation on <tt class="docutils literal"><span class="pre">S</span></tt>, then <tt class="docutils literal"><span class="pre">[a]</span></tt>, the <em>equivalence class of a</em> is defined by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-745ac71989fff73a05adad42aab3e8b6.gif" alt="[a] = \{b\in S|b\sim a\}" title="[a] = \{b\in S|b\sim a\}" style="vertical-align: -5px; border: none;"/></p>
<p>For example, let&#8217;s take the integers <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-812b3f5a051a22b060fbc64deea076db.gif" alt="\mathbb{Z}" title="\mathbb{Z}" style="vertical-align: 0px; border: none;"/> and define an equivalence relation &quot;congruent modulo 5&quot;. For instance, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6fc4ecdc85a9ddf4f830da64a62ed873.gif" alt="1\sim 6" title="1\sim 6" style="vertical-align: -1px; border: none;"/>. The congruence class of 1 modulo 5 (denoted <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c7b538e2e8b75a83fc0d95ec43d47b9a.gif" alt="[1]_5" title="[1]_5" style="vertical-align: -5px; border: none;"/>) is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c5fca11e7c837c03baa19ebe1723a7b4.gif" alt="\{\mathellipsis, -9, -4, 1, 6, 11, \mathellipsis\}" title="\{\mathellipsis, -9, -4, 1, 6, 11, \mathellipsis\}" style="vertical-align: -5px; border: none;"/>.</p>
<p><strong>(V) Group partition:</strong> If <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> is an equivalence relation on <tt class="docutils literal"><span class="pre">S</span></tt>, then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f73dd090e6958835ff560b55538c0765.gif" alt="S=\bigcup [a]" title="S=\bigcup [a]" style="vertical-align: -5px; border: none;"/> for all <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6a9ad2adbb2e75baca075ea3fad9caff.gif" alt="a\in S" title="a\in S" style="vertical-align: -1px; border: none;"/>, and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-cbb2fa9f4aa73b354d494a47c4af64be.gif" alt="[a]\ne [b]" title="[a]\ne [b]" style="vertical-align: -5px; border: none;"/> implies that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-41c5f9feb34e645a85653d47b6ab73ec.gif" alt="[a]\cap [b]=\varnothing" title="[a]\cap [b]=\varnothing" style="vertical-align: -5px; border: none;"/>. In other words, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> partitions <tt class="docutils literal"><span class="pre">S</span></tt> into disjoint equivalence classes.</p>
<p>Proof: the first part is easy. Since always <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c66fc5947405b971cc2132c102bec9e0.gif" alt="a\in [a]" title="a\in [a]" style="vertical-align: -5px; border: none;"/>, then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6e221b5fb31e10eb08bb53ca1b07cd56.gif" alt="\bigcup_{a\in S}[a]=S" title="\bigcup_{a\in S}[a]=S" style="vertical-align: -6px; border: none;"/>. To prove the second part, we&#8217;ll show that if <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3b733b75bb6d8f7a1963d14b90d1abf4.gif" alt="[a]\cap [b]\ne \varnothing" title="[a]\cap [b]\ne \varnothing" style="vertical-align: -5px; border: none;"/> then <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-59cf3abe8a280a0de4a9dd1cfa304603.gif" alt="[a]=[b]" title="[a]=[b]" style="vertical-align: -5px; border: none;"/>.</p>
<p>Suppose that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3b733b75bb6d8f7a1963d14b90d1abf4.gif" alt="[a]\cap [b]\ne \varnothing" title="[a]\cap [b]\ne \varnothing" style="vertical-align: -5px; border: none;"/>, and let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7ec586a72b3e5a59e52d75a8fb5c205c.gif" alt="c\in [a]\cap [b]" title="c\in [a]\cap [b]" style="vertical-align: -5px; border: none;"/>. Therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d8c6b19c158015e909959f0aed060dee.gif" alt="c\sim a" title="c\sim a" style="vertical-align: 0px; border: none;"/> and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4a1e524ebd5a3ffb0c0fea3ac2227a97.gif" alt="c\sim b" title="c\sim b" style="vertical-align: 0px; border: none;"/>. But <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-6588c95074f2609674f5fe10ab63f88f.gif" alt="\sim" title="\sim" style="vertical-align: 2px; border: none;"/> is an equivalence relation and thus is transitive and symmetric. So <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-05073a04fe1376c3b0c45106273f9187.gif" alt="a\sim b" title="a\sim b" style="vertical-align: 0px; border: none;"/>. But this means that <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> are in the same equivalence class: <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-59cf3abe8a280a0de4a9dd1cfa304603.gif" alt="[a]=[b]" title="[a]=[b]" style="vertical-align: -5px; border: none;"/>. <em>Q.E.D.</em></p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1809&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle/' rel='bookmark' title='Permanent Link: The well-ordering principle'>The well-ordering principle</a> <small>The well-ordering principle states: The well-ordering principle: Any nonempty set...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detexify recognizes hand-written math symbols</title>
		<link>http://eli.thegreenplace.net/2009/07/13/detexify-recognizes-hand-written-math-symbols/</link>
		<comments>http://eli.thegreenplace.net/2009/07/13/detexify-recognizes-hand-written-math-symbols/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 03:31:16 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1800</guid>
		<description><![CDATA[Does it ever happen to you that you don&#8217;t remember the Latex code for some mathematical symbol? What can you do then except wading through pages of Latex symbols trying to locate the right one? 
Well, no more! Detexify is a great new service that allows you to &#8220;draw&#8221; the symbol you&#8217;re looking for:

&#8230; and [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Does it ever happen to you that you don&#8217;t remember the Latex code for some mathematical symbol? What can you do then except wading through pages of Latex symbols trying to locate the right one? </p>
<p>Well, no more! <a href="http://detexify.kirelabs.org/classify.html">Detexify</a> is a great new service that allows you to &#8220;draw&#8221; the symbol you&#8217;re looking for:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/uploads/2009/07/intg_handwriting.png" alt="intg_handwriting" title="intg_handwriting" width="301" height="366" class="alignnone size-full wp-image-1801" /></p>
<p>&#8230; and it will suggest the Latex code. </p>
<p><img src="http://eli.thegreenplace.net/wp-content/uploads/2009/07/intg_suggestions.png" alt="intg_suggestions" title="intg_suggestions" width="287" height="310" class="alignnone size-full wp-image-1802" /></p>
<p>Detexify is a learning OCR classifier, and can be &#8220;trained&#8221; by users to improve it&#8217;s performance. </p>
<p>Kudos to the <a href="http://kirelabs.org/">creator</a> of Detexify for a great project. It will definitely be useful&#8230;</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1800&type=feed" alt="" />

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/13/detexify-recognizes-hand-written-math-symbols/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Generating multi-subsets using arithmetic</title>
		<link>http://eli.thegreenplace.net/2009/07/11/generating-multi-subsets-using-arithmetic/</link>
		<comments>http://eli.thegreenplace.net/2009/07/11/generating-multi-subsets-using-arithmetic/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 05:27:13 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1784</guid>
		<description><![CDATA[In the past I&#8217;ve written about how simple arithmetic can be employed to compute a powerset of a given set.
Here I want to show a generalization, that uses n-nary arithmetic. But first, let&#8217;s define the problem:
Suppose you have a set of elements and you want to select multi-subsets from it. By multi-subset in this context [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li><li><a href='http://eli.thegreenplace.net/2003/07/23/allocating-multi-dimensional-arrays-in-c/' rel='bookmark' title='Permanent Link: Allocating multi-dimensional arrays in C++'>Allocating multi-dimensional arrays in C++</a> <small>Updated on 04.06.2010 Allocating multi-dimensional arrays in C++ (and C)...</small></li><li><a href='http://eli.thegreenplace.net/2010/01/28/generating-random-sentences-from-a-context-free-grammar/' rel='bookmark' title='Permanent Link: Generating random sentences from a context free grammar'>Generating random sentences from a context free grammar</a> <small>Sometimes it&#8217;s interesting to randomly generate a large amount of...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>In the past <a class="reference external" href="http://eli.thegreenplace.net/2005/03/29/application-of-combinations/">I&#8217;ve written</a> about how simple arithmetic can be employed to compute a powerset of a given set.</p>
<p>Here I want to show a generalization, that uses n-nary arithmetic. But first, let&#8217;s define the problem:</p>
<p>Suppose you have a set of elements and you want to select multi-subsets from it. By multi-subset in this context I mean that an element can appear more than once in it. For example, given the set {0, 1, 2, 3, 4, 5}, then {1, 1, 2} is a multi-subset. So are {5, 5, 5, 5} and {0, 1, 2, 3, 4, 5}. Suppose you want to go over <em>all</em> multi-subsets of a set. How can this be done?</p>
<p>Note that generating a superset is a private case of this problem, restricting each element to appear either 0 or 1 times in the resulting subset.</p>
<p>So the solution is a generalization of the <a class="reference external" href="http://eli.thegreenplace.net/2005/03/29/application-of-combinations/">binary-arithmetic solution</a> for the powerset problem.</p>
<p>Intuitive motivation: consider the decimal numbers, for example 25. If we use the position of each digit (starting with the units) to convey information, this leads to an interesting observation. If we have two elements to choose from, 25 may mean 5 times the 1st element, 2 times the second element. Now, going over all numbers from 0 to 99, we are actually generating all multi-subsets of two elements where each can be picked from 0 to 9 times.</p>
<p>Once this is clear, the algorithm is simple. Let&#8217;s generalize to a n-ary base system, using position to point to an element and the &#8216;digit&#8217; at this position to say how many times it appears in a given multi-subset. And the best part &#8211; the simple rules of addition with carry can now be used to efficiently generate all multi-subsets, given the amount of elements we have (<tt class="docutils literal"><span class="pre">length</span></tt>) and the maximal amount of times each can be picked (<tt class="docutils literal"><span class="pre">upto</span></tt>), the minimum being assumed 0.</p>
<p>Here&#8217;s the code:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">multiselects</span>(upto, length):
  <span style="color: #007f00"># Arithmetically, we create an array of digits</span>
  <span style="color: #007f00"># (each in the range 0..upto).</span>
  <span style="color: #007f00"># It&#39;s initialized with &#39;1&#39;</span>
  <span style="color: #007f00">#</span>
  ar = [<span style="color: #007f7f">1</span>] + [<span style="color: #007f7f">0</span>] * (length - <span style="color: #007f7f">1</span>)

  <span style="color: #00007f; font-weight: bold">while</span> <span style="color: #00007f">True</span>:
      <span style="color: #00007f; font-weight: bold">yield</span> ar

      <span style="color: #007f00"># The index we&#39;re currently trying to</span>
      <span style="color: #007f00"># advance</span>
      <span style="color: #007f00">#</span>
      idx = <span style="color: #007f7f">0</span>

      <span style="color: #007f00"># Advance the current index. If it reaches</span>
      <span style="color: #007f00"># the limit (upto), pefrorm a carry to the</span>
      <span style="color: #007f00"># next index (digits)</span>
      <span style="color: #007f00">#</span>
      <span style="color: #00007f; font-weight: bold">while</span> idx &lt; length:
          ar[idx] += <span style="color: #007f7f">1</span>
          <span style="color: #00007f; font-weight: bold">if</span> ar[idx] &lt;= upto:
              <span style="color: #00007f; font-weight: bold">break</span>
          <span style="color: #00007f; font-weight: bold">else</span>:
              ar[idx] = <span style="color: #007f7f">0</span>
              idx += <span style="color: #007f7f">1</span>

      <span style="color: #007f00"># We&#39;ve reached the last number...</span>
      <span style="color: #007f00">#</span>
      <span style="color: #00007f; font-weight: bold">if</span> idx == length:
          <span style="color: #00007f; font-weight: bold">break</span>
</pre>
</div>
<p>An an example run of:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">for</span> s <span style="color: #0000aa">in</span> multiselects(<span style="color: #007f7f">2</span>, <span style="color: #007f7f">3</span>):
    <span style="color: #00007f; font-weight: bold">print</span> s
</pre>
</div>
<p>Produces:</p>
<div class="highlight">
<pre>[1, 0, 0]
[2, 0, 0]
[0, 1, 0]
[1, 1, 0]
[2, 1, 0]
[0, 2, 0]
[1, 2, 0]
[2, 2, 0]
[0, 0, 1]
[1, 0, 1]
[2, 0, 1]
[0, 1, 1]
[1, 1, 1]
[2, 1, 1]
[0, 2, 1]
[1, 2, 1]
[2, 2, 1]
[0, 0, 2]
[1, 0, 2]
[2, 0, 2]
[0, 1, 2]
[1, 1, 2]
[2, 1, 2]
[0, 2, 2]
[1, 2, 2]
[2, 2, 2]
</pre>
</div>
<p>Note that the solution is general, as the lists it returns are lists of indices. These can be employed with any set to generate multi-subsets.</p>
<p><strong>Background and links</strong></p>
<p>I came up with this function while working on Project Euler&#8217;s problem 77. I ended up using a different method, but visualizing the possible partitions of primes was very useful.</p>
<p>Here are some interesting mathematical links related to this problem:</p>
<ul class="simple">
<li><a class="reference external" href="http://mathworld.wolfram.com/EulerTransform.html">Euler transform</a></li>
<li><a class="reference external" href="http://mathworld.wolfram.com/PartitionFunctionP.html">Partition function P</a></li>
<li><a class="reference external" href="http://mathworld.wolfram.com/PrimePartition.html">Prime partition</a></li>
</ul>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1784&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li><li><a href='http://eli.thegreenplace.net/2003/07/23/allocating-multi-dimensional-arrays-in-c/' rel='bookmark' title='Permanent Link: Allocating multi-dimensional arrays in C++'>Allocating multi-dimensional arrays in C++</a> <small>Updated on 04.06.2010 Allocating multi-dimensional arrays in C++ (and C)...</small></li><li><a href='http://eli.thegreenplace.net/2010/01/28/generating-random-sentences-from-a-context-free-grammar/' rel='bookmark' title='Permanent Link: Generating random sentences from a context free grammar'>Generating random sentences from a context free grammar</a> <small>Sometimes it&#8217;s interesting to randomly generate a large amount of...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/11/generating-multi-subsets-using-arithmetic/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The GCD and linear combinations</title>
		<link>http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/</link>
		<comments>http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 05:49:39 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1763</guid>
		<description><![CDATA[A linear combination of a and b is some integer of the form , where .
There&#8217;s a very interesting theorem that gives a useful connection between linear combinations and the GCD of a and b, called Bézout&#8217;s identity:
Bézout&#8217;s identity:  (the GCD of a and b) is the smallest positive linear combination of non-zero a [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li><li><a href='http://eli.thegreenplace.net/2005/01/21/mathematical-musing/' rel='bookmark' title='Permanent Link: mathematical musing'>mathematical musing</a> <small>This morning while washing the dishes I was contemplating a...</small></li><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>A linear combination of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> is some integer of the form <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f833285e71d36915b97317a6c7632040.gif" alt="ma+nb" title="ma+nb" style="vertical-align: -1px; border: none;"/>, where <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5ebbb1a8c3f8da088733148134a0bf72.gif" alt="m,n\in \mathbb{Z}" title="m,n\in \mathbb{Z}" style="vertical-align: -4px; border: none;"/>.</p>
<p>There&#8217;s a very interesting theorem that gives a useful connection between linear combinations and the GCD of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>, called <a class="reference external" href="http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity">Bézout&#8217;s identity</a>:</p>
<p><strong>Bézout&#8217;s identity:</strong> <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d05e1f15387f87456155cd96cc06235.gif" alt="(a,b)" title="(a,b)" style="vertical-align: -4px; border: none;"/> (the GCD of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>) is the smallest positive linear combination of non-zero <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>.</p>
<p>Both Bézout&#8217;s identity and its corollary I show below are very useful tools in elementary number theory, being used for the proofs of many of the most fundamental theorems. Let&#8217;s see why it&#8217;s true.</p>
<p><strong>(I) Intuition:</strong> First I&#8217;d like to explain this (surprising at first sight) theorem intuitively. By defintion, any common divisor of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> will divide <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f833285e71d36915b97317a6c7632040.gif" alt="ma+nb" title="ma+nb" style="vertical-align: -1px; border: none;"/> for all <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5ebbb1a8c3f8da088733148134a0bf72.gif" alt="m,n\in \mathbb{Z}" title="m,n\in \mathbb{Z}" style="vertical-align: -4px; border: none;"/>. In particular, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d05e1f15387f87456155cd96cc06235.gif" alt="(a,b)" title="(a,b)" style="vertical-align: -4px; border: none;"/> also divides any <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f833285e71d36915b97317a6c7632040.gif" alt="ma+nb" title="ma+nb" style="vertical-align: -1px; border: none;"/>.</p>
<p>Now, assume we&#8217;ve found some small <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-4620cf3489510b47807045b2e1ab5827.gif" alt="x=ma+nb" title="x=ma+nb" style="vertical-align: -1px; border: none;"/> which isn&#8217;t the GCD. But we&#8217;ve just said that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d05e1f15387f87456155cd96cc06235.gif" alt="(a,b)" title="(a,b)" style="vertical-align: -4px; border: none;"/> divides all linear combinations, so it also divides <tt class="docutils literal"><span class="pre">x</span></tt>. Therefore, <tt class="docutils literal"><span class="pre">x</span></tt> can not be smaller than the GCD. In other words, the smallest positive linear combination can only be <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d05e1f15387f87456155cd96cc06235.gif" alt="(a,b)" title="(a,b)" style="vertical-align: -4px; border: none;"/> itself.</p>
<p><strong>Corollary:</strong> An integer is a linear combination of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> IFF it is a multiple of their GCD.</p>
<p>To prove Bézout&#8217;s identity more formally, and along the way to see why the corollary is also true, let&#8217;s first prove the following:</p>
<p><strong>(III)</strong> Let <tt class="docutils literal"><span class="pre">I</span></tt> be a nonempty set of integers that is closed under addition and subtraction, and contains at least one non-zero integer. Then there exists a smallest positive element <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f66ce0142128fc4f10f4bc9dbf3aea2e.gif" alt="b\in I" title="b\in I" style="vertical-align: -1px; border: none;"/>, and <tt class="docutils literal"><span class="pre">I</span></tt> consists of all multiples of <tt class="docutils literal"><span class="pre">b</span></tt> (<img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-90ec31e269b753a59bf0cbedfccd4e90.gif" alt="I=b\mathbb{Z}" title="I=b\mathbb{Z}" style="vertical-align: 0px; border: none;"/>).</p>
<p>Proof: <tt class="docutils literal"><span class="pre">I</span></tt> contains at least one non-zero integer. Then it definitely contains at least one positive integer, because it is closed under addition and subtraction. Assume we have <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-abef24526d3a709bf207e0f4b72c86ee.gif" alt="a\in I" title="a\in I" style="vertical-align: -1px; border: none;"/> for some <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-66e4644fdc425de4d89ce90073d57adf.gif" alt="0>a" title="0>a" style="vertical-align: 0px; border: none;"/>. Therefore <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e6a589c17b1536f23a6f8ca6010fced9.gif" alt="a-a=0\in I" title="a-a=0\in I" style="vertical-align: -1px; border: none;"/> and then also <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-8416b88396c2d1f7f9379caba667989b.gif" alt="0-a=-a\in I" title="0-a=-a\in I" style="vertical-align: -1px; border: none;"/>. Thus we have positive integers in <tt class="docutils literal"><span class="pre">I</span></tt>. According to the <a class="reference external" href="http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle">well-ordering principle</a>, <tt class="docutils literal"><span class="pre">I</span></tt> has a smallest positive element which we&#8217;ll call <tt class="docutils literal"><span class="pre">b</span></tt>.</p>
<p>Now we&#8217;ll want to show that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-90ec31e269b753a59bf0cbedfccd4e90.gif" alt="I=b\mathbb{Z}" title="I=b\mathbb{Z}" style="vertical-align: 0px; border: none;"/>. As usual, to prove equalities of sets, it will be shown that they contain one another.</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e8f352535d59c229a38cfe2c55b09c89.gif" alt="I\supseteq b\mathbb{Z}" title="I\supseteq b\mathbb{Z}" style="vertical-align: -3px; border: none;"/> is obvious &#8211; since <tt class="docutils literal"><span class="pre">I</span></tt> contains <tt class="docutils literal"><span class="pre">b</span></tt> and is closed under addition and subtraction, it contains all the multiples of <tt class="docutils literal"><span class="pre">b</span></tt>.</p>
<p>To prove <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0a4610c0513882474f2530d6bbd93029.gif" alt="I\subseteq b\mathbb{Z}" title="I\subseteq b\mathbb{Z}" style="vertical-align: -3px; border: none;"/> we&#8217;ll demonstrate that any element <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c71eb26970a87c67d6b566686d594bde.gif" alt="c\in I" title="c\in I" style="vertical-align: -1px; border: none;"/> is a multiple of <tt class="docutils literal"><span class="pre">b</span></tt>. Using the division algorithm we write <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e77f5b378e41cfc56462d254321362df.gif" alt="c=bq+r" title="c=bq+r" style="vertical-align: -4px; border: none;"/> for some integers <tt class="docutils literal"><span class="pre">q</span></tt> and <tt class="docutils literal"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">r</span> <span class="pre">&lt;</span> <span class="pre">b</span></tt>. But this means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c221f319128124533bf3367f57af2c09.gif" alt="r\in I" title="r\in I" style="vertical-align: -1px; border: none;"/> (because <tt class="docutils literal"><span class="pre">I</span></tt> contains <tt class="docutils literal"><span class="pre">bq</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt> and is closed under subtraction and addition). However, recall that <tt class="docutils literal"><span class="pre">b</span></tt> was chosen to be the smallest positive element of <tt class="docutils literal"><span class="pre">I</span></tt>, so <tt class="docutils literal"><span class="pre">r</span></tt> must be equal to 0. Therefore <tt class="docutils literal"><span class="pre">c</span></tt> is a multiple of <tt class="docutils literal"><span class="pre">b</span></tt>, and we have shown that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0a4610c0513882474f2530d6bbd93029.gif" alt="I\subseteq b\mathbb{Z}" title="I\subseteq b\mathbb{Z}" style="vertical-align: -3px; border: none;"/>. <em>Q.E.D.</em></p>
<p>Now back to Bézout&#8217;s identity. We&#8217;ll define:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a812f1098fb40bd893c4042708a4a046.gif" alt="I=\{x\in \mathbb{Z}|x=ma+nb; m,n\in \mathbb{Z}\}" title="I=\{x\in \mathbb{Z}|x=ma+nb; m,n\in \mathbb{Z}\}" style="vertical-align: -5px; border: none;"/></p>
<p>This <tt class="docutils literal"><span class="pre">I</span></tt> is obviously non-empty and is closed under addition and subtraction (by its definition as a linear combination). Note, in particular, that it also contains <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>. By <strong>(III)</strong>, <tt class="docutils literal"><span class="pre">I</span></tt> consists of all multiples of its smallest positive element, which we&#8217;ll call <tt class="docutils literal"><span class="pre">d</span></tt> here.</p>
<p>To show that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-51abf15d7187e2c9ffe9188b44ed0037.gif" alt="d=(a,b)" title="d=(a,b)" style="vertical-align: -4px; border: none;"/> we have to show that <tt class="docutils literal"><span class="pre">d|a</span></tt>, <tt class="docutils literal"><span class="pre">d|b</span></tt> and if <tt class="docutils literal"><span class="pre">c|a</span></tt> and <tt class="docutils literal"><span class="pre">c|b</span></tt> then <tt class="docutils literal"><span class="pre">c|d</span></tt>. First, by definition <tt class="docutils literal"><span class="pre">d</span></tt> is a divisor of any element in <tt class="docutils literal"><span class="pre">I</span></tt>, so it also divides <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>. If <tt class="docutils literal"><span class="pre">c|a</span></tt> and <tt class="docutils literal"><span class="pre">c|b</span></tt>, say <tt class="docutils literal"><span class="pre">a=cq</span></tt> and <tt class="docutils literal"><span class="pre">b=cp</span></tt>, then:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7b8feefa08fe328ed2f1aa712ab241a4.gif" alt="d=ma+mb=m(cq)+n(cp)=c(mq+np)" title="d=ma+mb=m(cq)+n(cp)=c(mq+np)" style="vertical-align: -4px; border: none;"/></p>
<p>So <tt class="docutils literal"><span class="pre">d|c</span></tt>, which completes our proof that <tt class="docutils literal"><span class="pre">d=(a,b)</span></tt>. <em>Q.E.D.</em></p>
<p>Regarding the corollary, it stems trivially from the definition of <tt class="docutils literal"><span class="pre">I</span></tt> and the proof above.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1763&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li><li><a href='http://eli.thegreenplace.net/2005/01/21/mathematical-musing/' rel='bookmark' title='Permanent Link: mathematical musing'>mathematical musing</a> <small>This morning while washing the dishes I was contemplating a...</small></li><li><a href='http://eli.thegreenplace.net/2005/03/29/application-of-combinations/' rel='bookmark' title='Permanent Link: application of combinations'>application of combinations</a> <small>The text buffewring widget I&#8217;m building allows the user to...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The well-ordering principle</title>
		<link>http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle/</link>
		<comments>http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 07:25:53 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1772</guid>
		<description><![CDATA[The well-ordering principle states:
The well-ordering principle:  Any nonempty set of nonnegative integers has a smallest element.
DUH, you don&#8217;t say! &#8211; seems obvious, doesn&#8217;t it? This principle is, nevertheless, a very important and fundamental tool for proving other basic principles of number theory.
Consider, for instance, the Division Algorithm:
The Division Algorithm: If m and n are [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/' rel='bookmark' title='Permanent Link: Equivalence classes and group partitions'>Equivalence classes and group partitions</a> <small>In this post I want to show some interesting definitions...</small></li><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The <a class="reference external" href="http://en.wikipedia.org/wiki/Well-ordering_principle">well-ordering principle</a> states:</p>
<p><strong>The well-ordering principle:</strong>  Any nonempty set of nonnegative integers has a smallest element.</p>
<p><em>DUH, you don&#8217;t say!</em> &#8211; seems obvious, doesn&#8217;t it? This principle is, nevertheless, a very important and fundamental tool for proving other basic principles of number theory.</p>
<p>Consider, for instance, the <a class="reference external" href="http://en.wikipedia.org/wiki/Division_algorithm">Division Algorithm</a>:</p>
<p><strong>The Division Algorithm:</strong> If <tt class="docutils literal"><span class="pre">m</span></tt> and <tt class="docutils literal"><span class="pre">n</span></tt> are integers with <tt class="docutils literal"><span class="pre">n</span> <span class="pre">&gt;</span> <span class="pre">0</span></tt>, then there exist integers <tt class="docutils literal"><span class="pre">q</span></tt> and <tt class="docutils literal"><span class="pre">r</span></tt>, with <tt class="docutils literal"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">r</span> <span class="pre">&lt;</span> <span class="pre">n</span></tt>, such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-e1541616193bc56040d7a19b20d23c45.gif" alt="m = qn + r" title="m = qn + r" style="vertical-align: -4px; border: none;"/>.</p>
<p>Again, this is so basic that one may doubt whether it should even be proved. But the well-ordering principle allows us, in fact, to prove the division algorithm in a rigorous manner:</p>
<p>Let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0a9c517cf917057383f2800ec8b5be1a.gif" alt="W=\{m-tn|t\in \mathbb{Z}\}" title="W=\{m-tn|t\in \mathbb{Z}\}" style="vertical-align: -5px; border: none;"/>. It is obvious that <tt class="docutils literal"><span class="pre">W</span></tt> contains nonnegative integers. Let <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-33941c5f33cd661242d435cbc22fdacd.gif" alt="V=\{v\in W|v \geq0\}" title="V=\{v\in W|v \geq0\}" style="vertical-align: -5px; border: none;"/>. <em>By the well-ordering principle</em>, <tt class="docutils literal"><span class="pre">V</span></tt> has a smallest element, which we&#8217;ll call <tt class="docutils literal"><span class="pre">r</span></tt>. <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a1d188332535342ffdf4cbc841ec2297.gif" alt="r\in V" title="r\in V" style="vertical-align: -1px; border: none;"/>, so <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a196644a0789306ce7faae9f03e3821f.gif" alt="r = m-qn" title="r = m-qn" style="vertical-align: -4px; border: none;"/> for some <tt class="docutils literal"><span class="pre">q</span></tt> and <tt class="docutils literal"><span class="pre">r</span> <span class="pre">&gt;=</span> <span class="pre">0</span></tt> (by the definition of sets <tt class="docutils literal"><span class="pre">W</span></tt> and <tt class="docutils literal"><span class="pre">V</span></tt>, correspondingly).</p>
<p>Now, what&#8217;s left to prove is that <tt class="docutils literal"><span class="pre">r</span> <span class="pre">&lt;</span> <span class="pre">n</span></tt>. Let&#8217;s assume the opposite, namely that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5c7feb631ad50affab2fab32c78ce2a5.gif" alt="r=m-qn\geq n" title="r=m-qn\geq n" style="vertical-align: -4px; border: none;"/>. Rearranging: <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7a8a8a5e9c2015e9a8e524d2b862e6c1.gif" alt="r-n=m-(q+1)n\geq 0" title="r-n=m-(q+1)n\geq 0" style="vertical-align: -4px; border: none;"/>. By the definition of <tt class="docutils literal"><span class="pre">V</span></tt>, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0e8e6c383e04155afa4fec52d5e3f27d.gif" alt="m-(q+1)n\in V" title="m-(q+1)n\in V" style="vertical-align: -4px; border: none;"/> (since it has the form <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-d9e0fcc1bbb32e2d2cc40e233d4766ca.gif" alt="m-tn" title="m-tn" style="vertical-align: 0px; border: none;"/> for some integer <tt class="docutils literal"><span class="pre">t</span></tt> and is nonnegative). But recall that we called <tt class="docutils literal"><span class="pre">r</span></tt> the smallest element of <tt class="docutils literal"><span class="pre">V</span></tt>, and <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f9f07236623fb5aa7c817436faeb6698.gif" alt="m-(q+1)n\le r" title="m-(q+1)n\le r" style="vertical-align: -4px; border: none;"/>, so we have a contradiction.</p>
<p>Therefore, we see that <tt class="docutils literal"><span class="pre">r</span> <span class="pre">&lt;</span> <span class="pre">n</span></tt>. This completes the proof. <em>Q.E.D.</em></p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1772&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/07/10/the-gcd-and-linear-combinations/' rel='bookmark' title='Permanent Link: The GCD and linear combinations'>The GCD and linear combinations</a> <small>A linear combination of a and b is some integer...</small></li><li><a href='http://eli.thegreenplace.net/2009/07/17/equivalence-classes-and-group-partitions/' rel='bookmark' title='Permanent Link: Equivalence classes and group partitions'>Equivalence classes and group partitions</a> <small>In this post I want to show some interesting definitions...</small></li><li><a href='http://eli.thegreenplace.net/2009/08/01/a-group-theoretic-proof-of-eulers-theorem/' rel='bookmark' title='Permanent Link: A group-theoretic proof of Euler&#8217;s theorem'>A group-theoretic proof of Euler&#8217;s theorem</a> <small>A very important and useful theorem in number theory is...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/07/09/the-well-ordering-principle/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Project Euler problem 66 and continued fractions</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-66-and-continued-fractions/</link>
		<comments>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-66-and-continued-fractions/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 12:49:07 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1740</guid>
		<description><![CDATA[Problem 66 is one of those problems that make Project Euler lots of fun. It doesn&#8217;t have a brute-force solution, and to solve it one actually has to implement a non-trivial mathematical algorithm and get exposed to several interesting techniques.
I will not post the solution or the full code for the problem here, just a [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/02/28/project-euler-problem-69/' rel='bookmark' title='Permanent Link: Project Euler problem 69'>Project Euler problem 69</a> <small>Problem 69 is a really nice one. I looked at...</small></li><li><a href='http://eli.thegreenplace.net/2009/02/20/project-euler-problem-012/' rel='bookmark' title='Permanent Link: Project Euler problem 12'>Project Euler problem 12</a> <small>I&#8217;ve got hooked to Project Euler this week, after hearing...</small></li><li><a href='http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/' rel='bookmark' title='Permanent Link: Project Euler problem 83 &#8211; how creeps can help'>Project Euler problem 83 &#8211; how creeps can help</a> <small>Problems 18, 67, 81, 82 and 83 in Project Euler...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a class="reference external" href="http://projecteuler.net/index.php?section=problems&amp;id=66">Problem 66</a> is one of those problems that make Project Euler lots of fun. It doesn&#8217;t have a brute-force solution, and to solve it one actually has to implement a non-trivial mathematical algorithm and get exposed to several interesting techniques.</p>
<p>I will not post the solution or the full code for the problem here, just a couple of hints.</p>
<p>After a very short bout of Googling, you&#8217;ll discover that the Diophantine equation:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-05b738f6ffde78bd15dd6ea46a9bb86c.gif" alt="x^2-Dy^2=1" title="x^2-Dy^2=1" style="vertical-align: -4px; border: none;"/></p>
<p>Is quite famous and is called <a class="reference external" href="http://en.wikipedia.org/wiki/Pell%27s_equation">Pell&#8217;s equation</a>. From here, further web searches and Wikipedia-reading will bring you to at least two methods for finding the <em>fundamental solution</em>, which is the pair of <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> with minimal <tt class="docutils literal"><span class="pre">x</span></tt> solving it.</p>
<p>One of the methods involves computing the continued-fraction representation of the square root of <tt class="docutils literal"><span class="pre">D</span></tt>. <a class="reference external" href="http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html">This page</a> is a must read on this topic, and will help you with other Euler problems as well.</p>
<p>I want to post here a code snippet that implements the continued-fraction computation described in that link. Its steps follow the <em>Algebraic algoritm</em> given there:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">CF_of_sqrt</span>(n):
    <span style="color: #7f007f">&quot;&quot;&quot; Compute the continued fraction representation of the</span>
<span style="color: #7f007f">        square root of N.</span>

<span style="color: #7f007f">        The first element in the returned array is the whole</span>
<span style="color: #7f007f">        part of the fraction. The others are the denominators</span>
<span style="color: #7f007f">        up to (and not including) the point where it starts</span>
<span style="color: #7f007f">        repeating.</span>

<span style="color: #7f007f">        Uses the algorithm explained here:</span>
<span style="color: #7f007f">        http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html</span>
<span style="color: #7f007f">        In the section named: &quot;Methods of finding continued</span>
<span style="color: #7f007f">        fractions for square roots&quot;</span>
<span style="color: #7f007f">    &quot;&quot;&quot;</span>
    <span style="color: #00007f; font-weight: bold">if</span> is_square(n):
        <span style="color: #00007f; font-weight: bold">return</span> [<span style="color: #00007f">int</span>(math.sqrt(n))]

    ans = []

    step1_num = <span style="color: #007f7f">0</span>
    step1_denom = <span style="color: #007f7f">1</span>

    <span style="color: #00007f; font-weight: bold">while</span> <span style="color: #00007f">True</span>:
        nextn = <span style="color: #00007f">int</span>((math.floor(math.sqrt(n)) + step1_num) / step1_denom)
        ans.append(<span style="color: #00007f">int</span>(nextn))

        step2_num = step1_denom
        step2_denom = step1_num - step1_denom * nextn

        step3_denom = (n - step2_denom ** <span style="color: #007f7f">2</span>) / step2_num
        step3_num = -step2_denom

        <span style="color: #00007f; font-weight: bold">if</span> step3_denom == <span style="color: #007f7f">1</span>:
            ans.append(ans[<span style="color: #007f7f">0</span>] * <span style="color: #007f7f">2</span>)
            <span style="color: #00007f; font-weight: bold">break</span>

        step1_num, step1_denom = step3_num, step3_denom

    <span style="color: #00007f; font-weight: bold">return</span> ans
</pre>
</div>
<p>As I said, this still isn&#8217;t enough to solve the problem, but with this code in hand, the solution isn&#8217;t too far. Read some more about Pell&#8217;s equation and you&#8217;ll discover how to use this code to reach a solution.</p>
<p>It took my program ~30 milliseconds to find an answer to the problem, by the way. It took less than a second to solve a 10-times larger problem (for D &lt;= 10000), so I believe it to be a pretty good implementation.</p>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1740&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/02/28/project-euler-problem-69/' rel='bookmark' title='Permanent Link: Project Euler problem 69'>Project Euler problem 69</a> <small>Problem 69 is a really nice one. I looked at...</small></li><li><a href='http://eli.thegreenplace.net/2009/02/20/project-euler-problem-012/' rel='bookmark' title='Permanent Link: Project Euler problem 12'>Project Euler problem 12</a> <small>I&#8217;ve got hooked to Project Euler this week, after hearing...</small></li><li><a href='http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/' rel='bookmark' title='Permanent Link: Project Euler problem 83 &#8211; how creeps can help'>Project Euler problem 83 &#8211; how creeps can help</a> <small>Problems 18, 67, 81, 82 and 83 in Project Euler...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-66-and-continued-fractions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Efficient modular exponentiation algorithms</title>
		<link>http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/</link>
		<comments>http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 07:51:29 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1618</guid>
		<description><![CDATA[Earlier this week I&#8217;ve discussed efficient algorithms for exponentiation.
However, for real-life needs of number theoretic computations, just raising numbers to large exponents isn&#8217;t very useful, because extremely huge numbers start appearing very quickly [1], and these don&#8217;t have much use. What&#8217;s much more useful is modular exponentiation, raising integers to high powers  [2]
Luckily, we [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient integer exponentiation algorithms'>Efficient integer exponentiation algorithms</a> <small>Did you ever think about the most efficient method to...</small></li><li><a href='http://eli.thegreenplace.net/2009/03/07/computing-modular-square-roots-in-python/' rel='bookmark' title='Permanent Link: Computing modular square roots in Python'>Computing modular square roots in Python</a> <small>Consider the congruence of the form: n is a quadradic...</small></li><li><a href='http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/' rel='bookmark' title='Permanent Link: Horner&#8217;s rule: efficient evaluation of polynomials'>Horner&#8217;s rule: efficient evaluation of polynomials</a> <small>Here&#8217;s a general degree-n polynomial: To evaluate such a polynomial...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p><a class="reference external" href="http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/">Earlier this week</a> I&#8217;ve discussed efficient algorithms for exponentiation.</p>
<p>However, for real-life needs of number theoretic computations, just raising numbers to large exponents isn&#8217;t very useful, because extremely huge numbers start appearing very quickly <a class="footnote-reference" href="#id8" id="id1">[1]</a>, and these don&#8217;t have much use. What&#8217;s much more useful is <a class="reference external" href="http://en.wikipedia.org/wiki/Modular_exponentiation">modular exponentiation</a>, raising integers to high powers <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-209c3f9822ba416c3ee2262b756045a7.gif" alt="\pmod n" title="\pmod n" style="vertical-align: -4px; border: none;"/> <a class="footnote-reference" href="#id9" id="id2">[2]</a></p>
<p>Luckily, we can reuse the efficient algorithms developed in the previous article, with very few modifications to perform modular exponentiation as well. This is possible because of some convenient properties of modular arithmetic.</p>
<div class="section" id="modular-multiplication">
<h3>Modular multiplication</h3>
<p>Given two numbers, <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>, their product modulo <tt class="docutils literal"><span class="pre">n</span></tt> is <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3263817a532e631469c198ee1e9bde93.gif" alt="ab \pmod n" title="ab \pmod n" style="vertical-align: -4px; border: none;"/>. Consider the number <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">n</span></tt>, such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-456ec11ac5276d83f4b29153dab9f7b0.gif" alt="x\equiv a\pmod n" title="x\equiv a\pmod n" style="vertical-align: -4px; border: none;"/>. Such a number always exists, and we usually call it the <em>remainder</em> of dividing <tt class="docutils literal"><span class="pre">a</span></tt> by <tt class="docutils literal"><span class="pre">n</span></tt>. Similarly, there is a <tt class="docutils literal"><span class="pre">y</span> <span class="pre">&lt;</span> <span class="pre">b</span></tt>, such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2fdf83e53f0bceff8a76e75dbae80492.gif" alt="y\equiv b\pmod n" title="y\equiv b\pmod n" style="vertical-align: -4px; border: none;"/>. It follows from basic rules of modular arithmetic that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82a401c579a5b948666c86749ec2d4ab.gif" alt="xy\equiv ab\pmod n" title="xy\equiv ab\pmod n" style="vertical-align: -4px; border: none;"/> <a class="footnote-reference" href="#id10" id="id3">[3]</a></p>
<p>Therefore, if we want to know the product of <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> modulo <tt class="docutils literal"><span class="pre">n</span></tt>, we just have to keep their remainders when divided by <tt class="docutils literal"><span class="pre">n</span></tt>. Note: <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> may be arbitrarily large, but <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> are always smaller than <tt class="docutils literal"><span class="pre">n</span></tt>.</p>
</div>
<div class="section" id="a-naive-algorithm">
<h3>A naive algorithm</h3>
<p>What is the most naive way you can think of for raising computing <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7cdd097234bb3d53a2a621ca874f0b88.gif" alt="a^{b} \pmod n" title="a^{b} \pmod n" style="vertical-align: -4px; border: none;"/>? Raise <tt class="docutils literal"><span class="pre">a</span></tt> to the power <tt class="docutils literal"><span class="pre">b</span></tt>, and then reduce modulo <tt class="docutils literal"><span class="pre">n</span></tt>. Right?</p>
<p>Indeed, this is a very unsophisticated and slow method, because raising <tt class="docutils literal"><span class="pre">a</span></tt> to the power <tt class="docutils literal"><span class="pre">b</span></tt> can result in a really huge number that takes long to compute.</p>
<p>For any useful number, this algorithm is so slow that I&#8217;m not even going to run it in the tests.</p>
</div>
<div class="section" id="using-the-properties-of-modular-multiplication">
<h3>Using the properties of modular multiplication</h3>
<p>As we&#8217;ve learned above, modular multiplication allows us to just keep the intermediate result <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-209c3f9822ba416c3ee2262b756045a7.gif" alt="\pmod n" title="\pmod n" style="vertical-align: -4px; border: none;"/> at each step. So we don&#8217;t have to ever hold numbers larger than the modulo. Here&#8217;s the implementation of a simple repeated multiplication algorithm for computing modular exponents this way:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">modexp_mul</span>(a, b, n):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> i <span style="color: #0000aa">in</span> <span style="color: #00007f">xrange</span>(b):
        r = r * a % n
    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>It&#8217;s much better than the naive algorithm, but as we saw in the previous article it&#8217;s quite slow, requiring <tt class="docutils literal"><span class="pre">b</span></tt> multiplications (and reductions modulo <tt class="docutils literal"><span class="pre">n</span></tt>).</p>
<p>We can apply the same modular reduction rule to the more efficient exponentiation algorithms we&#8217;ve studied <a class="reference external" href="http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/">before</a>.</p>
</div>
<div class="section" id="modular-exponentiation-by-squaring">
<h3>Modular exponentiation by squaring</h3>
<p>Here&#8217;s the right-to-left method with modular reductions at each step.</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">modexp_rl</span>(a, b, n):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">while</span> <span style="color: #007f7f">1</span>:
        <span style="color: #00007f; font-weight: bold">if</span> b % <span style="color: #007f7f">2</span> == <span style="color: #007f7f">1</span>:
            r = r * a % n
        b /= <span style="color: #007f7f">2</span>
        <span style="color: #00007f; font-weight: bold">if</span> b == <span style="color: #007f7f">0</span>:
            <span style="color: #00007f; font-weight: bold">break</span>
        a = a * a % n

    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>We use exactly the same algorithm, but reduce every multiplication <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-209c3f9822ba416c3ee2262b756045a7.gif" alt="\pmod n" title="\pmod n" style="vertical-align: -4px; border: none;"/>. So the numbers we deal with here are never very large.</p>
<p>Similarly, here&#8217;s the left-to-right method:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">modexp_lr</span>(a, b, n):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> bit <span style="color: #0000aa">in</span> reversed(_bits_of_n(b)):
        r = r * r % n
        <span style="color: #00007f; font-weight: bold">if</span> bit == <span style="color: #007f7f">1</span>:
            r = r * a % n
    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>With <tt class="docutils literal"><span class="pre">_bits_of_n</span></tt> being, as before:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">_bits_of_n</span>(n):
    <span style="color: #7f007f">&quot;&quot;&quot; Return the list of the bits in the binary</span>
<span style="color: #7f007f">        representation of n, from LSB to MSB</span>
<span style="color: #7f007f">    &quot;&quot;&quot;</span>
    bits = []

    <span style="color: #00007f; font-weight: bold">while</span> n:
        bits.append(n % <span style="color: #007f7f">2</span>)
        n /= <span style="color: #007f7f">2</span>

    <span style="color: #00007f; font-weight: bold">return</span> bits
</pre>
</div>
</div>
<div class="section" id="relative-performance">
<h3>Relative performance</h3>
<p>As I&#8217;ve noted in the <a class="reference external" href="http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/">previous article</a>, the RL method does a worse job of keeping its multiplicands low than the LR method. And indeed, for smaller <tt class="docutils literal"><span class="pre">n</span></tt>, RL is somewhat faster than LR. For larger <tt class="docutils literal"><span class="pre">n</span></tt>, RL is somewhat slower.</p>
<p>What&#8217;s obvious is that now the built-in <tt class="docutils literal"><span class="pre">pow</span></tt> is superior to both hand-coded methods <a class="footnote-reference" href="#id11" id="id4">[4]</a>. My tests show it&#8217;s anywhere from twice to 10 times as fast.</p>
<p>Why is <tt class="docutils literal"><span class="pre">pow</span></tt> so much faster? Is it only the efficiency of C versus Python? Not really. In fact, <tt class="docutils literal"><span class="pre">pow</span></tt> uses an even more sophisticated algorithm for large exponents <a class="footnote-reference" href="#id12" id="id5">[5]</a>. Indeed, for small exponents the runtime of <tt class="docutils literal"><span class="pre">pow</span></tt> is similar to the runtime of the implementations I presented above.</p>
</div>
<div class="section" id="the-k-ary-lr-method">
<h3>The k-ary LR method</h3>
<p>It turns out that the LR method of repeated squaring can be generalized. Instead of breaking the exponent into bits of its base-2 representation, we can break it into larger pieces, and save some computations this way.</p>
<p>I&#8217;ll present the k-ary LR method that breaks the exponent into its &quot;digits&quot; in base <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-082bf236294ff058c05fd953990bafb0.gif" alt="m=2^k" title="m=2^k" style="vertical-align: 0px; border: none;"/> for some integer <tt class="docutils literal"><span class="pre">k</span></tt>. The exponent can be written as:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f067d34fb634f156fc8275983ce4db1a.gif" alt="b=t_{i}m^{i}+t_{i-1}m^{i-1}+\cdots t_{0}m^{0}" title="b=t_{i}m^{i}+t_{i-1}m^{i-1}+\cdots t_{0}m^{0}" style="vertical-align: -4px; border: none;"/></p>
<p>Where <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f406db9a09c0430f7e54c1a3bb217c3e.gif" alt="t_i" title="t_i" style="vertical-align: -3px; border: none;"/> are the digits of <tt class="docutils literal"><span class="pre">b</span></tt> in base <tt class="docutils literal"><span class="pre">m</span></tt>. <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bbd87e394c9a2e7a45fd63ed40ba0722.gif" alt="a^b" title="a^b" style="vertical-align: 0px; border: none;"/> is then:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-31312d7ddfbbbb697aec1de9cfa66cbf.gif" alt="a^{t_{i}m^{i}}\cdot a^{t_{i-1}m^{i-1}}\cdot \cdots a^{t_{0}}" title="a^{t_{i}m^{i}}\cdot a^{t_{i-1}m^{i-1}}\cdot \cdots a^{t_{0}}" style="vertical-align: 0px; border: none;"/></p>
<p>We compute this iteratively as follows <a class="footnote-reference" href="#id13" id="id6">[6]</a>:</p>
<p>Raise <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c5c7c883c5a1d1f7f24a2744366f0af7.gif" alt="a^{t_0}" title="a^{t_0}" style="vertical-align: 0px; border: none;"/> to the <tt class="docutils literal"><span class="pre">m</span></tt>-th power and multiply by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bb0b397fca0afe93b51c2c38e267459e.gif" alt="a^{t_1}" title="a^{t_1}" style="vertical-align: 0px; border: none;"/>. We get <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-3feef4c887ea91ce09bfb76198cea2b5.gif" alt="r_1 = a^{t_{0}m+t_1}" title="r_1 = a^{t_{0}m+t_1}" style="vertical-align: -4px; border: none;"/>. Next, raise <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-baf606802b02a2603db47ea634c06429.gif" alt="r_1" title="r_1" style="vertical-align: -4px; border: none;"/> to the <tt class="docutils literal"><span class="pre">m</span></tt>-th power and multiply by <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5c44fe2934adc3b72860d1c720f1949e.gif" alt="a^{t_2}" title="a^{t_2}" style="vertical-align: 0px; border: none;"/>, obtaining <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-0f5f2980dbe14ff15d99b6a636c7c72f.gif" alt="r_2 = a^{t_{0}m^{2}+t_{1}m+t_{2}}" title="r_2 = a^{t_{0}m^{2}+t_{1}m+t_{2}}" style="vertical-align: -3px; border: none;"/>. If we continue with this, we&#8217;ll eventually get <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-bbd87e394c9a2e7a45fd63ed40ba0722.gif" alt="a^b" title="a^b" style="vertical-align: 0px; border: none;"/>.</p>
<p>This translates into the following code:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">modexp_lr_k_ary</span>(a, b, n, k=<span style="color: #007f7f">5</span>):
    <span style="color: #7f007f">&quot;&quot;&quot; Compute a ** b (mod n)</span>

<span style="color: #7f007f">        K-ary LR method, with a customizable &#39;k&#39;.</span>
<span style="color: #7f007f">    &quot;&quot;&quot;</span>
    base = <span style="color: #007f7f">2</span> &lt;&lt; (k - <span style="color: #007f7f">1</span>)

    <span style="color: #007f00"># Precompute the table of exponents</span>
    table = [<span style="color: #007f7f">1</span>] * base
    <span style="color: #00007f; font-weight: bold">for</span> i <span style="color: #0000aa">in</span> <span style="color: #00007f">xrange</span>(<span style="color: #007f7f">1</span>, base):
        table[i] = table[i - <span style="color: #007f7f">1</span>] * a % n

    <span style="color: #007f00"># Just like the binary LR method, just with a</span>
    <span style="color: #007f00"># different base</span>
    <span style="color: #007f00">#</span>
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> digit <span style="color: #0000aa">in</span> reversed(_digits_of_n(b, base)):
        <span style="color: #00007f; font-weight: bold">for</span> i <span style="color: #0000aa">in</span> <span style="color: #00007f">xrange</span>(k):
            r = r * r % n
        <span style="color: #00007f; font-weight: bold">if</span> digit:
            r = r * table[digit] % n

    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>Note that we save some time by pre-computing the powers of <tt class="docutils literal"><span class="pre">a</span></tt> for exponents that can be digits in base <tt class="docutils literal"><span class="pre">m</span></tt>. Also, the <tt class="docutils literal"><span class="pre">_digits_of_n</span></tt> is the following generalization of <tt class="docutils literal"><span class="pre">_bits_of_n</span></tt>:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">_digits_of_n</span>(n, b):
    <span style="color: #7f007f">&quot;&quot;&quot; Return the list of the digits in the base &#39;b&#39;</span>
<span style="color: #7f007f">        representation of n, from LSB to MSB</span>
<span style="color: #7f007f">    &quot;&quot;&quot;</span>
    digits = []

    <span style="color: #00007f; font-weight: bold">while</span> n:
        digits.append(n % b)
        n /= b

    <span style="color: #00007f; font-weight: bold">return</span> digits
</pre>
</div>
</div>
<div class="section" id="performance-of-the-k-ary-method">
<h3>Performance of the k-ary method</h3>
<p>In my tests, the k-ary LR method with <tt class="docutils literal"><span class="pre">k</span> <span class="pre">=</span> <span class="pre">5</span></tt> is about 25% faster than the binary LR method, and is within 20% of the built-in <tt class="docutils literal"><span class="pre">pow</span></tt> function.</p>
<p>Experimenting with the value of <tt class="docutils literal"><span class="pre">k</span></tt> affects these results, but 5 seems to be a good value that produces the best performance in most cases. This is probably why it&#8217;s also used as the value of <tt class="docutils literal"><span class="pre">k</span></tt> in the implementation of <tt class="docutils literal"><span class="pre">pow</span></tt>.</p>
</div>
<div class="section" id="python-s-built-in-pow">
<h3>Python&#8217;s built-in <tt class="docutils literal"><span class="pre">pow</span></tt></h3>
<p>I&#8217;ve mentioned Python&#8217;s <tt class="docutils literal"><span class="pre">pow</span></tt> function several times in this article. The Python version I&#8217;m talking about is 2.5, though I doubt this functionality has changed in 2.6 or 3.0. The <tt class="docutils literal"><span class="pre">pow</span></tt> I&#8217;m interested in is implemented in the <tt class="docutils literal"><span class="pre">long_pow</span></tt> function in <tt class="docutils literal"><span class="pre">objects/longobject.c</span></tt> in the Python source code distribution. As mentioned in <a class="footnote-reference" href="#id12" id="id7">[5]</a>, it uses the binary LR method for small exponents, and the k-ary LR method for large exponents.</p>
<p>These implementations follow closely algorithms 14.79 and 14.82 in the excellent <em>Handbook of Applied Cryptography</em>, which is freely <a class="reference external" href="http://www.cacr.math.uwaterloo.ca/hac/">available online</a>.</p>
</div>
<div class="section" id="summary">
<h3>Summary</h3>
<p>As we&#8217;ve seen, exponentiation and modular exponentiation are one of those applications in which an efficient algorithm is required for feasibility. Using the trivial/naive algorithms is possible only for small cases which aren&#8217;t very interesting. To process realistically large numbers (such as the ones required for cryptographic algorithms), one needs powerful methods in his toolbox.</p>
<div align="center" class="align-center"><img alt="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" class="align-center" src="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" style="width: 320px; height: 5px;" /></div>
<table class="docutils footnote" frame="void" id="id8" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id1">[1]</a></td>
<td>For instance, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-ef84cc23743f471a30cf691e30d11570.gif" alt="3^{10000}" title="3^{10000}" style="vertical-align: 0px; border: none;"/> is a 4772-digit number.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id9" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id2">[2]</a></td>
<td>Modular exponentiation is essential for the <a class="reference external" href="http://en.wikipedia.org/wiki/RSA">RSA algorithm</a>, for example.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id10" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id3">[3]</a></td>
<td>To be a bit more rigorous, we start with <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-456ec11ac5276d83f4b29153dab9f7b0.gif" alt="x\equiv a\pmod n" title="x\equiv a\pmod n" style="vertical-align: -4px; border: none;"/>. This means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f26ecfd03b2710cf52ec9ba1b4951a13.gif" alt="n|(a-x)" title="n|(a-x)" style="vertical-align: -5px; border: none;"/>, so also <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-c6c552ce4eedcef66ef898366e7b0cfa.gif" alt="n|(ab-xb)" title="n|(ab-xb)" style="vertical-align: -5px; border: none;"/>. Similarly <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-53b975070af84fe5e46c76f14dd3ad94.gif" alt="n|(b-y)" title="n|(b-y)" style="vertical-align: -5px; border: none;"/>, so also <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-54cda7db3bc07590b108837dee01031d.gif" alt="n|(bx-yx)" title="n|(bx-yx)" style="vertical-align: -5px; border: none;"/>. Adding these two we get <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-f99e2dd8f5c9a5d91e0528ec5c9c8d8e.gif" alt="n|(ab-yx" title="n|(ab-yx" style="vertical-align: -5px; border: none;"/>), which means that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-82a401c579a5b948666c86749ec2d4ab.gif" alt="xy\equiv ab\pmod n" title="xy\equiv ab\pmod n" style="vertical-align: -4px; border: none;"/>.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id11" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id4">[4]</a></td>
<td>Using the 3-argument form of <tt class="docutils literal"><span class="pre">pow</span></tt>, you can perform modular exponentiation.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id12" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id5">[5]</a></td>
<td><tt class="docutils literal"><span class="pre">FIVEARY_CUTOFF</span></tt> in the code of <tt class="docutils literal"><span class="pre">pow</span></tt> is set to 8. This means that for exponents with more than 8 digits, a special 5-ary algorithm is used. For smaller exponents, the regular LR binary method is used &#8211; just like the one I presented, just coded in C.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id13" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id6">[6]</a></td>
<td>Note that for <tt class="docutils literal"><span class="pre">m</span> <span class="pre">=</span> <span class="pre">2</span></tt> this is the familiar binary LR method.</td>
</tr>
</tbody>
</table>
</div>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1618&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient integer exponentiation algorithms'>Efficient integer exponentiation algorithms</a> <small>Did you ever think about the most efficient method to...</small></li><li><a href='http://eli.thegreenplace.net/2009/03/07/computing-modular-square-roots-in-python/' rel='bookmark' title='Permanent Link: Computing modular square roots in Python'>Computing modular square roots in Python</a> <small>Consider the congruence of the form: n is a quadradic...</small></li><li><a href='http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/' rel='bookmark' title='Permanent Link: Horner&#8217;s rule: efficient evaluation of polynomials'>Horner&#8217;s rule: efficient evaluation of polynomials</a> <small>Here&#8217;s a general degree-n polynomial: To evaluate such a polynomial...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Efficient integer exponentiation algorithms</title>
		<link>http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/</link>
		<comments>http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 17:10:57 +0000</pubDate>
		<dc:creator>eliben</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1581</guid>
		<description><![CDATA[Did you ever think about the most efficient method to perform integer exponentiation, that is, raising an integer a to an integer power b, when either a or b, or both, are rather large?

Repeated multiplication
The naive method is, of course, repeated multiplications.  is a multiplied by itself b times. Here&#8217;s how it&#8217;s coded in [...]


Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient modular exponentiation algorithms'>Efficient modular exponentiation algorithms</a> <small>Earlier this week I&#8217;ve discussed efficient algorithms for exponentiation. However,...</small></li><li><a href='http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/' rel='bookmark' title='Permanent Link: Horner&#8217;s rule: efficient evaluation of polynomials'>Horner&#8217;s rule: efficient evaluation of polynomials</a> <small>Here&#8217;s a general degree-n polynomial: To evaluate such a polynomial...</small></li><li><a href='http://eli.thegreenplace.net/2009/02/21/rabin-miller-primality-test-implementation/' rel='bookmark' title='Permanent Link: Rabin-Miller primality test implementation'>Rabin-Miller primality test implementation</a> <small>Here&#8217;s a fairly efficient Python (2.5) and well-documented implementation of...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Did you ever think about the most efficient method to perform integer exponentiation, that is, raising an integer <tt class="docutils literal"><span class="pre">a</span></tt> to an integer power <tt class="docutils literal"><span class="pre">b</span></tt>, when either <tt class="docutils literal"><span class="pre">a</span></tt> or <tt class="docutils literal"><span class="pre">b</span></tt>, or both, are rather large?</p>
<div class="section" id="repeated-multiplication">
<h3>Repeated multiplication</h3>
<p>The naive method is, of course, repeated multiplications. <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d03b11a93699a69986dff42a5bf57af.gif" alt="a^{b}" title="a^{b}" style="vertical-align: 0px; border: none;"/> is <tt class="docutils literal"><span class="pre">a</span></tt> multiplied by itself <tt class="docutils literal"><span class="pre">b</span></tt> times. Here&#8217;s how it&#8217;s coded in my pseudo-code of choice, Python:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">expt_mul</span>(a, b):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> i <span style="color: #0000aa">in</span> <span style="color: #00007f">xrange</span>(b):
        r *= a
    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>Is this efficient? Not really, as we require <tt class="docutils literal"><span class="pre">b</span></tt> multiplications, and as I said earlier <tt class="docutils literal"><span class="pre">b</span></tt> can be very large (think number theory algorithms). In fact, there&#8217;s a <em>much</em> more efficient method.</p>
</div>
<div class="section" id="exponentiation-by-squaring">
<h3>Exponentiation by squaring</h3>
<p>The efficient exponentiation algorithm is based on the simple observation that for an even <tt class="docutils literal"><span class="pre">b</span></tt>, <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-622dcaaea0a551f9696056cd0fc5fce6.gif" alt="a^{b}=a^{b/2}\cdot a^{b/2}" title="a^{b}=a^{b/2}\cdot a^{b/2}" style="vertical-align: 0px; border: none;"/>. This may not look very brilliant, but now consider the following recursive definition:</p>
<p><center><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-5e0c3a7a156cf7ab3e92a85816c498a7.gif" alt="\mbox{Power}(a,\,b)=<br />
  \begin{cases} 1, &#038; \mbox{if }b\mbox{ = 0} \\<br />
                a\times\mbox{Power}(a,\,b-1), &#038; \mbox{if }b\mbox{ is odd} \\<br />
                \mbox{Power}(a,\,b/2)^2, &#038; \mbox{if }b\mbox{ is even}<br />
\end{cases}" title="\mbox{Power}(a,\,b)=<br />
  \begin{cases} 1, &#038; \mbox{if }b\mbox{ = 0} \\<br />
                a\times\mbox{Power}(a,\,b-1), &#038; \mbox{if }b\mbox{ is odd} \\<br />
                \mbox{Power}(a,\,b/2)^2, &#038; \mbox{if }b\mbox{ is even}<br />
\end{cases}" style="vertical-align: -34px; border: none;"/></center></p>
<p>The case of odd <tt class="docutils literal"><span class="pre">b</span></tt> is trivial, as it&#8217;s obvious that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-43d6101cf78c582a9b77ec19be480e81.gif" alt="a^{b}=a\cdot a^{b-1}" title="a^{b}=a\cdot a^{b-1}" style="vertical-align: 0px; border: none;"/>. So now we can compute <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d03b11a93699a69986dff42a5bf57af.gif" alt="a^{b}" title="a^{b}" style="vertical-align: 0px; border: none;"/> by doing only <tt class="docutils literal"><span class="pre">log(b)</span></tt> squarings and no more than <tt class="docutils literal"><span class="pre">log(b)</span></tt> multiplications, instead of <tt class="docutils literal"><span class="pre">b</span></tt> multiplications &#8211; and this is a vast improvement for a large <tt class="docutils literal"><span class="pre">b</span></tt>.</p>
<p>This algorithm can be coded in a straightforward way:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">expt_rec</span>(a, b):
    <span style="color: #00007f; font-weight: bold">if</span> b == <span style="color: #007f7f">0</span>:
        <span style="color: #00007f; font-weight: bold">return</span> <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">elif</span> b % <span style="color: #007f7f">2</span> == <span style="color: #007f7f">1</span>:
        <span style="color: #00007f; font-weight: bold">return</span> a * expt_rec(a, b - <span style="color: #007f7f">1</span>)
    <span style="color: #00007f; font-weight: bold">else</span>:
        p = expt_rec(a, b / <span style="color: #007f7f">2</span>)
        <span style="color: #00007f; font-weight: bold">return</span> p * p
</pre>
</div>
<p>Indeed, this algorithm is about 10 times faster than the naive one for exponents in the order of a few thousands. When the exponent is about 100K, it is more than 100 times faster, and the difference keeps growing for larger exponents.</p>
</div>
<div class="section" id="an-iterative-implementation">
<h3>An iterative implementation</h3>
<p>It will be useful to develop an iterative implementation for the fast exponentiation algorithm. For this purpose, however, we need to dive into some mathematics.</p>
<p>We can represent the exponent <tt class="docutils literal"><span class="pre">b</span></tt> as:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-dfbb63dd1b4e9787b3904778f03e67af.gif" alt="b=t_{i}2^{i}+t_{i-1}2^{i-1}+\cdots t_{0}2^{0}" title="b=t_{i}2^{i}+t_{i-1}2^{i-1}+\cdots t_{0}2^{0}" style="vertical-align: -4px; border: none;"/></p>
<p>Where <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-a578f93d86a9111f5900e4681f14ce16.gif" alt="t_{i}" title="t_{i}" style="vertical-align: -3px; border: none;"/> are the bits (0 or 1) of <tt class="docutils literal"><span class="pre">b</span></tt> in base 2. <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-2d03b11a93699a69986dff42a5bf57af.gif" alt="a^{b}" title="a^{b}" style="vertical-align: 0px; border: none;"/> is then:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-65cf6064290b6a5d2d3a6f6cca98deed.gif" alt="a^{t_{i}2^{i}}\cdot a^{t_{i-1}2^{i-1}}\cdot \cdots a^{t_{0}2^{0}}" title="a^{t_{i}2^{i}}\cdot a^{t_{i-1}2^{i-1}}\cdot \cdots a^{t_{0}2^{0}}" style="vertical-align: 0px; border: none;"/></p>
<p>Or, in other words:</p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-aab843b85587db3a5d5da0d7f3821bd4.gif" alt="a^{b}=\prod_{k}a^{2^{k}}" title="a^{b}=\prod_{k}a^{2^{k}}" style="vertical-align: -5px; border: none;"/> for <tt class="docutils literal"><span class="pre">k</span></tt> such that <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7b6ec5f5f80a2a21ebc38ac3b41bfd2e.gif" alt="t_{k}=1" title="t_{k}=1" style="vertical-align: -3px; border: none;"/></p>
<p><img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-fe70a970645558627e3f3417ed2b8c12.gif" alt="a^{2^{k}}" title="a^{2^{k}}" style="vertical-align: 0px; border: none;"/> can be computed by repetitive squaring, and moreover, we can reuse the result from a lower <tt class="docutils literal"><span class="pre">k</span></tt> to compute a higher <tt class="docutils literal"><span class="pre">k</span></tt>. This directly translates into the following iterative algorithm:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">expt_bin_rl</span>(a, b):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">while</span> <span style="color: #007f7f">1</span>:
        <span style="color: #00007f; font-weight: bold">if</span> b % <span style="color: #007f7f">2</span> == <span style="color: #007f7f">1</span>:
            r *= a
        b /= <span style="color: #007f7f">2</span>
        <span style="color: #00007f; font-weight: bold">if</span> b == <span style="color: #007f7f">0</span>:
            <span style="color: #00007f; font-weight: bold">break</span>
        a *= a

    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>To understand how the algorithm works, try to relate it to the formula from above. Using a standard &quot;divide by two and look at the LSB&quot; loop, the exponent <tt class="docutils literal"><span class="pre">b</span></tt> is broken into its binary representation. The lowest bits of <tt class="docutils literal"><span class="pre">b</span></tt> are considered first. <tt class="docutils literal"><span class="pre">a</span></tt> is continually squared to hold <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-fe70a970645558627e3f3417ed2b8c12.gif" alt="a^{2^{k}}" title="a^{2^{k}}" style="vertical-align: 0px; border: none;"/>, and is multiplied into the result only when <img src="http://eli.thegreenplace.net/wp-content/ql-cache/quicklatex-7b6ec5f5f80a2a21ebc38ac3b41bfd2e.gif" alt="t_{k}=1" title="t_{k}=1" style="vertical-align: -3px; border: none;"/>.</p>
<p>This algorithm is called <em>right-to-left binary exponentiation</em>, because the binary representation of the exponent is computed from right to left (from the LSB to the MSB) <a class="footnote-reference" href="#id4" id="id1">[1]</a>.</p>
<p>A related algorithm can be developed if we prefer to look at the binary representation of the exponent from left to right.</p>
</div>
<div class="section" id="left-to-right-binary-exponentiation">
<h3>Left-to-right binary exponentiation</h3>
<p>Going over the bits of <tt class="docutils literal"><span class="pre">b</span></tt> from MSB to LSB, we get:</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">expt_bin_lr</span>(a, b):
    r = <span style="color: #007f7f">1</span>
    <span style="color: #00007f; font-weight: bold">for</span> bit <span style="color: #0000aa">in</span> reversed(_bits_of_n(b)):
        r *= r
        <span style="color: #00007f; font-weight: bold">if</span> bit == <span style="color: #007f7f">1</span>:
            r *= a
    <span style="color: #00007f; font-weight: bold">return</span> r
</pre>
</div>
<p>Where <tt class="docutils literal"><span class="pre">_bits_of_n</span></tt> is a method returning the binary representation of its argument as an array of bits from LSB to MSB (which is then reversed, as you see):</p>
<div class="highlight">
<pre><span style="color: #00007f; font-weight: bold">def</span> <span style="color: #00007f">_bits_of_n</span>(n):
    <span style="color: #7f007f">&quot;&quot;&quot; Return the list of the bits in the binary</span>
<span style="color: #7f007f">        representation of n, from LSB to MSB</span>
<span style="color: #7f007f">    &quot;&quot;&quot;</span>
    bits = []

    <span style="color: #00007f; font-weight: bold">while</span> n:
        bits.append(n % <span style="color: #007f7f">2</span>)
        n /= <span style="color: #007f7f">2</span>

    <span style="color: #00007f; font-weight: bold">return</span> bits
</pre>
</div>
<p>Rationale: consider how you &quot;build&quot; a number from its binary representation when seen from MSB to LSB. You begin with 1 for the MSB (which is always 1, by definition, for numbers &gt; 0). For each new bit you see you double the result, and if the bit is 1, you add 1 <a class="footnote-reference" href="#id5" id="id2">[2]</a>.</p>
<p>For example consider the binary 1101. Begin with 1 for the leftmost 1. We have another bit, so we double. That&#8217;s 2. Now, the new bit is 1, so we add 1, that&#8217;s 3. We have another bit, so again double, that&#8217;s 6. The new bit is 0, so nothing is added. And we have one more bit, so once again double, getting 12, and finally adding 1, getting 13. Indeed, 1101 is the binary representation of 13.</p>
<p>Back to the exponentiation now. As you see in the code of <tt class="docutils literal"><span class="pre">expt_bin_lr</span></tt>, the binary representation of the exponent is read from MSB to LSB. Since this is the exponent, each &quot;doubling&quot; from the rationale above is squaring, and each &quot;adding 1&quot; is multiplying by the number itself. Hence, the algorithm works.</p>
</div>
<div class="section" id="performance">
<h3>Performance</h3>
<p>As I&#8217;ve mentioned, the squaring method of exponentiation is far more efficient than the naive method of repeated multiplication. In the tests I ran, the iterative left-to-right method is about the same speed as the recursive one, while the iterative right-to-left method is somewhat slower. In fact, both the recursive and the iterative left-to-right methods are so efficient they&#8217;re completely on par with Python&#8217;s built-in <tt class="docutils literal"><span class="pre">pow</span></tt> method <a class="footnote-reference" href="#id6" id="id3">[3]</a>.</p>
<p>This is surprising, as I&#8217;d actually expect the right-to-left method to be faster, because it skips the reversing of bits when computing the binary representation of the exponent. I&#8217;d also expect the built-in <tt class="docutils literal"><span class="pre">pow</span></tt> to be faster.</p>
<p>However, thinking harder for a moment, I think I can see why this happens. The RL (right-to-left) version has to multiply larger numbers at all stages, because LR sometimes multiplies by <code>a</code> itself, which is relatively small. Python&#8217;s bignum implementation can multiply by a small number faster, and this compensates for the need to reverse the bit list. I&#8217;ll come back to this issue when I&#8217;ll discuss modular exponentiation. But this is a topic for another article&#8230;</p>
<div align="center" class="align-center"><img alt="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" class="align-center" src="http://eli.thegreenplace.net/wp-content/uploads/hline.jpg" style="width: 320px; height: 5px;" /></div>
<table class="docutils footnote" frame="void" id="id4" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id1">[1]</a></td>
<td>From the looks of it (featuring the binary representation) you&#8217;d think this is a modern algorithm. Not at all! According to Knuth, it was first mentioned by the Persian mathematician Jamshīd al-Kāshī in 1427. The left-to-right method presented later in the article is even more ancient &#8211; it appeared in a Hindu book in about 200 BC.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id2">[2]</a></td>
<td>This holds for any base, by the way. You can similarly build a number from its decimal digits by multiplying by 10 for each digit you see and adding the digit, at each step.</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id6" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id3">[3]</a></td>
<td><tt class="docutils literal"><span class="pre">pow</span></tt> is coded in C and uses the iterative left-to-right method I described with some optimizations and complicated tricks.</td>
</tr>
</tbody>
</table>
</div>
<img src="http://eli.thegreenplace.net/?ak_action=api_record_view&id=1581&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://eli.thegreenplace.net/2009/03/28/efficient-modular-exponentiation-algorithms/' rel='bookmark' title='Permanent Link: Efficient modular exponentiation algorithms'>Efficient modular exponentiation algorithms</a> <small>Earlier this week I&#8217;ve discussed efficient algorithms for exponentiation. However,...</small></li><li><a href='http://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials/' rel='bookmark' title='Permanent Link: Horner&#8217;s rule: efficient evaluation of polynomials'>Horner&#8217;s rule: efficient evaluation of polynomials</a> <small>Here&#8217;s a general degree-n polynomial: To evaluate such a polynomial...</small></li><li><a href='http://eli.thegreenplace.net/2009/02/21/rabin-miller-primality-test-implementation/' rel='bookmark' title='Permanent Link: Rabin-Miller primality test implementation'>Rabin-Miller primality test implementation</a> <small>Here&#8217;s a fairly efficient Python (2.5) and well-documented implementation of...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
