<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Antimatroid, The &#187; Number Theory</title>
	<atom:link href="http://antimatroid.wordpress.com/tag/number-theory/feed/" rel="self" type="application/rss+xml" />
	<link>http://antimatroid.wordpress.com</link>
	<description>niche for the aesthetics, mathematics and computer science</description>
	<lastBuildDate>Tue, 01 Dec 2009 14:00:24 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='antimatroid.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/4f7d497af5ea099e7d73389ebe338967?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Antimatroid, The &#187; Number Theory</title>
		<link>http://antimatroid.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://antimatroid.wordpress.com/osd.xml" title="Antimatroid, The" />
		<item>
		<title>Integer Factorization by Dynamic Programming with Number Theoretic Applications</title>
		<link>http://antimatroid.wordpress.com/2009/04/01/integer-factorization-by-dynamic-programming-with-number-theoretic-applications/</link>
		<comments>http://antimatroid.wordpress.com/2009/04/01/integer-factorization-by-dynamic-programming-with-number-theoretic-applications/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 07:00:45 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Integer Factorization]]></category>
		<category><![CDATA[Number Theory]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=615</guid>
		<description><![CDATA[Having been a participant of a number of mathematical programming competitions over the years, I&#8217;ve had to find a number of efficient ways of implementing many common Number Theoretic Functions. In this write up, I&#8217;m going to go over a method I&#8217;ve found useful for easily factoring numbers using a sieving method, go over some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=615&subd=antimatroid&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Having been a participant of a number of mathematical programming competitions over the years, I&#8217;ve had to find a number of efficient ways of implementing many common Number Theoretic Functions. In this write up, I&#8217;m going to go over a method I&#8217;ve found useful for easily factoring numbers using a sieving method, go over some the implementation of a few number theory functions along with time complexity analysis of each. The cornerstone to many of these implementations relies on the ability to quickly factor integers and find primes.</p>
<p>One of the most popular methods of for finding primes is the <a target="blank" href="http://mathworld.wolfram.com/SieveofEratosthenes.html">Sieve of Eratosthenes</a>. The algorithm starts by populating a table with every positive integer from 2 to a ceiling value. Then find the first integer not yet crossed off, in the case 2, and eliminate every multiple of 2 from the table then return to 2 and find the next positive integer not yet crossed off and repeat the procedure until the end of the table is reached. The method is fine and all, but a lot of really great information is lost in that computation. Here is a sample implementation:</p>
<pre class="brush: csharp;">
bool[] isPrime = new bool[400];
for (uint n = 2; n &lt; isPrime.Length; n++)
    isPrime[n] = true;
for (uint n = 2; n &lt; isPrime.Length; n++)
    if (isPrime[n])
        for (uint m = 2, c = 0; (c = m * n) &lt; isPrime.Length; m++)
            isPrime[c] = false;
</pre>
<p>On the other hand, say we approach sieve a little differently. Create an empty table as large as the ceiling value. Start at 2 and for every multiple of 2, create a record that has two parts: 2 and half of the multiple. Return to 2 and find the next integer in the table that has yet to be recorded, in this case 3. For every multiple of 3, create a record that has two parts: 3 and third of the multiple (only if the multiple was not previously recorded. E.g., 6 because 2 previously recorded the record). Return to 3 and find the next integer in the table that has yet to be recorded so on and so forth until every integer in the table has been recorded.</p>
<p>The following graphic demonstrates this process for a ceiling values of 25. If we wish to factor 16, we go to 16&#8217;s record (2, 8), follow to 8&#8217;s record (2,4), again follow 4&#8217;s record (2, 2) and finally 2&#8217;s record (2, &lambda;). Thus the prime factorization of 16 is 2, 2, 2, 2.</p>
<p><a href="http://antimatroid.files.wordpress.com/2009/05/factor_table_polar.png"><img src="http://antimatroid.files.wordpress.com/2009/05/factor_table_polar.png?w=700&#038;h=760" alt="factor_table_polar" title="factor_table_polar" width="700" height="760" class="aligncenter size-full wp-image-622" /></a></p>
<p>It should be apparent that this algorithm is a simple dynamic programming solution that yields two major results: </p>
<ol>
<li>We have factored every positive integer up to a ceiling value.</li>
<li>We have found every positive prime integer up to a ceiling value.</li>
</ol>
<p>And one major draw back</p>
<ol>
<li>Uses a lot of memory as a trade off for speed.</li>
</ol>
<p>Let&#8217;s get into the C# implementation. To start off, we need a record class that&#8217;ll store the information about the first prime that divides an entry and the composite to jump to if the record corresponds to a composite. </p>
<pre class="brush: csharp;">
public class Record {
    public uint Prime {get; set;}
    public uint? JumpTo {get; set;}
}
</pre>
<p>We&#8217;ll have a class called NumberTheory and assume that it has the following structure. If you want, you could make this a Singleton class but I felt it was unnecessary for the scope of this write up.</p>
<pre class="brush: csharp;">
public class NumberTheory {
    private Record[] table;

    ...
}
</pre>
<p>It makes sense to put the core algorithm in the constructor and then have member methods for each of the functions we&#8217;d like to have. It should be assumed that for the lifetime of the class that the largest value ever called on the methods will be N otherwise an exception should be thrown by the methods (omitted here for brevity).</p>
<pre class="brush: csharp;">
public NumberTheory(uint N){
    uint c = 0;
    table = new Record[N+1];
    for(uint n = 2; n &lt; table.Length; n++) {
        if(table[n] != null)
            continue;
        table[n] = new Record() { Prime = n };
        for(uint m = 2; (c = n * m) &lt; table.Length; m++)
            if(table[c] == null)
                table[c] = new Record() { JumpTo = m, Prime = n };
    }
}
</pre>
<p>The time complexity of the implementation can be derived using some analysis and by having some knowledge of certain identities. Starting from 2 there are N &#8211; 2 numbers to check of which 1/2 will be visited by the interior loop, starting from 3 there are N &#8211; 3 numbers to check of which 1/3 will be visited visited by the interior loop, so on and so forth leading to the following summation:<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+%5Csum_%7Bp+%5Cle+n%7D+%5Cleft%28+%5Cfrac%7B1%7D%7Bp%7D%28n+-+p%29+%5Cright%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = \sum_{p \le n} \left( \frac{1}{p}(n - p) \right)' title='\displaystyle T(n) = \sum_{p \le n} \left( \frac{1}{p}(n - p) \right)' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+%5Csum_%7Bp+%5Cle+n%7D+%5Cleft%28+%5Cfrac%7Bn%7D%7Bp%7D+-+1+%5Cright%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = \sum_{p \le n} \left( \frac{n}{p} - 1 \right)' title='\displaystyle T(n) = \sum_{p \le n} \left( \frac{n}{p} - 1 \right)' class='latex' /></p>
<p>If we separate the summation into the harmonic series of primes (HSP) and prime counting function (aggregate 1 for every prime less than n) we get:<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+n+%5Csum_%7Bp+%5Cle+n%7D+%5Cleft%28+%5Cfrac%7B1%7D%7Bp%7D+%5Cright%29+-+%5Cpi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = n \sum_{p \le n} \left( \frac{1}{p} \right) - \pi(n)' title='\displaystyle T(n) = n \sum_{p \le n} \left( \frac{1}{p} \right) - \pi(n)' class='latex' /></p>
<p>Asymptotically, the HSP tends towards <img src='http://l.wordpress.com/latex.php?latex=%5Cln+%5Cln+n&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\ln \ln n' title='\ln \ln n' class='latex' /> and <img src='http://l.wordpress.com/latex.php?latex=%5Cpi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pi(n)' title='\pi(n)' class='latex' /> towards <img src='http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bn%7D%7B%5Cln+n%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\frac{n}{\ln n}' title='\frac{n}{\ln n}' class='latex' />.<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+n+%5Cleft%28+%5Cln+%5Cln+n+%5Cright%29+-+%5Cpi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = n \left( \ln \ln n \right) - \pi(n)' title='\displaystyle T(n) = n \left( \ln \ln n \right) - \pi(n)' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+n+%5Cleft%28+%5Cln+%5Cln+n+%5Cright%29+-+%5Cfrac%7Bn%7D%7B%5Cln+n%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = n \left( \ln \ln n \right) - \frac{n}{\ln n}' title='\displaystyle T(n) = n \left( \ln \ln n \right) - \frac{n}{\ln n}' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T%28n%29+%3D+n+%5Cfrac%7B+%5Cln+n+%5Ccdot+%5Cln+%5Cln%28n%29+-+1%7D%7B%5Cln+n%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T(n) = n \frac{ \ln n \cdot \ln \ln(n) - 1}{\ln n}' title='\displaystyle T(n) = n \frac{ \ln n \cdot \ln \ln(n) - 1}{\ln n}' class='latex' /></p>
<p>Giving us our final asymptotic time complexity of <img src='http://l.wordpress.com/latex.php?latex=O%28n+%5Cln+%5Cln+n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O(n \ln \ln n)' title='O(n \ln \ln n)' class='latex' />.</p>
<p>The first member method we&#8217;ll implement is a trivial check to see if a given number is prime by checking the table&#8217;s Record&#8217;s JumpTo property for null.</p>
<pre class="brush: csharp;">
public bool IsPrime(uint N){
    return !table[N].JumpTo.HasValue;
}
</pre>
<p>The time complexity here is a simple <img src='http://l.wordpress.com/latex.php?latex=O%281%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O(1)' title='O(1)' class='latex' />.</p>
<p>From IsPrime, we can easily implement a function that will get every single prime up to a given value by iterating over the table.</p>
<pre class="brush: csharp;">
public void PrimesLessThan(uint value, Action&lt;uint&gt; actOnPrime) {
    for(int n = 2; n &lt; value; n++)
	if(IsPrime(n))
		actOnPrime(n);
}
</pre>
<p>We can get the prime counting function <img src='http://l.wordpress.com/latex.php?latex=%5Cpi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pi(n)' title='\pi(n)' class='latex' /> but utilizing the PrimeLessThan function.</p>
<pre class="brush: csharp;">
public uint CountPrimes(uint n) {
	uint count = 0;
	PrimesLessThan(n, (p) =&gt; {count++;});
	return count;
}
</pre>
<p>Here the time complexity is the same as PrimesLessThan: <img src='http://l.wordpress.com/latex.php?latex=O%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O(n)' title='O(n)' class='latex' />.</p>
<p>We can get the prime factorization of a composite easily. To do so, we simply iterate over the records until we reach a Record with no JumpTo value.</p>
<pre class="brush: csharp;">
public void PrimeFactorsOf(uint composite, Action actOnFactor) {
    Record temp = table[composite];
    while(temp != null) {
        actOnFactor(temp.Prime);
        if(temp.JumpTo.HasValue)
            temp = table[temp.JumpTo.Value];
        else
            temp = null;
    }
}
</pre>
<p>The time complexity of this implementation relies on the the prime omega function <img src='http://l.wordpress.com/latex.php?latex=%5COmega%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\Omega(n)' title='\Omega(n)' class='latex' /> which is the number of prime factors (not necessarily distinct) of n. The function tends to <img src='http://l.wordpress.com/latex.php?latex=O%28+%5Cln+%5Cln+n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O( \ln \ln n)' title='O( \ln \ln n)' class='latex' />.</p>
<p>From PrimeFactorsOf, we can also easily implement Euler&#8217;s Totient Function <img src='http://l.wordpress.com/latex.php?latex=%5Cphi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\phi(n)' title='\phi(n)' class='latex' />- the function tells us how many positive integers less than n are coprime to n. It is defined as:<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cphi%28n%29+%3D+n+%5Cprod_%7Bp%7Cn%7D+%5Cleft%28+1+-+%5Cfrac%7B1%7D%7Bp%7D+%5Cright%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle \phi(n) = n \prod_{p|n} \left( 1 - \frac{1}{p} \right)' title='\displaystyle \phi(n) = n \prod_{p|n} \left( 1 - \frac{1}{p} \right)' class='latex' /><br />
Which essentially states that if you multiply all of the repeated prime factors of n together by all of the non-repeat prime factors &#8211; 1 of n together, you will have the result of <img src='http://l.wordpress.com/latex.php?latex=%5Cphi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\phi(n)' title='\phi(n)' class='latex' />.</p>
<pre class="brush: csharp;">
public uint EulerTotient(uint n){
    uint phi = 1, last = 0;
    PrimeFactorsOf(n, (p) =&gt; {
        if(p != last) {
            phi *= p - 1;
            last = p;
        } else {
            phi *= p;
        }
    });
    return phi;
}
</pre>
<p>A similar function known as the Dedekind <img src='http://l.wordpress.com/latex.php?latex=%5Cpsi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\psi(n)' title='\psi(n)' class='latex' /> Function defined as<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cpsi%28n%29+%3D+n+%5Cprod_%7Bp%7Cn%7D+%5Cleft+%28+1+%2B+%5Cfrac%7B1%7D%7Bp%7D+%5Cright+%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle \psi(n) = n \prod_{p|n} \left ( 1 + \frac{1}{p} \right )' title='\displaystyle \psi(n) = n \prod_{p|n} \left ( 1 + \frac{1}{p} \right )' class='latex' /><br />
can be implemented in a similar way as <img src='http://l.wordpress.com/latex.php?latex=%5Cphi%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\phi(n)' title='\phi(n)' class='latex' />:</p>
<pre class="brush: csharp;">
public uint DedekindPsi(uint n){
    uint phi = 1, last = 0;
    PrimeFactorsOf(n, (p) =&gt; {
        if(p != last) {
            phi *= p + 1;
            last = p;
        } else {
            phi *= p;
        }
    });
    return phi;
}
</pre>
<p>The Von Mangoldt Function <img src='http://l.wordpress.com/latex.php?latex=%5CLambda%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\Lambda(n)' title='\Lambda(n)' class='latex' /> is another interesting function, unfortunately I haven&#8217;t had a chance to use it, but it is trivial to implement so I will include it here for completeness. It is defined as<br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5CLambda+%28n%29+%3D+%5Cbegin%7Bcases%7D+%5Cln%7Bp%7D+%26+%5Ctext+%7Bif+n+%3D+prime+to+some+positive+integer+power%7D+%5C%5C+0+%26+%5Ctext%7Botherwise%7D+%5Cend%7Bcases%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle \Lambda (n) = \begin{cases} \ln{p} &amp; \text {if n = prime to some positive integer power} \\ 0 &amp; \text{otherwise} \end{cases}' title='\displaystyle \Lambda (n) = \begin{cases} \ln{p} &amp; \text {if n = prime to some positive integer power} \\ 0 &amp; \text{otherwise} \end{cases}' class='latex' /></p>
<pre class="brush: csharp;">
public double VonMangoldt(uint n){
    uint P = 0;
    PrimeFactorsOf(n, (p) =&gt; {
        if(P == 0) {
            P = p;
        } else if (P != p) {
            P = 1;
        }
    });
    return Math.Log(P);
}
</pre>
<p>The M&ouml;bius Function <img src='http://l.wordpress.com/latex.php?latex=%5Cmu%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mu(n)' title='\mu(n)' class='latex' /> is a handy little function for determining if a number is square free or not (among many more interesting things). It is defined as:<br />
<img src='http://l.wordpress.com/latex.php?latex=+%5Cdisplaystyle+%5Cmu%28n%29+%3D+%5Cbegin%7Bcases%7D+0+%26+%5Ctext%7Bif+n+has+one+or+more+replicated+factors%7D+%5C%5C+1+%26+%5Ctext%7Bif+n+%3D+1%7D+%5C%5C+%28-1%29%5E%7Bk%7D+%26+%5Ctext%7Bif+n+is+a+product+of+k+distinct+primes%7D+%5Cend%7Bcases%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt=' \displaystyle \mu(n) = \begin{cases} 0 &amp; \text{if n has one or more replicated factors} \\ 1 &amp; \text{if n = 1} \\ (-1)^{k} &amp; \text{if n is a product of k distinct primes} \end{cases}' title=' \displaystyle \mu(n) = \begin{cases} 0 &amp; \text{if n has one or more replicated factors} \\ 1 &amp; \text{if n = 1} \\ (-1)^{k} &amp; \text{if n is a product of k distinct primes} \end{cases}' class='latex' /></p>
<pre class="brush: csharp;">
public int MoebiusFunction(uint N){
    if(N == 1)
        return 1;
    bool distinct = true;
    uint last = 0, k = 0;
    PrimeFactorsOf(N, (p) =&gt; {
        if(p == last) {
            distinct = false;
        } else {
            k++;
            last = p;
        }
    });

    if(distinct)
        return ((k &amp; 1) == 0) ? 1 : -1;
    return 0;
}
</pre>
<p>Since EulerTotient, DedekindPsi, VonMangoldt and Moebius each use PrimeFactorsOf without any additional lifting, their time complexities are the same as PrimeFactorsOf &#8211; <img src='http://l.wordpress.com/latex.php?latex=O%28%5Cln+%5Cln+n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O(\ln \ln n)' title='O(\ln \ln n)' class='latex' />.</p>
<p>The last function I&#8217;ll implement is the Mertens Function <img src='http://l.wordpress.com/latex.php?latex=M%28n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='M(n)' title='M(n)' class='latex' /> which is simply defined as <img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+M%28n%29+%3D+%5Csum_%7Bk+%3D+1%7D%5E%7Bn%7D+%5Cmu%28k%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle M(n) = \sum_{k = 1}^{n} \mu(k)' title='\displaystyle M(n) = \sum_{k = 1}^{n} \mu(k)' class='latex' />:</p>
<pre class="brush: csharp;">
public int Mertens(uint n){
    int m = 0;
    for(uint k = 1; k &lt;= n; k++)
        m += MoebiusFunction(k);
    return m;
}
</pre>
<p>The implementation&#8217;s time complexity is simply <img src='http://l.wordpress.com/latex.php?latex=O%28n+%5Cln+%5Cln+n%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='O(n \ln \ln n)' title='O(n \ln \ln n)' class='latex' />.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/615/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=615&subd=antimatroid&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2009/04/01/integer-factorization-by-dynamic-programming-with-number-theoretic-applications/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>

		<media:content url="http://antimatroid.files.wordpress.com/2009/05/factor_table_polar.png" medium="image">
			<media:title type="html">factor_table_polar</media:title>
		</media:content>
	</item>
		<item>
		<title>In the crosshairs of number theory and complex analysis</title>
		<link>http://antimatroid.wordpress.com/2008/08/10/in-the-crosshairs-of-number-theory-and-complex-analysis/</link>
		<comments>http://antimatroid.wordpress.com/2008/08/10/in-the-crosshairs-of-number-theory-and-complex-analysis/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 03:05:05 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Complex Analysis]]></category>
		<category><![CDATA[Gaussian Integers]]></category>
		<category><![CDATA[Number Theory]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=262</guid>
		<description><![CDATA[
I recently started taking a foray into Complex Analysis as a means of filling in the gaps of my undergraduate mathematics knowledge. After reading about holomorphic functions, the Cauchy-Riemann equations and how to model ideal fluid flows I decided to take a break to digest it all. During this period I was reflecting on what [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=262&subd=antimatroid&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>
I recently started taking a foray into Complex Analysis as a means of filling in the gaps of my undergraduate mathematics knowledge. After reading about holomorphic functions, the Cauchy-Riemann equations and how to model ideal fluid flows I decided to take a break to digest it all. During this period I was reflecting on what I had done with Number Theory the previous summer and the question popped in my head: what is the complex equivalent of <img src='http://l.wordpress.com/latex.php?latex=w+%5Cequiv+v+%5Cpmod%7Bz%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='w \equiv v \pmod{z}' title='w \equiv v \pmod{z}' class='latex' />? Or for that matter, are integer primes also primes in the complex domain (and vice versa)? And, are all complex <img src='http://l.wordpress.com/latex.php?latex=z+%3D+%5Cdisplaystyle%5Cprod_%7Bk+%3D+0%7D%5E%7B%5Cinfty%7D+p_%7Bk%7D%5E%7Be_%7Bk%7D%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z = \displaystyle\prod_{k = 0}^{\infty} p_{k}^{e_{k}}' title='z = \displaystyle\prod_{k = 0}^{\infty} p_{k}^{e_{k}}' class='latex' /> factorizations unique?
</p>
<p>
After looking around the internet for a while I came across a rather well written <a href="http://www.math.uconn.edu/~kconrad/blurbs/ugradnumthy/Zinotes.pdf">paper [pdf]</a> by Assistant Professor Keith Conrad of the University of Connecticut that answered all of the questions brewing in my head along with the ones that weren&#8217;t. I&#8217;m going to surmise the basic ideas behind the solutions Conrad wrote about, if you have the free time it&#8217;s worthwhile to read the original text in its entirety.
</p>
<p>
If we consider the set of complex numbers of the form <img src='http://l.wordpress.com/latex.php?latex=n+%2B+mi&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n + mi' title='n + mi' class='latex' /> and restrict <img src='http://l.wordpress.com/latex.php?latex=n%2C+m+%5Cin+%5Cmathbb%7BZ%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n, m \in \mathbb{Z}' title='n, m \in \mathbb{Z}' class='latex' /> we have the set of the Gaussian Integers <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' />. If we take two values <img src='http://l.wordpress.com/latex.php?latex=z%2C+w+%5Cin+%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z, w \in \mathbb{Z}[i]' title='z, w \in \mathbb{Z}[i]' class='latex' /> we can say that <img src='http://l.wordpress.com/latex.php?latex=z%7Cw&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z|w' title='z|w' class='latex' /> only if <img src='http://l.wordpress.com/latex.php?latex=w+%3D+zu&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='w = zu' title='w = zu' class='latex' /> where <img src='http://l.wordpress.com/latex.php?latex=u+%5Cin+%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='u \in \mathbb{Z}[i]' title='u \in \mathbb{Z}[i]' class='latex' />. Thus, <img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Cbar%7Bz%7Dw+%3D+%5Cbar%7Bz%7Dzu+%5CRightarrow+u+%3D+%5Cfrac%7B%5Cbar%7Bz%7Dw%7D%7B%5Cbar%7Bz%7Dz%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle\bar{z}w = \bar{z}zu \Rightarrow u = \frac{\bar{z}w}{\bar{z}z}' title='\displaystyle\bar{z}w = \bar{z}zu \Rightarrow u = \frac{\bar{z}w}{\bar{z}z}' class='latex' />. With this last step we&#8217;ve reduced the problem to satisfying two conditions: <img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Cbar%7Bz%7Dz%7C%5CRe%7B%5Cbar%7Bz%7Dw%7D+%5Cwedge+%5Cbar%7Bz%7Dz%7C%5CIm%7B%5Cbar%7Bz%7Dw%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle\bar{z}z|\Re{\bar{z}w} \wedge \bar{z}z|\Im{\bar{z}w}' title='\displaystyle\bar{z}z|\Re{\bar{z}w} \wedge \bar{z}z|\Im{\bar{z}w}' class='latex' />. For example let <img src='http://l.wordpress.com/latex.php?latex=z+%3D+1+%2B+2i%2C+w+%3D+-5+%2B+10i+%5CRightarrow+%5Cbar%7Bz%7Dz+%3D+5%2C+%5Cbar%7Bz%7Dw+%3D+15+%2B+20i&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z = 1 + 2i, w = -5 + 10i \Rightarrow \bar{z}z = 5, \bar{z}w = 15 + 20i' title='z = 1 + 2i, w = -5 + 10i \Rightarrow \bar{z}z = 5, \bar{z}w = 15 + 20i' class='latex' /> which satisfies the conditions <img src='http://l.wordpress.com/latex.php?latex=5%7C15+%5Cwedge+5%7C20+%5Ctherefore+z%7Cw&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='5|15 \wedge 5|20 \therefore z|w' title='5|15 \wedge 5|20 \therefore z|w' class='latex' />. For the congruence <img src='http://l.wordpress.com/latex.php?latex=w+%5Cequiv+v+%5Cpmod%7Bz%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='w \equiv v \pmod{z}' title='w \equiv v \pmod{z}' class='latex' /> with <img src='http://l.wordpress.com/latex.php?latex=w%2Cv%2Cz+%5Cin+%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='w,v,z \in \mathbb{Z}[i]' title='w,v,z \in \mathbb{Z}[i]' class='latex' /> to hold true, it must be the case that <img src='http://l.wordpress.com/latex.php?latex=w+%3D+zu+%2B+v+%5CRightarrow+z%7C%28w+-+v%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='w = zu + v \Rightarrow z|(w - v)' title='w = zu + v \Rightarrow z|(w - v)' class='latex' />.
</p>
<p>
Under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}' title='\mathbb{Z}' class='latex' /> any integer <img src='http://l.wordpress.com/latex.php?latex=m&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='m' title='m' class='latex' /> that is divisible by any integer other than a unit <img src='http://l.wordpress.com/latex.php?latex=%5Cpm+1&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pm 1' title='\pm 1' class='latex' /> or unit multiple <img src='http://l.wordpress.com/latex.php?latex=%5Cpm+m&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pm m' title='\pm m' class='latex' /> is said to be composite, otherwise <img src='http://l.wordpress.com/latex.php?latex=m&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='m' title='m' class='latex' /> is said to be prime. The same definition holds in <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' /> but our units are now <img src='http://l.wordpress.com/latex.php?latex=%5Cpm+1%2C+%5Cpm+i&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pm 1, \pm i' title='\pm 1, \pm i' class='latex' /> and our unit multiples are now <img src='http://l.wordpress.com/latex.php?latex=%5Cpm+z%2C+%5Cpm+zi&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\pm z, \pm zi' title='\pm z, \pm zi' class='latex' />. As one might have guessed all primes in <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}' title='\mathbb{Z}' class='latex' /> are primes in <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' /> however, the converse does not hold. For example <img src='http://l.wordpress.com/latex.php?latex=5&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='5' title='5' class='latex' /> is prime but can be written as <img src='http://l.wordpress.com/latex.php?latex=%281+%2B+2i%29%281+-+2i%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='(1 + 2i)(1 - 2i)' title='(1 + 2i)(1 - 2i)' class='latex' /> and is thus a composite number under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' />. It is also useful to mention that if <img src='http://l.wordpress.com/latex.php?latex=%5Cbar%7Bz%7Dz+%5Cin+%5Cmathbb%7BP%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\bar{z}z \in \mathbb{P}' title='\bar{z}z \in \mathbb{P}' class='latex' /> then <img src='http://l.wordpress.com/latex.php?latex=z&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z' title='z' class='latex' /> is prime. By way of the Fundamental Theorem of Arithmetic, every composite integer can be written as the product of prime integers, this is also the case under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' /> and each factorization is unique.
</p>
<p>
The mechanics of finding the prime factorization of <img src='http://l.wordpress.com/latex.php?latex=m+%5Cin+%5Cmathbb%7BZ%7D%5E%7B%2B%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='m \in \mathbb{Z}^{+}' title='m \in \mathbb{Z}^{+}' class='latex' /> can be naïvely done by trial division from <img src='http://l.wordpress.com/latex.php?latex=n+%3D+2&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n = 2' title='n = 2' class='latex' /> to <img src='http://l.wordpress.com/latex.php?latex=%5Csqrt%7Bm%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\sqrt{m}' title='\sqrt{m}' class='latex' />. Once <img src='http://l.wordpress.com/latex.php?latex=n%7Cm&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n|m' title='n|m' class='latex' /> reset <img src='http://l.wordpress.com/latex.php?latex=n+%3D+2&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n = 2' title='n = 2' class='latex' /> and <img src='http://l.wordpress.com/latex.php?latex=m_%7Bk%7D+%3D+m_%7Bk+-+1%7D+%2F+n&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='m_{k} = m_{k - 1} / n' title='m_{k} = m_{k - 1} / n' class='latex' /> and continue this procedure until <img src='http://l.wordpress.com/latex.php?latex=m_%7Bk%7D+%3D+1&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='m_{k} = 1' title='m_{k} = 1' class='latex' />. That is all well and fine for <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5E%7B%2B%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}^{+}' title='\mathbb{Z}^{+}' class='latex' /> but it isn&#8217;t immediately useful for doing the same under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' />.
</p>
<p>
For example, let&#8217;s consider <img src='http://l.wordpress.com/latex.php?latex=z+%3D+-1395+-+12410i&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z = -1395 - 12410i' title='z = -1395 - 12410i' class='latex' />, since we are uncertain about how to factor <img src='http://l.wordpress.com/latex.php?latex=z&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z' title='z' class='latex' />, lets think about how to factor <img src='http://l.wordpress.com/latex.php?latex=%5Cbar%7Bz%7Dz&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\bar{z}z' title='\bar{z}z' class='latex' /> as we already know how to factor under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}' title='\mathbb{Z}' class='latex' /> and see where that takes us. <img src='http://l.wordpress.com/latex.php?latex=%5Cbar%7Bz%7Dz+%3D+155954125+%3D+5%5E%7B3%7D+%5Ccdot+61+%5Ccdot+113+%5Ccdot+181&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\bar{z}z = 155954125 = 5^{3} \cdot 61 \cdot 113 \cdot 181' title='\bar{z}z = 155954125 = 5^{3} \cdot 61 \cdot 113 \cdot 181' class='latex' /> We went from <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D+%5Cto+%5Cmathbb%7BZ%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i] \to \mathbb{Z}' title='\mathbb{Z}[i] \to \mathbb{Z}' class='latex' /> so we&#8217;d hope that we could go the other way around by finding <img src='http://l.wordpress.com/latex.php?latex=p+%3D+%28n+%2B+mi%29%28n+-+mi%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='p = (n + mi)(n - mi)' title='p = (n + mi)(n - mi)' class='latex' /> for each of the prime factors of <img src='http://l.wordpress.com/latex.php?latex=%5Cbar%7Bz%7Dz&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\bar{z}z' title='\bar{z}z' class='latex' />. In other words, we want to know <img src='http://l.wordpress.com/latex.php?latex=p+%3D+n%5E%7B2%7D+%2B+m%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='p = n^{2} + m^{2}' title='p = n^{2} + m^{2}' class='latex' /> which will produce four Gaussian primes <img src='http://l.wordpress.com/latex.php?latex=n+%5Cpm+mi%2C+m+%5Cpm+ni&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n \pm mi, m \pm ni' title='n \pm mi, m \pm ni' class='latex' />. Thus, we find <img src='http://l.wordpress.com/latex.php?latex=5+%3D+1+%2B+2%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='5 = 1 + 2^{2}' title='5 = 1 + 2^{2}' class='latex' />, <img src='http://l.wordpress.com/latex.php?latex=61+%3D+5%5E%7B2%7D+%2B+6%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='61 = 5^{2} + 6^{2}' title='61 = 5^{2} + 6^{2}' class='latex' />, <img src='http://l.wordpress.com/latex.php?latex=113+%3D+7%5E%7B2%7D+%2B+8%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='113 = 7^{2} + 8^{2}' title='113 = 7^{2} + 8^{2}' class='latex' />, <img src='http://l.wordpress.com/latex.php?latex=181+%3D+9%5E%7B2%7D+%2B+10%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='181 = 9^{2} + 10^{2}' title='181 = 9^{2} + 10^{2}' class='latex' />. Now, according to Conrad we should be able to pick any one of the four possible Guassian primes for each of the prime factors and multiply each Gaussian prime across, in doing so we get <img src='http://l.wordpress.com/latex.php?latex=z+%3D+%282+%2B+i%29%5E%7B2%7D+%282+-+i%29+%286-5i%29+%288+-+7i%29+%2810+-+9i%29&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z = (2 + i)^{2} (2 - i) (6-5i) (8 - 7i) (10 - 9i)' title='z = (2 + i)^{2} (2 - i) (6-5i) (8 - 7i) (10 - 9i)' class='latex' />. Now that&#8217;s pretty damn cool.
</p>
<p>
The general procedure for factoring a Gaussian Integer <img src='http://l.wordpress.com/latex.php?latex=z&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z' title='z' class='latex' /> into it&#8217;s prime components is to find the integer factorization of <img src='http://l.wordpress.com/latex.php?latex=%5Cbar%7Bz%7Dz&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\bar{z}z' title='\bar{z}z' class='latex' /> and for each prime factor find <img src='http://l.wordpress.com/latex.php?latex=p+%3D+n%5E%7B2%7D+%2B+m%5E%7B2%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='p = n^{2} + m^{2}' title='p = n^{2} + m^{2}' class='latex' />. Next, explore the Cartesian space formed by each factor&#8217;s four Gaussian primes until you come across a product coordinate that equals <img src='http://l.wordpress.com/latex.php?latex=z&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z' title='z' class='latex' /> and output said coordinate. As one can image this is a woefully poor algorithm for find the prime factorization of <img src='http://l.wordpress.com/latex.php?latex=z&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='z' title='z' class='latex' />. If anyone has ideas or knows of more efficient methods post them in the comments.
</p>
<p>
There is clearly much, much more to be said about Gaussian Integers, but this feels like a good stopping point. If you want to find out more about how some of the traditional Number Theory constructs are defined under <img src='http://l.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D%5Bi%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathbb{Z}[i]' title='\mathbb{Z}[i]' class='latex' /> you&#8217;ll want to read the entirety of Conrad&#8217;s paper or jump on <a href="http://www.google.com/search?&amp;q=Gaussian+Integer">Google</a> and see what&#8217;s out there.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/antimatroid.wordpress.com/262/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/antimatroid.wordpress.com/262/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=262&subd=antimatroid&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2008/08/10/in-the-crosshairs-of-number-theory-and-complex-analysis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>
	</item>
		<item>
		<title>Name that fraction</title>
		<link>http://antimatroid.wordpress.com/2008/07/27/name-that-fraction/</link>
		<comments>http://antimatroid.wordpress.com/2008/07/27/name-that-fraction/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 04:30:20 +0000</pubDate>
		<dc:creator>lewellen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[Number Theory]]></category>

		<guid isPermaLink="false">http://antimatroid.wordpress.com/?p=214</guid>
		<description><![CDATA[
How do we go about converting a floating point number  to a quotient of natural numbers  to a given precision  such that ?

There are two ways to go about this, one of which is a subset of the other. We could use the Farey Sequence, but that is limited to mapping floating [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=214&subd=antimatroid&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p>
How do we go about converting a floating point number <img src='http://l.wordpress.com/latex.php?latex=q&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='q' title='q' class='latex' /> to a quotient of natural numbers <img src='http://l.wordpress.com/latex.php?latex=n%2C+m&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='n, m' title='n, m' class='latex' /> to a given precision <img src='http://l.wordpress.com/latex.php?latex=p&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='p' title='p' class='latex' /> such that <img src='http://l.wordpress.com/latex.php?latex=%5Cgcd%28n%2C+m%29+%3D+1&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\gcd(n, m) = 1' title='\gcd(n, m) = 1' class='latex' />?
</p></blockquote>
<p>There are two ways to go about this, one of which is a subset of the other. We could use the <a href="http://en.wikipedia.org/wiki/Farey_sequence">Farey Sequence</a>, but that is limited to mapping floating point values to fractions which fall into the range <img src='http://l.wordpress.com/latex.php?latex=%5Clvert+q+%5Crvert+%5Cin+%5B0%2C+1%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\lvert q \rvert \in [0, 1]' title='\lvert q \rvert \in [0, 1]' class='latex' /> or we can use a <a href="http://en.wikipedia.org/wiki/Stern-Brocot_tree">Stern-Brocot tree</a>, which allows for values to fall into the range <img src='http://l.wordpress.com/latex.php?latex=%5Cvert+q+%5Crvert+%5Cin+%5B0%2C+%5Cinfty%5D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\vert q \rvert \in [0, \infty]' title='\vert q \rvert \in [0, \infty]' class='latex' />.
</p>
<p>
If we think of the Stern-Brocot tree as a list whose composition is determined by iteration, we will want to define the initial list as the domain <img src='http://l.wordpress.com/latex.php?latex=%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B0%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\frac{0}{1}, \frac{1}{0}' title='\frac{0}{1}, \frac{1}{0}' class='latex' />. We want to know a value that lies between these two values such that the numerator and denominator are coprime. (So that the tree we construct contains only fractions that are in minimal terms.) The mediant achieves that goal and is defined as <img src='http://l.wordpress.com/latex.php?latex=%5Cfrac%7Ba%7D%7Bc%7D+%3C+%5Cfrac%7Ba+%2B+b%7D%7Bc+%2B+d%7D+%3C+%5Cfrac%7Bb%7D%7Bd%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\frac{a}{c} &lt; \frac{a + b}{c + d} &lt; \frac{b}{d}' title='\frac{a}{c} &lt; \frac{a + b}{c + d} &lt; \frac{b}{d}' class='latex' />. If we insert the mediant between every pair in the list until the last pair is accessed we&#8217;ve completed an iteration. If we take this process ad infinitum we get the Stern-Brocot tree. For example:
</p>
<p><img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T_%7B0%7D+%3D+%5C%7B+%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B0%7D+%5C%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T_{0} = \{ \frac{0}{1}, \frac{1}{0} \}' title='\displaystyle T_{0} = \{ \frac{0}{1}, \frac{1}{0} \}' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T_%7B1%7D+%3D+%5C%7B+%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B0%7D+%5C%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T_{1} = \{ \frac{0}{1}, \frac{1}{1}, \frac{1}{0} \}' title='\displaystyle T_{1} = \{ \frac{0}{1}, \frac{1}{1}, \frac{1}{0} \}' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T_%7B2%7D+%3D+%5C%7B+%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B2%7D%2C+%5Cfrac%7B1%7D%7B1%7D%2C+%5Cfrac%7B2%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B0%7D+%5C%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T_{2} = \{ \frac{0}{1}, \frac{1}{2}, \frac{1}{1}, \frac{2}{1}, \frac{1}{0} \}' title='\displaystyle T_{2} = \{ \frac{0}{1}, \frac{1}{2}, \frac{1}{1}, \frac{2}{1}, \frac{1}{0} \}' class='latex' /><br />
<img src='http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+T_%7B3%7D+%3D+%5C%7B+%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B3%7D%2C+%5Cfrac%7B1%7D%7B2%7D%2C+%5Cfrac%7B2%7D%7B3%7D%2C+%5Cfrac%7B1%7D%7B1%7D%2C+%5Cfrac%7B3%7D%7B2%7D%2C+%5Cfrac%7B2%7D%7B1%7D%2C+%5Cfrac%7B3%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B0%7D+%5C%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\displaystyle T_{3} = \{ \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}, \frac{3}{2}, \frac{2}{1}, \frac{3}{1}, \frac{1}{0} \}' title='\displaystyle T_{3} = \{ \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}, \frac{3}{2}, \frac{2}{1}, \frac{3}{1}, \frac{1}{0} \}' class='latex' /></p>
<p>
It is worth noting that if we had instead started with <img src='http://l.wordpress.com/latex.php?latex=%5Cfrac%7B0%7D%7B1%7D%2C+%5Cfrac%7B1%7D%7B1%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\frac{0}{1}, \frac{1}{1}' title='\frac{0}{1}, \frac{1}{1}' class='latex' /> that we would instead be operating over the Farey Sequence where every iteration is denoted as <img src='http://l.wordpress.com/latex.php?latex=%5Cmathfrak%7BF%7D_%7Bn%7D&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='\mathfrak{F}_{n}' title='\mathfrak{F}_{n}' class='latex' />. Arguably, we could have gone with this sequence by inverting all <img src='http://l.wordpress.com/latex.php?latex=q+%3E+1&#038;bg=fff&#038;fg=1c1c1c&#038;s=0' alt='q &gt; 1' title='q &gt; 1' class='latex' /> and then inverting the resolved fraction- which would have added unnecessary complexity to the end implementation.
</p>
<p>
Using the Stern-Brocot structure, we can implement a simple binary search over the list to map a floating point number to a fraction within a given precision. The following C# implementation illustrates how this can be done but it is not complete nor is it optimized in any way.
</p>
<pre class="brush: csharp;">
public static Fraction toFraction(this double q, int precision) {
    if (double.IsNaN(q))
        return null;
    if (double.IsPositiveInfinity(q))
        return new Fraction(1, 0);
    else if (double.IsNegativeInfinity(q))
        return new Fraction(-1, 0);
    else if (q == 0.0)
        return new Fraction(0, 0);

    bool negative = q &lt; 0;
    double val;

    Fraction L = new Fraction(0, 1);
    Fraction R = new Fraction(1, 0);
    Fraction M = null;

    q = Math.Round(Math.Abs(q), precision);

    do {
        M = Fraction.mediant(L, R);
        val = M.getValue(precision);

        if (val &lt; q)
            L = M;
        else if(val &gt; q)
            R = M;
    } while (val != q);

    return new Fraction(negative ? -1 : 1 * M.N, M.D);
}
</pre>
<pre class="brush: csharp;">
public class Fraction {
       public long N, D;

       public Fraction(long n, long d) {
           N = n; D = d;
       }

       public double value(int precision) {
           return Math.Round(N / (double)D, precision);
       }

       public static Fraction mediant(Fraction l, Fraction r) {
           return new Fraction(l.N + r.N, l.D + r.D);
       }
}
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/antimatroid.wordpress.com/214/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/antimatroid.wordpress.com/214/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antimatroid.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/antimatroid.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/antimatroid.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/antimatroid.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/antimatroid.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/antimatroid.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/antimatroid.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/antimatroid.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/antimatroid.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/antimatroid.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=antimatroid.wordpress.com&blog=4448583&post=214&subd=antimatroid&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://antimatroid.wordpress.com/2008/07/27/name-that-fraction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/62c32c6293d483fc7e1d545c4b9a0ac3?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">lewellen</media:title>
		</media:content>
	</item>
	</channel>
</rss>