<?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>So Jake Says &#187; Math</title>
	<atom:link href="http://www.jakevoytko.com/blog/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Human Numerical Methods</title>
		<link>http://www.jakevoytko.com/blog/2008/10/20/human-numerical-methods/</link>
		<comments>http://www.jakevoytko.com/blog/2008/10/20/human-numerical-methods/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 04:00:34 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[Abacus]]></category>
		<category><![CDATA[Affine]]></category>
		<category><![CDATA[Feynman]]></category>
		<category><![CDATA[Kilometers to Miles]]></category>
		<category><![CDATA[Miles to Kilometers]]></category>
		<category><![CDATA[Numerical Methods]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=175</guid>
		<description><![CDATA[Or: I thought about calling this &#8220;Humerical Methods&#8221;, but there&#8217;s nothing funny here. My girlfriend and I recently went on vacation to Germany. Distant relatives of mine (Wolfgang and Birgit), agreed to host us for a few days, which turned out to be a great break from cheap hostels. One day, Wolfgang tells us that [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or: I thought about calling this &#8220;Humerical Methods&#8221;, but there&#8217;s nothing funny here.</em></p>
<p>My girlfriend and I recently went on vacation to Germany. Distant relatives of mine (Wolfgang and Birgit), agreed to host us for a few days, which turned out to be a great break from cheap hostels.</p>
<p>One day, Wolfgang tells us that they are taking us to Öhringen, and town about 16 kilometers away.</p>
<p>He quickly corrects himself for the benefit of the Americans. &#8220;That should be about nine miles.&#8221;</p>
<p>&#8220;It&#8217;s closer to 9.6&#8243; I said without skipping a beat.</p>
<p>The speed of my answer surprised him. &#8220;Are you a quick multiplier?&#8221;</p>
<p>&#8220;Well, not exactly. Each kilometer is a little over .6 miles, so I add 1/2 and 1/10 the original number. It&#8217;s usually really faster than multiplying or dividing.&#8221;</p>
<p>It should be noted that this is an approximate method. The error is around 3.5%, as the conversion factor is closer to .621371. Most of the time, approximation are good enough with respect to distances. Once you have a handle on the distance, you can make all sorts of reasonable estimations, like driving time.</p>
<p><strong>Figuring it out</strong></p>
<p>Last March, I went on a road trip to Canada to compete in the <a href="http://www.csgames.org/2009/?page=news&amp;lang=en">CS Games</a> with my fellow Computer Science majors. Since it&#8217;s a <a href="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=2000+Pennington+Rd,+Trenton,+NJ+08618+to+Sherbrooke,+Canada&amp;sll=42.88908,-74.105515&amp;sspn=6.743637,19.753418&amp;ie=UTF8&amp;ll=42.795401,-73.344727&amp;spn=6.754174,19.753418&amp;z=6">long effing way</a> from TCNJ to Sherbrooke in Canada, we needed something to do. My buddy <a href="http://www.stephenlombardi.com/blog/">Steve</a> realized that we accidentally printed our return directions in kilometers instead of miles. He read out one of the distances on the map and asked what it was in miles/hour.</p>
<p>We thought for a second, and then I looked at the speedometer and read off the corresponding speed in miles per hour.</p>
<p>&#8220;How did you do it so fast?&#8221;</p>
<p>&#8220;Mental math,&#8221; I replied.</p>
<p>&#8220;What&#8217;s the conversion factor?&#8221; somebody asked. I saw that 100km/hr was roughly 60 miles, so I said &#8220;about .6.&#8221;</p>
<p>Luckily for me, all of the distances Steve read off were under 160km (the limit of my speedometer), so I was able to use the speedometer trick for a while. I figured that charade couldn&#8217;t last long, so I broke down the problem (multiplying by ~.6), and saw easy fractions I could use. I continued using this flawlessly.</p>
<p>Going from miles to kilometers is just as easy. Since a mile is ~1.609 kilometers, you can get an even better answer with identical math.</p>
<p>For a distance <em>n</em>, just add together <em>n</em> + <em>n</em>/2 + <em>n</em>/10. This approximation leaves you within .6%!</p>
<p>It&#8217;s a piece of cake to develop new methods along these lines. Take the expansion of the multiplicative constant, <em>c</em>, pick the first few digits of <em>c</em>, and break it up however mental arithmetic is easiest. Each digit of <em>c</em> that you use gives you a better approximation.</p>
<p>Determining the relative error is easy: it&#8217;s the unused digits of <em>c</em> divided by <em>c</em>.</p>
<p>You probably use this method for calculating tips already. If <em>n</em> is the dollar amount of the tip, it is easy to calculate 10% of the tip: <em>x</em> = .1 * <em>n</em>. The full amount of the tip is <em>x</em> + <em>x</em>/2, giving you 15%. That&#8217;s 1/10th + 1/20th, or 10% + 5%. This particular application happens to be exact.</p>
<p>You can even determine a generalization for any affine method, such as temperature conversions, by also adding and subtracting rounded approximations.</p>
<h2>Feynman Vs. the Abacus</h2>
<p>The above is child&#8217;s play! Any kid who has learned division can figure out their own tricks. The <em>real</em> limits of human mental math are accomplished by mental lookup tables. Special numbers are the name of the game; the more special numbers you know, the easier it is to quickly solve a problem.</p>
<p>The best example I have found of this is a story in <a href="http://www.amazon.com/gp/product/0393316041?ie=UTF8&amp;tag=jakvoyshom-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0393316041">Surely You&#8217;re Joking, Mr. Feynman!</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=as2&amp;o=1&amp;a=0393316041" border="0" alt="" width="1" height="1" />, where he is challenged by a Japanese abacus salesman while visiting Portugal. I highly recommend you read the <a href="http://www.ee.ryerson.ca/~elf/abacus/feynman.html">full text of the encounter</a>.</p>
<p>The first challenge is addition. Feynman doesn&#8217;t stand a chance against the abacus, even when the numbers are written down and shown to both parties at the same time.</p>
<p>Next comes multiplication. Feynman still loses, but he has started to close the gap.</p>
<p>The next challenge is long division. It is a tie. This bothered the abacus salesman, so he challenges Feynman to cube roots. He writes the number &#8220;1729.03&#8243; on a piece of paper and starts working the abacus. Feynman quickly figures out that the first few digits are &#8220;12.002&#8243; while the abacus salesman struggles to even determine &#8220;12&#8243;.</p>
<p>As the book puts it:</p>
<blockquote><p>How did the customer beat the abacus?</p>
<p>The number was 1729.03. I happened to know that a cubic foot contains 1728 cubic inches, so the answer is a tiny bit more than 12. The excess, 1.03 is only one part in nearly 2000, and I had learned in calculus that for small fractions, the cube root&#8217;s excess is one-third of the number&#8217;s excess. So all I had to do is find the fraction 1/1728, and multiply by 4 (divide by 3 and multiply by 12). So I was able to pull out a whole lot of digits that way.</p></blockquote>
<h2>Hooray Math!</h2>
<p>The Feynman abacus story demonstrates one of the great things about math: every little fact eventually counts for something.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=175&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/10/20/human-numerical-methods/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How Important are Computers in Mathematics?</title>
		<link>http://www.jakevoytko.com/blog/2008/03/21/how-important-are-computers-in-mathematics/</link>
		<comments>http://www.jakevoytko.com/blog/2008/03/21/how-important-are-computers-in-mathematics/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 04:00:17 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quote]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/03/21/how-important-are-computers-in-mathematics/</guid>
		<description><![CDATA[&#8220;The use of a program to prove the 4-color theorem will not change mathematics &#8211; it merely demonstrates that the theorem, a challenge for a century, is probably not important to mathematics.&#8221;~ From &#8220;Epigrams in Programming&#8221; by Alan J. Perlis It&#8217;s hard to agree or disagree with this quote. On one hand, results in mathematics [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;The use of a program to prove the 4-color theorem will not change mathematics &#8211; it merely demonstrates that the theorem, a challenge for a century, is probably not important to mathematics.&#8221;~ From &#8220;<a href="http://www.cs.yale.edu/quotes.html">Epigrams in Programming</a>&#8221; by Alan J. Perlis</p></blockquote>
<p>It&#8217;s hard to agree or disagree with this quote.</p>
<p>On one hand, results in mathematics often end up in strange places. For example, <a href="http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/">Fermat&#8217;s Little Theorem</a> was little more than a curiosity for centuries. Its generalization is the basis of the RSA system, and it can be used in a pseudoprime generator. In fact, <a href="http://www.it.pgp.net/pgp-faq/faq-appendix2.html#2.3">PGP uses it</a> for just that very purpose.</p>
<p>The mere fact that a computer makes the result reachable not only means that other proofs can use the 4-color theorem, it means that computers can be used to prove other theorems. Many proofs are broken down into cases. This one just happens to have a lot of them that couldn&#8217;t be checked by hand. The computer helps advance mathematical results as well as humanity.</p>
<p>On the other hand, brute force isn&#8217;t a novel technique, so the process of getting the result is itself unimportant. The computer did what 1000 mathematicians with infinite cups of coffee could have done.</p>
<p>If you happen to know where I can witness 1000 mathematicians with infinite cups of coffee, please let me know.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=62&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/03/21/how-important-are-computers-in-mathematics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Approximating Euler&#8217;s Constant From Scratch</title>
		<link>http://www.jakevoytko.com/blog/2008/03/17/approximating-eulers-constant-from-scratch/</link>
		<comments>http://www.jakevoytko.com/blog/2008/03/17/approximating-eulers-constant-from-scratch/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 04:00:49 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Euler's constant]]></category>
		<category><![CDATA[Numerical Methods]]></category>
		<category><![CDATA[Taylor series]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/03/17/approximating-eulers-constant-from-scratch/</guid>
		<description><![CDATA[Or: Calculus You Will Use Either Every Day or Never Approximating Constants We will be calculating Euler&#8217;s constant, e, by starting off close to the correct value, and then moving closer and closer until we get to the correct value. This is known as approximation. There are a few different ways to do this. A [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or: Calculus You Will Use Either Every Day or Never</em></p>
<h2>Approximating Constants</h2>
<p>We will be calculating Euler&#8217;s constant, <em>e</em>, by starting off close to the correct value, and then moving closer and closer until we get to the correct value. This is known as approximation.</p>
<p>There are a few different ways to do this. A few are listed below:</p>
<ol>
<li>Find a function that evaluates to <em>e</em> and approximate the function.</li>
<li>Rephrase the problem as a root-finding problem and find solutions via famous root-finding methods (any of Householder&#8217;s Methods, Bisection, the Secant Method, etc).</li>
<li>Rephrase the problem as a fixed-point problem (I.E., find an <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" > such that <img src='/blog/wp-content/plugins/latexrender/pictures/908bf6fcdfc3677e5f7994db7d3cf594_3.5pt.gif' title='f(\alpha)=\alpha' alt='f(\alpha)=\alpha'  style="vertical-align:-3.5pt;" >.</li>
</ol>
<p>I have opted to write about number 1., find an approximating function using Taylor Series. Why did I pick it? The math is easy, and helps teach basics of approximation. Taylor approximation is generally slow, and is often better used to approximate functions over intervals rather than particular constants. This is just a particular application of Taylor series.</p>
<p>If you have not taken a Calculus course, the material presented here may or may not be lost on you.</p>
<h2>Taylor Series</h2>
<p>Assume that <img src='/blog/wp-content/plugins/latexrender/pictures/50bbd36e1fd2333108437a2ca378be62_3.5pt.gif' title='f(x)' alt='f(x)'  style="vertical-align:-3.5pt;" > is continuous and infinitely differentiable. If we want to know values of <img src='/blog/wp-content/plugins/latexrender/pictures/50bbd36e1fd2333108437a2ca378be62_3.5pt.gif' title='f(x)' alt='f(x)'  style="vertical-align:-3.5pt;" > near <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" >, we can use the formula below to determine just how many decimal places we need to use in our approximation.</p>
<blockquote><p><strong>Formula for a Taylor Series with infinite terms:</strong></p>
<p>If <img src='/blog/wp-content/plugins/latexrender/pictures/7997339883ac20f551e7f35efff0a2b9_3.5pt.gif' title='$f(x)$' alt='$f(x)$'  style="vertical-align:-3.5pt;" > is infinitely differentiable and continuous, a Taylor Series of <img src='/blog/wp-content/plugins/latexrender/pictures/7997339883ac20f551e7f35efff0a2b9_3.5pt.gif' title='$f(x)$' alt='$f(x)$'  style="vertical-align:-3.5pt;" > about <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" > is written:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/15301b787d39a4f31545c4ae469c5635_4.44841pt.gif' title='$f(x) =  \sum_{k=0}^{\infty}\frac{(x-\alpha)^k}{k!}f^{(k)}(\alpha)$' alt='$f(x) =  \sum_{k=0}^{\infty}\frac{(x-\alpha)^k}{k!}f^{(k)}(\alpha)$'  style="vertical-align:-4.44841pt;" ></p></blockquote>
<p>This may look a little confusing, but once you start trying a few test functions, it gets really easy to start approximating the function.</p>
<blockquote><p><strong>Example:</strong> <img src='/blog/wp-content/plugins/latexrender/pictures/c69fb211a9c11bcbab73a4fe8b750190_3.5pt.gif' title='$f(x) = e^x$' alt='$f(x) = e^x$'  style="vertical-align:-3.5pt;" > about <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" ></p>
<p><span style="text-decoration: underline;">The zeroth term:</span></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/94e2845116f28698a2e0848c0113830a_4.44841pt.gif' title='$f^{(0)}(\alpha)\frac{(x-\alpha)^0}{0!} ' alt='$f^{(0)}(\alpha)\frac{(x-\alpha)^0}{0!} '  style="vertical-align:-4.44841pt;" > = f(\alpha) = e^\alpha$[/tex]</p>
<p><span style="text-decoration: underline;">The first term:</span></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/97592a943ac7dc77459343d11cceea41_4.44841pt.gif' title='$f^{(1)}(\alpha)\frac{(x-\alpha)^1}{1!} = (x-\alpha)f\prime(\alpha) = (x-\alpha)*e^\alpha$' alt='$f^{(1)}(\alpha)\frac{(x-\alpha)^1}{1!} = (x-\alpha)f\prime(\alpha) = (x-\alpha)*e^\alpha$'  style="vertical-align:-4.44841pt;" ></p>
<p><span style="text-decoration: underline;">The result:</span></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/bfc2e78b9693064a4e7f29a12b94d732_4.44841pt.gif' title='$f(x) = e^\alpha + (x-\alpha)e^\alpha + \frac{(x-\alpha)^2}{2!}e^\alpha + \frac{(x-\alpha)^3}{3!}e^\alpha + \ldots$' alt='$f(x) = e^\alpha + (x-\alpha)e^\alpha + \frac{(x-\alpha)^2}{2!}e^\alpha + \frac{(x-\alpha)^3}{3!}e^\alpha + \ldots$'  style="vertical-align:-4.44841pt;" ></p></blockquote>
<h2>Error in Taylor Series</h2>
<p>If we add up infinitely many terms, we will have an infinite degree polynomial that exactly represents the function. However, I can&#8217;t wait for my computer to calculate infinite terms, so we need to know when to stop.</p>
<blockquote><p><strong>Remainder of a Taylor Polynomial</strong></p>
<p>In general, the error for an <em>n</em>th degree Taylor Polynomial of a function <img src='/blog/wp-content/plugins/latexrender/pictures/7997339883ac20f551e7f35efff0a2b9_3.5pt.gif' title='$f(x)$' alt='$f(x)$'  style="vertical-align:-3.5pt;" > about <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" > can, for some <img src='/blog/wp-content/plugins/latexrender/pictures/07063748ae35bba93105c37e5d7c71f6_2.94444pt.gif' title='$\varepsilon_x, x \leq \varepsilon_x \leq \alpha$' alt='$\varepsilon_x, x \leq \varepsilon_x \leq \alpha$'  style="vertical-align:-2.94444pt;" > be written as follows:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/24ab18481799e1080015dabb653abeca_6.19841pt.gif' title='$R_n(x) = \frac{(x-\alpha)^{n+1}}{(n+1)!}f^{(n+1)}(\varepsilon_x)$' alt='$R_n(x) = \frac{(x-\alpha)^{n+1}}{(n+1)!}f^{(n+1)}(\varepsilon_x)$'  style="vertical-align:-6.19841pt;" ></p></blockquote>
<p>When we are calculating how many terms we need, we need, in part, to find the maximum value of <img src='/blog/wp-content/plugins/latexrender/pictures/4e4a4e1a613356b3172ba5dfc81d2ae8_3.5pt.gif' title='$f^{(n+1)}(\varepsilon_x)$' alt='$f^{(n+1)}(\varepsilon_x)$'  style="vertical-align:-3.5pt;" > over the area <img src='/blog/wp-content/plugins/latexrender/pictures/bed6ee24fd08320c797f995ba6bdae16_3.5pt.gif' title='$[x, \alpha]$' alt='$[x, \alpha]$'  style="vertical-align:-3.5pt;" >.</p>
<h2>Actually Calculating <em>e</em></h2>
<p>We want to (for some twisted reason) compute <em>e</em> using a Taylor series without the calculation being in terms of <em>e</em>. One way to do this is to calculate the Taylor series for <img src='/blog/wp-content/plugins/latexrender/pictures/b6b70db98c2a5c2031dea120886f8211_1.0pt.gif' title='$e^x$' alt='$e^x$'  style="vertical-align:-1.0pt;" ></p>
<blockquote><p>Taylor Series for <img src='/blog/wp-content/plugins/latexrender/pictures/ff2d26be6b0b506663911208302f91b3_1.0pt.gif' title='e^x' alt='e^x'  style="vertical-align:-1.0pt;" > about <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" >:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/d91c6bf8f975b212a01fcc8fac940320_4.44841pt.gif' title='$e^x = 1 +  (x-\alpha) + \frac{1}{2!}(x-\alpha)^2 + \frac{1}{3!}(x-\alpha)^3 + \ldots' alt='$e^x = 1 +  (x-\alpha) + \frac{1}{2!}(x-\alpha)^2 + \frac{1}{3!}(x-\alpha)^3 + \ldots'  style="vertical-align:-4.44841pt;" ></p></blockquote>
<p>We should also note that <img src='/blog/wp-content/plugins/latexrender/pictures/7ec4db85c9b4f2d6ac7aeddd5013eddb_1.0pt.gif' title='$e^1=e$' alt='$e^1=e$'  style="vertical-align:-1.0pt;" >, so we can go on ahead and substitute 1 for x:</p>
<blockquote><p>Taylor Series for <img src='/blog/wp-content/plugins/latexrender/pictures/3206014b23d193061431d3405bbe24ff_1.0pt.gif' title='e^1' alt='e^1'  style="vertical-align:-1.0pt;" > about <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" >:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/9c372b67a65d5506d0e4c307fdb14d4f_4.44841pt.gif' title='$f(x) = e^\alpha +e^\alpha(1-\alpha) + e^\alpha\frac{1}{2!}(1-\alpha)^2 + \ldots' alt='$f(x) = e^\alpha +e^\alpha(1-\alpha) + e^\alpha\frac{1}{2!}(1-\alpha)^2 + \ldots'  style="vertical-align:-4.44841pt;" ></p></blockquote>
<p><strong>Set <img src='/blog/wp-content/plugins/latexrender/pictures/7b7f9dbfea05c83784f8b85149852f08_1.0pt.gif' title='\alpha' alt='\alpha'  style="vertical-align:-1.0pt;" > equal to zero.</strong> Magically, all of the <em>e</em>s will disappear, and we will be able to evaluate <em>e</em> without having to use <em>e</em>.</p>
<blockquote><p>Taylor Series for <img src='/blog/wp-content/plugins/latexrender/pictures/3206014b23d193061431d3405bbe24ff_1.0pt.gif' title='e^1' alt='e^1'  style="vertical-align:-1.0pt;" > about 0:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/f9f8e1d10a6ae2eb14943f083fa3e45d_3.5pt.gif' title='$f(x) = 2 + 1/2! + 1/3! + \ldots$' alt='$f(x) = 2 + 1/2! + 1/3! + \ldots$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<h2>What Degree Polynomial do we Use?</h2>
<p>We can play around with our error formula until we determine how many steps will be needed.</p>
<blockquote><p><strong>Simplifying Error Formula</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/2f651f6c5c7e80f2d579db5e27cae4d8_6.19841pt.gif' title='$error = f^{(n+1)}(\varepsilon_x)\frac{(x-\alpha)^{(n+1)}}{(n+1)!}$' alt='$error = f^{(n+1)}(\varepsilon_x)\frac{(x-\alpha)^{(n+1)}}{(n+1)!}$'  style="vertical-align:-6.19841pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/67deb8dfa7a984be0f75d71fc8839149_6.19841pt.gif' title='$error = f^{(n+1)}(\varepsilon_x)\frac{(1-\alpha)^{n+1}}{(n+1)!}$' alt='$error = f^{(n+1)}(\varepsilon_x)\frac{(1-\alpha)^{n+1}}{(n+1)!}$'  style="vertical-align:-6.19841pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/fbd5358095ae5e2f9863abaee10d1ffc_6.19841pt.gif' title='$error = f^{(n+1)}(\varepsilon_x)\frac{(1)^{n+1}}{(n+1)!}$' alt='$error = f^{(n+1)}(\varepsilon_x)\frac{(1)^{n+1}}{(n+1)!}$'  style="vertical-align:-6.19841pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/a330dd3ec63b56119ccc3eaf33318f4a_6.19841pt.gif' title='$error = e^{\varepsilon_x}\frac{1}{(n+1)!}$' alt='$error = e^{\varepsilon_x}\frac{1}{(n+1)!}$'  style="vertical-align:-6.19841pt;" ></p></blockquote>
<p>We see that we need to determine an upper bound on the value of  <img src='/blog/wp-content/plugins/latexrender/pictures/d149932f7c4d1e52656ebd05a1128a08_1.0pt.gif' title='e^{\varepsilon_x}' alt='e^{\varepsilon_x}'  style="vertical-align:-1.0pt;" > between 0 and 1. An easy upper bound for us is 2.72, as we know that it is larger than <em>e</em>.</p>
<blockquote><p><strong>Final error formula</strong></p>
<p>The error in our Taylor Series <img src='/blog/wp-content/plugins/latexrender/pictures/b70667752b64a675316b529adc8b9615_3.5pt.gif' title='f(x) = e^x, f(1)' alt='f(x) = e^x, f(1)'  style="vertical-align:-3.5pt;" > about 0 is:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/7f2a42e3a3ff098387d690f4702361ea_3.5pt.gif' title='$error = 2.72/(n+1)!$' alt='$error = 2.72/(n+1)!$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<h2>Naieve C++ Implementation</h2>
<p><em>Please note that this code is susceptible to a few calculation errors, including mismatched-precision addition.</em></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">double</span> calc_e<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> degree<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> answer <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> current_factorial <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i
    <span style="color: #008000;">&#123;</span>
        answer<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color:#800080;">1.</span><span style="color: #000040;">/</span>current_factorial<span style="color: #008080;">;</span>
        current_factorial<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span>i<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> answer<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Output for various Taylor polynomials:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Taylor poly of degree <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span><span style="color: #0000dd;">2</span>
Taylor poly of degree <span style="color: #0000dd;">2</span><span style="color: #008080;">:</span><span style="color:#800080;">2.5</span>
Taylor poly of degree <span style="color: #0000dd;">3</span><span style="color: #008080;">:</span><span style="color:#800080;">2.66666667</span>
Taylor poly of degree <span style="color: #0000dd;">4</span><span style="color: #008080;">:</span><span style="color:#800080;">2.70833333</span>
Taylor poly of degree <span style="color: #0000dd;">5</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71666667</span>
Taylor poly of degree <span style="color: #0000dd;">6</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71805556</span>
Taylor poly of degree <span style="color: #0000dd;">7</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71825397</span>
Taylor poly of degree <span style="color: #0000dd;">8</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71827877</span>
Taylor poly of degree <span style="color: #0000dd;">9</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71828153</span>
Taylor poly of degree <span style="color: #0000dd;">10</span><span style="color: #008080;">:</span><span style="color:#800080;">2.7182818</span>
Taylor poly of degree <span style="color: #0000dd;">11</span><span style="color: #008080;">:</span><span style="color:#800080;">2.71828183</span></pre></div></div>

<p>This gives us precision of less than <img src='/blog/wp-content/plugins/latexrender/pictures/9e504c5af74ea9ad8254e332a1d55ff8_1.0pt.gif' title='$10^8$' alt='$10^8$'  style="vertical-align:-1.0pt;" > with 11 steps. Checking the error formula, we see that <img src='/blog/wp-content/plugins/latexrender/pictures/f13f386349d31c45afc287fa26e4d28c_3.5pt.gif' title='2.71/12!' alt='2.71/12!'  style="vertical-align:-3.5pt;" > is less than <img src='/blog/wp-content/plugins/latexrender/pictures/9e504c5af74ea9ad8254e332a1d55ff8_1.0pt.gif' title='$10^8$' alt='$10^8$'  style="vertical-align:-1.0pt;" >, but <img src='/blog/wp-content/plugins/latexrender/pictures/02bbbf4c8f528e3ea7830cc171772097_3.5pt.gif' title='2.71/11!' alt='2.71/11!'  style="vertical-align:-3.5pt;" > is greater than <img src='/blog/wp-content/plugins/latexrender/pictures/9e504c5af74ea9ad8254e332a1d55ff8_1.0pt.gif' title='$10^8$' alt='$10^8$'  style="vertical-align:-1.0pt;" >, so our error is also correct.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=61&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/03/17/approximating-eulers-constant-from-scratch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quake 3&#8242;s Fast Inverse Square Root Function</title>
		<link>http://www.jakevoytko.com/blog/2008/01/28/quake-3s-fast-square-root-function/</link>
		<comments>http://www.jakevoytko.com/blog/2008/01/28/quake-3s-fast-square-root-function/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 05:25:47 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Fast inverse square root]]></category>
		<category><![CDATA[Iterative]]></category>
		<category><![CDATA[Newton-Raphson]]></category>
		<category><![CDATA[Quake 3]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/01/28/quake-3s-fast-square-root-function/</guid>
		<description><![CDATA[Note: This is not meant to be an authoritative mathematical description, and I&#8217;m pretty late to the party.. I was experimenting with the code, and am scratching an itch. For a far superior description, please look at Chris Lomont&#8217;s excellent analysis. The Infamous Code x2 = number * 0.5F; y = number; i = * [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: This is not meant to be an authoritative mathematical description, and I&#8217;m pretty late to the party.. I was experimenting with the code, and am scratching an itch. For a far superior description, please look at Chris Lomont&#8217;s <a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf">excellent analysis</a>.</em></p>
<h2>The Infamous Code</h2>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">x2 <span style="color: #000080;">=</span> number <span style="color: #000040;">*</span> <span style="color:#800080;">0.5F</span><span style="color: #008080;">;</span>
y  <span style="color: #000080;">=</span> number<span style="color: #008080;">;</span>
i  <span style="color: #000080;">=</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">long</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>y<span style="color: #008080;">;</span>
i  <span style="color: #000080;">=</span> <span style="color: #208080;">0x5f3759df</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span> i <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
y  <span style="color: #000080;">=</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>i<span style="color: #008080;">;</span>
y  <span style="color: #000080;">=</span> y <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span> threehalfs <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span> x2 <span style="color: #000040;">*</span> y <span style="color: #000040;">*</span> y <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// y  = y * ( threehalfs - ( x2 * y * y ) );</span></pre></div></div>

<h2>The Function to Model</h2>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/invsqrt.png" alt="Inverse Square Root" /></p>
<h2>How Did the Authors Think of This?</h2>
<p>Interestingly, this method is nearly identical to one from a mathematical text called &#8220;<a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FIntroduction-Numerical-Analysis-Kendall-Atkinson%2Fdp%2F0471624896%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1201497518%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">An Introduction to Numerical Analysis</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" />&#8220;, where there is an application exercise to compute the square root of a function, taking advantage of the storage of floating point numbers.</p>
<p>My Numerical Methods book for this semester contains the full derivation of the method from &#8220;An Introduction to Numerical Analysis&#8221; that uses linear interpolation for an initial guess to Newton&#8217;s Method that gives the accuracy of the function to provably under 4.7E-14 for four iterations. Chris Lomont&#8217;s paper goes into much more detail about the method for choosing a suitable constant that the &#8220;Quake 3&#8243; authors likely used. Linear interpolation gives a fairly good guess, but it&#8217;s possible to take advantage of the way the constant is stored to give us a much better guess. As you can see below, the guess isn&#8217;t linear, but actually fits the curve very well without any iterations of the Newton-Rhapson method.</p>
<p>The initial guess is very good. How good? It nearly overlaps the function. The guess is added in red:</p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/invsqrt_vs_constant.png" alt="Inverse Square Root With Constant" /></p>
<p>I was going to compare the output of the Quake 3 method with the real output, but it was difficult finding a view where there was any very noticeable difference at all, so suffice it to say that it is very close.</p>
<h2>Some of the Math</h2>
<p>We are trying to find a quick approximation for the function <img src='/blog/wp-content/plugins/latexrender/pictures/49183d94955ae5740aa3ce519cf8b009_2.94444pt.gif' title='$y = x^{\frac{-1}{2}}$' alt='$y = x^{\frac{-1}{2}}$'  style="vertical-align:-2.94444pt;" >. This can be rearranged as <img src='/blog/wp-content/plugins/latexrender/pictures/54d39f0f031540c16765d5ff80000bd9.gif' title='$0 = y^{-2} &amp;#8211; x$' alt='$0 = y^{-2} &amp;#8211; x$'  align=absmiddle>. We want to find the roots of this function for <img src='/blog/wp-content/plugins/latexrender/pictures/a68a2511f13494e2ba44cc046def78dd.gif' title='$F(y) = y^{-2} &amp;#8211; x$' alt='$F(y) = y^{-2} &amp;#8211; x$'  align=absmiddle>, which are +/- <img src='/blog/wp-content/plugins/latexrender/pictures/f52a893986e9de0715d949144c08ca0b_4.05008pt.gif' title='$\sqrt(x)$' alt='$\sqrt(x)$'  style="vertical-align:-4.05008pt;" >. Note that <img src='/blog/wp-content/plugins/latexrender/pictures/04f26fcbdfc34c1cd8d0688f942a15a8_3.5pt.gif' title='$F\prime(y) = -2 * y^{-3}$' alt='$F\prime(y) = -2 * y^{-3}$'  style="vertical-align:-3.5pt;" >.</p>
<h2>Newton-Raphson Method</h2>
<p>Back in the days of Newton, all math had to be calculated by hand. Since it was often impossible to calculate the exact value of many results, approximations were needed.</p>
<p>The Newton-Raphson method is used to quickly approximate function roots. The basic idea is that we start off with a guess that we think is very close to the value of the root. We then take the tangent line at the function f(x). Provided that f(x) is continuous, we follow the tangent line to the X-axis. We then take the derivative at this point and follow the tangent line to the function to the X-axis. Rinse and repeat until you get the precision you need.</p>
<p>It is important to note that this method doesn&#8217;t always work: it is not guaranteed to converge, and in fact, you could continue calculating intersections <em>ad infinitum </em>and never get any closer to having the right answer! Therefore, it is important in this instance to try to optimize the initial guess to have as little error as possible.</p>
<p>As with all good methods, this one has an easy-to-remember formula.  For a current approximation, <img src='/blog/wp-content/plugins/latexrender/pictures/d7084ce258ffe96f77e4f3647b250bbf_2.49998pt.gif' title='$x_n$' alt='$x_n$'  style="vertical-align:-2.49998pt;" >, we find <img src='/blog/wp-content/plugins/latexrender/pictures/bfa03e1b73d4cba50a3eef37c4f20d57_3.5pt.gif' title='$x(n+1)$' alt='$x(n+1)$'  style="vertical-align:-3.5pt;" > by:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/06cb14fcd30adbedf4908f6e5af49555.gif' title='$x_{n+1} = x_{n} &amp;#8211; \frac{f(x_{n})}{f\prime(x_{n})}$' alt='$x_{n+1} = x_{n} &amp;#8211; \frac{f(x_{n})}{f\prime(x_{n})}$'  align=absmiddle></p>
<p>For those interested, the derivation can be found here. For the mildly interested, it is derived by taking the first few terms of the Taylor Series of the function.</p>
<p>So to do the Newton-Raphson approximation on a differentiable function, we need one thing:</p>
<ol>
<li>An initial guess of the root. The closer, the better.</li>
</ol>
<h2>Actual Iterative Derivation</h2>
<p>One small nit pick I had with Chris Lomont&#8217;s paper was that it skipped the actual derivation of the iterative function, so here it is:</p>
<blockquote><p><img src='/blog/wp-content/plugins/latexrender/pictures/f5949838267d957b802074b070c17e9f.gif' title='$y_{n+1} = y_{n} &amp;#8211; \frac{f(y_{n})}{f\prime(y_{n})}$' alt='$y_{n+1} = y_{n} &amp;#8211; \frac{f(y_{n})}{f\prime(y_{n})}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/f9908df14b5c1e7f06684410f25b2002.gif' title='$y_{n+1} = y_{n} &amp;#8211; \frac{y_{n}^{-2} &amp;#8211; x}{-2*y_{n}^{-3}}$' alt='$y_{n+1} = y_{n} &amp;#8211; \frac{y_{n}^{-2} &amp;#8211; x}{-2*y_{n}^{-3}}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/a62806df07c758d1d7f049e224e52870.gif' title='$y_{n+1} = y_{n} + y_{n}^{-2} * \frac{y_{n}^{3}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$' alt='$y_{n+1} = y_{n} + y_{n}^{-2} * \frac{y_{n}^{3}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/28f4071f9a8337fda53f615073bd4605.gif' title='$y_{n+1} = y_{n} + \frac{y_{n}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$' alt='$y_{n+1} = y_{n} + \frac{y_{n}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/f09d9a2922ad41eff6ae66d44b1eac44.gif' title='$y_{n+1} = \frac{3y_{n}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$' alt='$y_{n+1} = \frac{3y_{n}}{2} &amp;#8211; \frac{x*y_{n}^{3}}{2}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/9e891513a659c3cb3b31004103aa1d9f.gif' title='$y_{n+1} = y_{n} * (1.5 &amp;#8211; (x/2) y_{n}^{2})$' alt='$y_{n+1} = y_{n} * (1.5 &amp;#8211; (x/2) y_{n}^{2})$'  align=absmiddle></p></blockquote>
<p>When we substitute &#8220;<img src='/blog/wp-content/plugins/latexrender/pictures/332cc365a4987aacce0ead01b8bdcc0b_1.0pt.gif' title='$x$' alt='$x$'  style="vertical-align:-1.0pt;" >&#8221; for &#8220;<img src='/blog/wp-content/plugins/latexrender/pictures/c3a8057857fabfcea20140f7c90a76a7_2.94444pt.gif' title='$y_{n}$' alt='$y_{n}$'  style="vertical-align:-2.94444pt;" >&#8221; and &#8220;<img src='/blog/wp-content/plugins/latexrender/pictures/319afcef79efd357fc57aba5ad0dc553_3.33333pt.gif' title='$y_{n+1}$' alt='$y_{n+1}$'  style="vertical-align:-3.33333pt;" >&#8220;, (Since the function uses the same variable to store the current and next guess), we find the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">x <span style="color: #000080;">=</span> x <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color:#800080;">1.5</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>x<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> x <span style="color: #000040;">*</span> x<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Which looks awfully familiar.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=45&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/01/28/quake-3s-fast-square-root-function/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Why Does RSA Work?</title>
		<link>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/</link>
		<comments>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 02:05:54 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Decryption]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[RSA]]></category>
		<category><![CDATA[Schneier]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/</guid>
		<description><![CDATA[To skip to the math, scroll down or click here. The Algorithm The algorithm is divided into three stages: precalculation, encryption, and decryption. Precalculation is performed a single time for each person with a public/private key pair, and encryption/decryption is performed for each message. Precalculation Pick primes: Find two arbitrarily large prime numbers, p and [...]]]></description>
			<content:encoded><![CDATA[<p><em>To skip to the math, scroll down or <a href="#rsa_math">click here.</a></em></p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/adleman_r_s.jpg" alt="adleman_r_s.jpg" /></p>
<h2>The Algorithm</h2>
<p>The algorithm is divided into three stages: precalculation, encryption, and decryption. Precalculation is performed a single time for each person with a public/private key pair, and encryption/decryption is performed for each message.</p>
<p><strong>Precalculation</strong></p>
<ol>
<li><span style="text-decoration: underline;">Pick primes</span><em>: </em>Find two arbitrarily large prime numbers, <em>p</em> and <em>q</em>.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Determine the modulus</span>: Multiply <em>p </em>and <em>q</em> to get <em>n</em>. This will be your modulus for all equations.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Calculate phi(n)</span>: Using Euler&#8217;s <a href="#totient">totient function</a>, calculate <img src='/blog/wp-content/plugins/latexrender/pictures/8c3daefe5bb907b8ba6db6b68be61371_3.5pt.gif' title='$\phi(n) = (p-1) * (q-1)$' alt='$\phi(n) = (p-1) * (q-1)$'  style="vertical-align:-3.5pt;" > . This number is a secret number, so don&#8217;t give it away. The strength of the algorithm depends on <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > being hard to calculate when given a sufficiently large <em>n = p * q</em></li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Determine an encryption exponent</span>: Take any number, <em>e</em>, such that GCD(<img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >, <em>e</em>) = 1. This means that they are <em><a href="http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/">relatively prime</a></em>, and share no common factors. This number is considered your public key (when combined with <em>n</em>), and you can give this number to whoever you like.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Compute the decryption exponent</span><em>:</em> Solve the <em><a href="#euclidean">Extended Euclidean Algorithm</a></em> of GCD(<img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >, e) to find <img src='/blog/wp-content/plugins/latexrender/pictures/506ad5480ebbbba8fc18288a504b6748_1.0pt.gif' title='e^{-1}' alt='e^{-1}'  style="vertical-align:-1.0pt;" >. This is your private key.</li>
</ol>
<p>We have a private decryption key pair: <em>{n, d}, </em>and a public encryption key pair: <em>{n, e}</em>.</p>
<p><strong>Encryption</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1aa9538eb07872740d6b24fb4b8d4f11_3.5pt.gif' title='$m_{1} \equiv m^{e} (mod\ n)$' alt='$m_{1} \equiv m^{e} (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/96def595395241fe3de787a7597a9021_2.49998pt.gif' title='$m_{1}$' alt='$m_{1}$'  style="vertical-align:-2.49998pt;" > is the encrypted message, and it can safely be sent publicly.</p>
<p><strong>Decryption</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/ca9c8e2a8355ae89777001437050f3f6_3.5pt.gif' title='$m_{2} \equiv m_{1}^d (mod\ n)$' alt='$m_{2} \equiv m_{1}^d (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/8e3acbbfc26099340474ce98c66b8490_2.49998pt.gif' title='$m_{2}$' alt='$m_{2}$'  style="vertical-align:-2.49998pt;" > is the decrypted message. The person with the private key for the message will be able to read it, and theoretically, nobody else.</p>
<p><strong>Is the message preserved?</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/d867bc3f64c350ae7b5df118025ff437_3.5pt.gif' title='$m_{2} \equiv m_{1}^{d} (mod\ n) \equiv m^{ed} (mod\ n)' alt='$m_{2} \equiv m_{1}^{d} (mod\ n) \equiv m^{ed} (mod\ n)'  style="vertical-align:-3.5pt;" ></p>
<p>Applying the <em><a href="#euler_corollary">Corollary to Euler&#8217;s Theorem</a></em> to <img src='/blog/wp-content/plugins/latexrender/pictures/62139464e5724a75e09b2061874a68fc_3.5pt.gif' title='$m^{ed} (mod\ n)$' alt='$m^{ed} (mod\ n)$'  style="vertical-align:-3.5pt;" >, we get <img src='/blog/wp-content/plugins/latexrender/pictures/aa358c65fd3c4dca33ffbc8896a7510d_3.5pt.gif' title='$m^{1} (mod\ n) \equiv m$' alt='$m^{1} (mod\ n) \equiv m$'  style="vertical-align:-3.5pt;" >.</p>
<p>We also notice that <img src='/blog/wp-content/plugins/latexrender/pictures/b44b387c2fae8bb389fbef405fc6b930_3.5pt.gif' title='$m^{ed} \equiv m^{ee^{-1}} \equiv m (mod\ n)$' alt='$m^{ed} \equiv m^{ee^{-1}} \equiv m (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><strong>Why is it hard for an attacker to crack?</strong></p>
<p>In a perfect world, the attacker needs to solve some equivalent of the <em><a href="http://en.wikipedia.org/wiki/Integer_factorization">Integer Factorization Problem</a></em> to factor <em>n</em>, which is suspected to be outside of complexity class P using classical computation. If we have access to quantum computers, we have access to an algorithm, <a href="http://en.wikipedia.org/wiki/Shor's_algorithm">Shor&#8217;s Algorithm</a>, to crack integer factorization efficiently, but probabilistically.</p>
<p>The essential problem that the attacker faces is as follows: they have the encryption exponent and the modulus. They know what <em>n</em> is, but they currently have no method of calculating what <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > is without first factoring <em>n</em>. They need to find the decryption exponent, <em>d = <img src='/blog/wp-content/plugins/latexrender/pictures/947268967467ff4aa2ebe810f8c7b60f_1.0pt.gif' title='$e^{-1}$' alt='$e^{-1}$'  style="vertical-align:-1.0pt;" >,</em>, but can&#8217;t find the exponent without being able to solve the <em>Extended Euclidean Algorithm</em>, where they need to know the value of <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >. So long as <em>n</em> is hard to factor, RSA will remain difficult to break.</p>
<p>We don&#8217;t live in a perfect world, and there are plenty of examples of attacks that take advantage of weak implementations of RSA. See <a href="http://tools.ietf.org/html/rfc3447#page-6">RFC 3447</a> for some best practices.</p>
<h2>The Math</h2>
<p><a name="rsa_math"></a><br />
<strong><a name="euclidean"></a>Extended Euclidean Algorithm</strong></p>
<p>The <em>Euclidean Algorithm</em> finds the Greatest Common Divisor (GCD) of two integers, <em>a</em> and <em>b.</em> The <em>Extended Euclidean Algorithm</em> finds integers <em>m</em> and <em>n</em> in the following equation:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/e75dfd8e4bfee86535502787f332ac0d_3.5pt.gif' title='$GCD(a,\ b) = m * a + n * b' alt='$GCD(a,\ b) = m * a + n * b'  style="vertical-align:-3.5pt;" ></p>
<p><span style="text-decoration: underline;">Euclidean Algorithm</span></p>
<p>To see how it does this, we will look at an example. Let <em>a</em> = 200, <em>b</em> = 37. Please note that 200 = <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(101 * 3). Since (200, 37) = 1, this is the equivalent of trying to find 37-1 (mod 200). For previous writings on the material, click here.</p>
<blockquote>
<ol>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/f364ad298499010b6c41859eb464a297_1.83333pt.gif' title='$200 = 37 * 5 + 15$' alt='$200 = 37 * 5 + 15$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/5e4e4077ef4f088cbd9dc5aeb14f0728_1.83333pt.gif' title='$37 = 15 * 2 + 7$' alt='$37 = 15 * 2 + 7$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/98683372abe86293f38b37105d32c221_1.83333pt.gif' title='$15 = 7 * 2 + 1$' alt='$15 = 7 * 2 + 1$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/c6c639e36962dc17e3ae1299e298462c_1.83333pt.gif' title='$7 = 1 * 7 + 0$' alt='$7 = 1 * 7 + 0$'  style="vertical-align:-1.83333pt;" ></li>
</ol>
<p>Line 3 is considered the &#8220;final&#8221; line of the algorithm because it is the last line where the remainder is nonzero. The remainder of that line, 1, is also GCD(200, 37).</p></blockquote>
<p><span style="text-decoration: underline;">Extended Euclidean Algorithm</span></p>
<blockquote><p>We start by rewriting every equation so that the remainder is on the RHS.</p>
<ol>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/6f1119aaa4676402b94a91b8c419a7d7.gif' title='$15 = 200 &amp;#8211; 5 * 37$' alt='$15 = 200 &amp;#8211; 5 * 37$'  align=absmiddle></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/a2116d8125db7b822e12a7061da88670.gif' title='$7 = 37 &amp;#8211; 2 * 15$' alt='$7 = 37 &amp;#8211; 2 * 15$'  align=absmiddle></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/b8b9d4527b1cfbe334daa095ecc21765.gif' title='$1 = 15 &amp;#8211; 2 * 7$' alt='$1 = 15 &amp;#8211; 2 * 7$'  align=absmiddle></li>
</ol>
<p>From here, we start at line 3, and substitute in line 2. Don&#8217;t simplify any multiplications, just the additions.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/56ae81fde4832cce6d1c14b9afdebafb.gif' title='$1 = 15 &amp;#8211; 2 * 7 = 15 &amp;#8211; 2 * (37 &amp;#8211; 2 * 15)$' alt='$1 = 15 &amp;#8211; 2 * 7 = 15 &amp;#8211; 2 * (37 &amp;#8211; 2 * 15)$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/4f2911dc78845705774c34854ac36620.gif' title='$1 = 5 * 15 &amp;#8211; 2 * 37$' alt='$1 = 5 * 15 &amp;#8211; 2 * 37$'  align=absmiddle></p>
<p>Substitute line 1 into the resulting equation.</p>
<p>[Unparseable or potentially dangerous latex formula. Error 5 : 533x369]</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/95a8e87ded93c7161168f4963dce3c7d.gif' title='$1 = 5 * 200 &amp;#8211; 27 * 37$' alt='$1 = 5 * 200 &amp;#8211; 27 * 37$'  align=absmiddle></p></blockquote>
<p>We now have everything that we need for the inverse of 37. It turns out that <img src='/blog/wp-content/plugins/latexrender/pictures/95a8e87ded93c7161168f4963dce3c7d.gif' title='$1 = 5 * 200 &amp;#8211; 27 * 37$' alt='$1 = 5 * 200 &amp;#8211; 27 * 37$'  align=absmiddle> is equivalent to saying:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/f0a630494ed21992307526d3803cd79a_3.5pt.gif' title='$-27 * 37 \equiv 173 * 37 \equiv 1 (mod\ 200)' alt='$-27 * 37 \equiv 173 * 37 \equiv 1 (mod\ 200)'  style="vertical-align:-3.5pt;" ></p>
<p>so 173 is the multiplicative inverse of 37 (mod 200). Neat, huh?</p>
<p><strong><a name="Fermat"></a>Fermat&#8217;s Little Theorem + Euler&#8217;s Theorem</strong></p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/pierre_de_fermat.png" alt="pierre_de_fermat.png" /><br />
In a letter written in 1640, Fermat (of Pythagorean fame) offhandedly mentioned that he had noticed and proved the following relation:</p>
<p><span style="text-decoration: underline;">Fermat&#8217;s Little Theorem</span></p>
<blockquote><p>For <em>a</em> any integer, and <em>p</em> prime,</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/eed0a89e26b8b8947f6dff7d05933039_3.5pt.gif' title='$a^{p-1} \equiv 1 (mod\ p)$' alt='$a^{p-1} \equiv 1 (mod\ p)$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<p>As he is <a href="http://en.wikipedia.org/wiki/Fermat's_last_theorem">occasionally noted for doing</a>, he did not bother to write down the proof. Leibniz is said to have proved it, but didn&#8217;t get around to publishing it. He was an inventor of calculus, so we&#8217;ll cut him some slack.</p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/leonhard_euler.jpg" alt="leonhard_euler.jpg" /></p>
<p>Enter Leonhard Euler. Euler was 18 feet tall, shot laser beams from his eyes, and <a href="http://en.wikipedia.org/wiki/List_of_topics_named_after_Leonhard_Euler">proved Mathematics every second he was awake</a>. In 1736, he took 23 minutes off from a conquest of Mars to prove Fermat&#8217;s Little Theorem.</p>
<p>This wasn&#8217;t enough, though. He attempted to find a way to generalize it for any number, <em>n</em>, instead of just for primes, <em>p</em>, but couldn&#8217;t.  The problem tortured him for 24 years, when in 1760* he was finally able to produce his <a href="http://www.cut-the-knot.org/blue/Euler.shtml">proof</a>.</p>
<p>His proof makes use of a function he defines, phi(x).</p>
<p><a name="totient"></a><span style="text-decoration: underline;">Euler&#8217;s Totient Function</span></p>
<blockquote><p>For any positive integer, <em>n</em>, <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > is equal to the number of positive integers where GCD(<em>a, n</em>) = 1, for <em>a</em> &lt; <em>n</em>.</p>
<p>Most germane to this discussion, for any prime, <em>p</em>, <img src='/blog/wp-content/plugins/latexrender/pictures/1440b24e3db52e2fcb1b6cd9f6cb4035_3.5pt.gif' title='$\phi(n)$ = p-1' alt='$\phi(n)$ = p-1'  style="vertical-align:-3.5pt;" >. This makes perfect sense, of course, as a prime is indivisible, and therefore all numbers less than <em>p</em> don&#8217;t share factors with <em>p</em>, or else <em>p</em> could be divided!</p></blockquote>
<p>The formula ends up being:</p>
<p><span style="text-decoration: underline;">Euler&#8217;s Theorem</span></p>
<blockquote><p>For any positive integers <em>a, n</em>:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/7c39c47c1950b82a4fb0723bd97f801d_3.5pt.gif' title='$a^{\phi(n)} \equiv 1 (mod\ n)$' alt='$a^{\phi(n)} \equiv 1 (mod\ n)$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<p>* Wikipedia says 1736, <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FElementary-Number-Theory-Kenneth-Rosen%2Fdp%2F0321237072%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1199671115%26sr%3D8-2&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">my number theory book</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> says 1760, tie goes to the book.</p>
<p><strong><a name="euler_corollary"></a>Corollary to Euler&#8217;s Theorem</strong></p>
<p>Because <img src='/blog/wp-content/plugins/latexrender/pictures/7c39c47c1950b82a4fb0723bd97f801d_3.5pt.gif' title='$a^{\phi(n)} \equiv 1 (mod\ n)$' alt='$a^{\phi(n)} \equiv 1 (mod\ n)$'  style="vertical-align:-3.5pt;" >, we can reduce the multiplication needed for any power of <em>a</em> &gt; <em>n</em>.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/ff8bed3e1d7d25a9f3ff424b32df92d4_3.5pt.gif' title='$a^{b} \equiv a^{b (mod\ \phi(n))} (mod\ n)$' alt='$a^{b} \equiv a^{b (mod\ \phi(n))} (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><em>Edit: added link to Wikipedia&#8217;s article on Integer Factorization, and a better explanation of why the attack is hard</em></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=31&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Basic Combinatorics for Programmers</title>
		<link>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/</link>
		<comments>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 12:29:05 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Choice]]></category>
		<category><![CDATA[Combination]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Permutation]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/</guid>
		<description><![CDATA[Man only likes to count his troubles, but he does not count his joys. ~Fyodor Dostoevsky What is Combinatorics? Combinatorics is the math behind counting. All problems that start with the phrase &#8220;How many ways..&#8221; are most likely combinatorics problem. Who is this page for? Programmers who have a simple background in Mathematics, or need [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Man only likes to count his troubles, but he does not count his joys.<br />
~Fyodor Dostoevsky</em></p></blockquote>
<h3>What is Combinatorics?</h3>
<p>Combinatorics is the math behind counting. All problems that start with the phrase &#8220;How many ways..&#8221; are most likely combinatorics problem.</p>
<h3>Who is this page for?</h3>
<ul>
<li>Programmers who have a simple background in Mathematics, or need a quick reference for how to implement combinatorial algorithms.</li>
<li>Those who did not know what the word &#8220;combinatorics&#8221; meant.</li>
</ul>
<h3>Factorials</h3>
<p>Factorials are used to solve the problem, &#8220;How many ways can I arrange <em>n</em> objects?&#8221; It makes the following assumptions:</p>
<ul>
<li>Each object is distinguishable (Can I tell apart the <em>n</em> apples?)</li>
<li>You can actually distinguish arrangements of objects (Can I line up <em>n</em> apples in a meaningful way?)</li>
<li>The elements are non-replaceable (Once I use an apple, I can&#8217;t use an apple again).</li>
</ul>
<p>The factorial function is defined as follows for positive integers:<br />
Factorial(1) = 1<br />
Factorial(n) = n * Factorial(n-1)</p>
<p>or, the naieve C implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// For 32 bit systems, will not return meaningful results</span>
  <span style="color: #666666;">// for values larger than 12!</span>
  <span style="color: #666666;">//</span>
  <span style="color: #666666;">// For 64-bit systems, will not return meaningful results</span>
  <span style="color: #666666;">// for values larger than 20!</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span>n<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
        ret<span style="color: #000040;">*</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> ret<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The C implementation above is fine for the values it can logically compute. However, when we get into arbitrary precision integer calculations, calculating <em>1 * 2 * 3 * &#8230; * n</em> is inefficient! The number of words produced at the end of each step grows linearly. If we divide-and-conquer, we keep down the number of words-per-integer until the very last few multiplications, which produces significant time savings.</p>
<p>Of course, we would NEVER use this in the real world without extra sanity checking of the input. The importance of sanity checks in the real world is demonstrated every day by celebrities. Don&#8217;t commit their same blunders.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class factorial<span style="color: #008000;">&#40;</span>mpz_class begin, mpz_class end<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>end<span style="color: #000080;">==</span>begin<span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> end<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>end<span style="color: #000040;">-</span>begin <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> begin<span style="color: #000040;">*</span>end<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> half <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>begin<span style="color: #000040;">+</span>end<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span>begin, half<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>half<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>, end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>In fact, running the two against each other to find 100,000! produces the following runtimes on my laptop (I was going to do 1 million and got bored waiting for the iterative solution to finish!):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  g<span style="color: #000040;">++</span> factorial.<span style="color: #007788;">cpp</span> <span style="color: #000040;">-</span>O2 <span style="color: #000040;">-</span>lgmp <span style="color: #000040;">-</span>lgmpxx
  .<span style="color: #000040;">/</span>a.<span style="color: #007788;">out</span>
  Recursive factorial<span style="color: #008080;">:</span> <span style="color:#800080;">0.2795</span> seconds
  Iterative factorial<span style="color: #008080;">:</span> <span style="color:#800080;">3.01006</span> seconds</pre></div></div>

<p>The most efficient ways to implement factorial algorithms turns out to be from its prime factorization, but the above is far easier than implementing sieving methods to find primes! Maybe another day <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Permutations</h3>
<p>Permutations are how we solve the problem, &#8220;how many different ways can I arrange <em>k</em> objects from a list of <em>n</em> objects? To give an example, assuming the same number could not be used twice in a combination, your &#8220;combination&#8221; lock from high school was actually a permutation lock. You were given a single permutation that you could use in order to open it. But how many different possible permutations were there, given 45 as a max value and 3 numbers?</p>
<ul>
<li>The first number has no restrictions. You can choose from all 45 numbers</li>
<li>The second number is restricted: it can&#8217;t be the same as the first number. You now have 44 to choose from, giving you <em>45 * 44</em> different possible 2-permutations of 45</li>
<li>The third number is also restricted: You can only choose 43 numbers. <em>45 * 44 * 43 </em>is the number of 3-permutations of 45.</li>
</ul>
<p>It&#8217;s not hard to convince ourselves that the permutation is all of the numbers between and including 45 and 43 multiplied together. Symbolically, we are talking about all of the numbers between and including <em>n</em> and <em>(n &#8211; k + 1)</em>, inclusive.</p>
<p>The formula we all learned in school (which you can convince yourself is equivalent to the above) is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// Picking and ordering k objects from n objects</span>
  mpz_class permutation<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, n<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Never do this!</strong> You have to recompute many of the values that you never use, and it is worthless and computationally wasteful to do so. Instead, you can use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class permutation<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>k <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
          <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span>n <span style="color: #000040;">-</span> k <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>It&#8217;s much simpler. It computes much less. Use it. It is calling your name. Use it.</p>
<h3>Combinations</h3>
<p>Permutations are very closely related to combinations. In fact, there is only a one word difference in the definition. Combinations are <em>unordered</em>, and permutations are <em>ordered.</em> We can derive the formula by what this implies.</p>
<p>Let&#8217;s take our combination lock example. We needed to get three numbers in a row, in order. But what happens if we only need to select the three numbers without concern to the order? We know from the factorial section that the number of ways to order 3 numbers is 3 factorial. So let&#8217;s say we have the permutations { (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1) }. These are all the same combination, because they all pick the same exact elements! So there is clearly 1/6 the number of combinations as permutations. 6 is 3!, which is no coincidence. In general, the formula for combinations is:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class combination<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>k <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
          <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">return</span> permutation<span style="color: #008000;">&#40;</span>n, k<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>*Hits the Easy Button*</p>
<p><strong><em>Next week:</em></strong><em> More complicated number sequences such as the Binomial Coefficients and the Sterling Numbers</em></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=11&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Number Theory, Hash Tables, and Geometric Progressions</title>
		<link>http://www.jakevoytko.com/blog/2007/09/30/number-theory-hash-tables-and-geometric-progressions/</link>
		<comments>http://www.jakevoytko.com/blog/2007/09/30/number-theory-hash-tables-and-geometric-progressions/#comments</comments>
		<pubDate>Sun, 30 Sep 2007 17:48:13 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Geometric Sequence]]></category>
		<category><![CDATA[Hash table]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[Phi]]></category>
		<category><![CDATA[Primitive Root]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/09/30/number-theory-hash-tables-and-geometric-progressions/</guid>
		<description><![CDATA[Or, and Loathing in Los Vegas What will this article focus on? This particular article looks at geometric sequences (mod n), and how we can use them instead of linear hashes. A geometric sequence is simply a sequence of powers of some number: 1, , , , &#8230; So instead of adding the same number [...]]]></description>
			<content:encoded><![CDATA[<p>Or, <strong><img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" > and Loathing in Los Vegas</strong></p>
<h3>What will this article focus on?</h3>
<p>This particular article looks at geometric sequences (mod <em>n</em>), and how we can use them instead of linear hashes. A <strong>geometric sequence</strong> is simply a sequence of powers of some number: 1, <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661_1.0pt.gif' title='a' alt='a'  style="vertical-align:-1.0pt;" >, <img src='/blog/wp-content/plugins/latexrender/pictures/ebc3d7bedc1f11e08895c3124001cbb5_1.0pt.gif' title='a^2' alt='a^2'  style="vertical-align:-1.0pt;" >, <img src='/blog/wp-content/plugins/latexrender/pictures/0e12d972c205ea4de06749a887ff1ffe_1.0pt.gif' title='a^3' alt='a^3'  style="vertical-align:-1.0pt;" >, &#8230; So instead of adding the same number together a bunch of times, we&#8217;re multiplying it together a bunch of times. And then you subtract one. More on that below!</p>
<h3>First, the math</h3>
<p><strong>Euler&#8217;s Phi Function</strong></p>
<p>When Euler was attempting to generalize <a href="http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/">Fermat&#8217;s Little Theorem</a>, he defined a function using the Greek symbol <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" > (pronounced fee by most people I&#8217;ve encountered). It has a simple job: it takes in a natural number, <em>n</em>, and returns the number of positive integers less than <em>n</em> that are <a href="http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/">relatively prime </a>to <em>n</em>. In this article, we are not concerned with <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >&#8216;s calculation for anything but prime numbers.</p>
<p>It is easy to show that <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(p) = p-1 when p is prime: all numbers less than a prime are relatively prime to the prime in question, otherwise it wouldn&#8217;t be prime! Easy proof.</p>
<p>Euler&#8217;s phi function is of vital to the RSA encryption algorithm, and is the cornerstone of the generalization of Fermat&#8217;s Little Theorem, but it makes cameo appearances in many other areas of mathematics.</p>
<p>Examples:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(5) = 4, because gcd(5, 1) = gcd(5, 2) = gcd(5, 3), = gcd(5, 4) = 1.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(6) = 2, because gcd(6, 1) = gcd(6, 5) = 1, but gcd(6, 2) = 2, gcd(6, 3) = 3, and gcd(6, 4) = 2.</p>
<p><strong>Order of a number (mod n)</strong></p>
<p>The <strong>order</strong> of a number (mod n), where n is an integer, is the smallest positive value of <em>x</em> such that <img src='/blog/wp-content/plugins/latexrender/pictures/6043e99443887a278ea012378e2faf9a_3.5pt.gif' title='s^x \equiv 1(mod\ p)' alt='s^x \equiv 1(mod\ p)'  style="vertical-align:-3.5pt;" >. If it is never equal to 1, it is considered infinite. 6 (mod 10) is an example that never has an answer. Note that this still has a solution under Euler&#8217;s generalization of Fermat&#8217;s Little Theorem. The laws of the universe won&#8217;t let you off that easy.</p>
<p><strong>Example</strong>:</p>
<p>The order of 2 (mod 7) is 3, because [Unparseable or potentially dangerous latex formula. Error 1 ](prime) = prime-1, so <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(p) = p-1. If order(m) (mod p) is p-1, that means that m is a generator for all numbers (mod p) except p itself! Since this will not generate p, and 0 by extension, (since they are in the same congruence class), we must subtract our result by 1. So our generator is <em>m</em>, and our hash function is <img src='/blog/wp-content/plugins/latexrender/pictures/a37ed226c9e6b3696ff43f2451cd1c40.gif' title='a^x &amp;#8211; 1(mod\ prime)' alt='a^x &amp;#8211; 1(mod\ prime)'  align=absmiddle></p>
<p>It is not true that all numbers have a primitive root, but it WAS proved by Legendre that every prime has at least one generator (mod p). Interestingly, according to my college Number Theory textbook, Euler tried his hand at the proof, but was incorrect. To the uninitiated into the <em>Cult of Euler,</em> this would be akin to a team of Michael Jordan clones failing to score a single point in a basketball game against a team of middle school students.</p>
<p>We need to find one such that the first time this happens is for a power of p-1. Instead of testing every power, we can instead (because of this proof), just test powers where the power divides p-1. If we were looking mod 9, and we knew 3^8 == 1(mod p) (which it has to be because of Fermat&#8217;s Little Theorem), then <img src='/blog/wp-content/plugins/latexrender/pictures/d7b213cee95b4b6b3ab6b90cadfed175_1.0pt.gif' title='3^1' alt='3^1'  style="vertical-align:-1.0pt;" >, <img src='/blog/wp-content/plugins/latexrender/pictures/15a774bb3441106ae6145acd8b634821_1.0pt.gif' title='3^2' alt='3^2'  style="vertical-align:-1.0pt;" >, <img src='/blog/wp-content/plugins/latexrender/pictures/a0faf7b4c911b1fd4448c87db5067057_1.0pt.gif' title='3^4' alt='3^4'  style="vertical-align:-1.0pt;" >, and <img src='/blog/wp-content/plugins/latexrender/pictures/130693682fe4d9d5612c6bc6f7df878f_1.0pt.gif' title='3^8' alt='3^8'  style="vertical-align:-1.0pt;" > are the only possible powers that can be equal to one. We will call this the <strong>generator test</strong>. We can check these particular values quickly through successive squaring. If any of the powers of 3 less than 8 are congruent to 1, then we have a failure, and it is not a generator.</p>
<p>If you do not have access to a good way to factor p-1, the following naive method will work well for small numbers. Please note that the preferable way is to factor p-1 and to find all of the divisors of p-1 that way.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ***********************************************************************</span>
<span style="color: #666666;">// Precondition: p is a prime. If it is not, it will return 0 indicating</span>
<span style="color: #666666;">// failure</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// This assumes that you are trying to do this for a small p, without being</span>
<span style="color: #666666;">// able to factorize p-1 quickly.</span>
<span style="color: #666666;">// ************************************************************************</span>
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> find_generator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> p<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> phi_p<span style="color: #008000;">&#40;</span>p<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> test_powers<span style="color: #008080;">;</span> 
&nbsp;
  <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span> 
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i
<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>i<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>powmod<span style="color: #008000;">&#40;</span>test, test_powers<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, p<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	      found <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
      <span style="color: #008000;">&#125;</span> 
&nbsp;
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>found<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
	  <span style="color: #0000ff;">return</span> test<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>So what?</h3>
<p>If we have an element a (mod n) who has <img src='/blog/wp-content/plugins/latexrender/pictures/d16434743153552f195e740c1f93bd26_1.0pt.gif' title='a^{n-1} = 1' alt='a^{n-1} = 1'  style="vertical-align:-1.0pt;" >, and <img src='/blog/wp-content/plugins/latexrender/pictures/433d6e4ef1439c920d75200519547732_1.0pt.gif' title='a^{positive\ integer\ less\ than\ n}' alt='a^{positive\ integer\ less\ than\ n}'  style="vertical-align:-1.0pt;" > is not equal to 1, we have a<strong> generator</strong>. The generator is for a set of integers of size (p-1), which is even.</p>
<h3>Finding generators is nontrivial</h3>
<p>A downside to this method is that there is no free lunch when it comes to finding generators. You have to find one, although fortunately for us, most numbers have generators that are less than 10, so you can find them by linearly searching. There are a few strategies of how we can pick primes that will allow us to (relatively) quickly find a generator (mod p). The one I use is:</p>
<p>One strategy is finding a prime, <em>p</em>, such that <em>q = </em>2*<em>p</em> + 1 is also prime. The only two numbers that you have to check that violate our generator condition are 2 and p, in which case <em>q</em> is a generator. This helps reduce the complexity of the test. How do we know if our numbers are prime? Probabilistic primality testing, of course <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . It&#8217;s amazing how all of this stuff ties together.</p>
<p>A professor I had for a cryptology course said that the odds of the first generator NOT being less than 10 has been shown to be inordinately small, but I can&#8217;t for the life of me find any sort of reference to a figure that states that. As there is no trivial way to find a hash function, it is acceptable to search for the first generator (mod p) linearly, using our generator test, if you are looking for just any generator of p. Likewise, you can also find the largest such generator (mod p) by reverse searching.</p>
<p><strong>This is so complicated. Why would I use this over a linear hash?</strong></p>
<ul>
<li>The elements selected are not at a fixed interval, so data is usually less likely to cluster, which results in fewer collisions</li>
<li>It does better at the <strong>avalanche test</strong>, which says that when a bit of the input changes, at least half of the bits of the output should change. The linear hash fails miserably at this, and geometric hashes (depending on your generator, of course), perform better than their linear counterparts.</li>
</ul>
<p>Sometime in the future, (not in the next post, though), I will develop benchmarks to see what is better to deal with various different input scenarios. There&#8217;s no sense in developing the mathematics if we don&#8217;t actually put it all on the line and see if the &#8220;better&#8221; method works better in the real world. The real world has an amazing way of yelling &#8220;surprise!&#8221;, but we can limit that surprise through testing, testing, testing.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=15&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/09/30/number-theory-hash-tables-and-geometric-progressions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Number Theory for Programmers, Part 2</title>
		<link>http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/</link>
		<comments>http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 21:40:46 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[GCD]]></category>
		<category><![CDATA[Greatest Common Divisor]]></category>
		<category><![CDATA[Hash]]></category>
		<category><![CDATA[Hash table]]></category>
		<category><![CDATA[Number Theory]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/</guid>
		<description><![CDATA[What is Number Theory? Number theory is the study of numbers, their properties, and what can be inferred from their properties. For programmers, it is most practical to focus on the theory of positive integers. Who should use this guide? Those who did not know the answer to the above question Those who are interested [...]]]></description>
			<content:encoded><![CDATA[<h3>What is Number Theory?</h3>
<p>Number theory is the study of numbers, their properties, and what can be inferred from their properties. For programmers, it is most practical to focus on the theory of positive integers.</p>
<h3>Who should use this guide?</h3>
<ul>
<li>Those who did not know the answer to the above question</li>
<li>Those who are interested in the math behind hash functions</li>
<li>Those who found my last article interesting</li>
</ul>
<h3>What will this article focus on?</h3>
<p>This article will focus on using the integers (mod <em>n</em>) as indices of a <a href="http://en.wikipedia.org/wiki/Hash_table">hash table</a>, and the math behind different choices of hash functions. Our goal is to find a &#8220;good&#8221; hash function (see below). The mathematical explanation will be done irrespective of Group Theory, and I may write another article to look at a hash table as a group over addition or multiplication of the integers (mod <em>n</em>). For a quick refresher of the (mod <em>n</em>) concept, go <a href="http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/">here</a>, or for another explanation, please look <a href="http://www.math.csusb.edu/notes/rel/node4.html">here</a>.</p>
<h3>Useful Tools</h3>
<h3>Greatest Common Divisor (GCD) of positive integers</h3>
<p><strong>Explanation:</strong></p>
<p>Mathematically, the greatest common divisor of two numbers a and b is the product of all common divisors of a and b. For a simple explanation as to why, look <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm#Proof">here</a>.</p>
<p><strong>The Algorithm:</strong></p>
<p><strong>Naive</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> gcd<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> remaind<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> b<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #666666;">// gcd(a, 0) = a</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>a <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> b<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        a <span style="color: #000040;">^</span><span style="color: #000080;">=</span> b<span style="color: #008080;">;</span>  <span style="color: #666666;">// Swap a and b in place</span>
        b <span style="color: #000040;">^</span><span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
        a <span style="color: #000040;">^</span><span style="color: #000080;">=</span> b<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>remaind <span style="color: #000080;">=</span> a <span style="color: #000040;">%</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        a <span style="color: #000080;">=</span> b<span style="color: #008080;">;</span>
        b <span style="color: #000080;">=</span> remaind<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Binary: </strong>(It&#8217;s <strong>always</strong> worth it to try to find the algorithms that take advantage of working with bits. If life gives you an integer as the sum of powers of two, make lemonade <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</p>
<p>Wikipedia has a <a href="http://en.wikipedia.org/wiki/Binary_GCD_algorithm">page</a> that explains a binary algorithm that takes advantage of the binary format of the data. It reduces the problem by stripping out common multiples of two, and then applying the binary analogy of the GCD algorithm. For more details, follow the above link. I haven&#8217;t benchmarked it, but it relies heavily on bit operations, so it should run a little faster on modern popular architectures.</p>
<h3>Least Common Multiple (LCM) of positive integers</h3>
<p><strong>Explanation:</strong></p>
<p>The least common multiple is as it sounds: the smallest multiple that both <em>a</em> and <em>b</em> share. For example:<br />
LCM(15, 20) = 60.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/cb3e98c4c0a1ad7600b28db8a0587ce6_1.0pt.gif' title=' 15 = 3^{1} * 5^{1} ' alt=' 15 = 3^{1} * 5^{1} '  style="vertical-align:-1.0pt;" ><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/25f505db899a6b15e31310cfe2837b22_1.0pt.gif' title='20 = 2^{2} * 5^{1}' alt='20 = 2^{2} * 5^{1}'  style="vertical-align:-1.0pt;" ><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/8fd773a9cfb91b509f5943cfeed1ae0d_1.0pt.gif' title='60 = 3^{1} * 2 ^{2} * 5^{1}' alt='60 = 3^{1} * 2 ^{2} * 5^{1}'  style="vertical-align:-1.0pt;" ></p>
<p>It appears that for each prime, the LCM of <em>a</em> and <em>b</em> includes the largest power from either <em>a</em> or <em>b</em>. In fact, this is true.</p>
<h3>Relation between GCD and LCM</h3>
<p>For integers a and b:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/ed271db0080f343fce6f6125b77c3872_3.5pt.gif' title='LCM(a, b)\ *\ GCD(a, b)\ =\ a\ *\ b' alt='LCM(a, b)\ *\ GCD(a, b)\ =\ a\ *\ b'  style="vertical-align:-3.5pt;" ></p>
<p>This is very powerful, and lets us efficiently calculate the LCM of a and b by dividing out the GCD of a * b. Why does this work? If <em>a</em> and <em>b</em> don&#8217;t have any prime factors in common, clearly the only way that we can have a multiple of <em>a</em> equal some multiple of <em>b</em> is by multiplying <em>b</em> by <em>a</em>. If <em>a</em> and <em>b</em> only have one prime factor in common (let&#8217;s call it <em>d</em>), if you multiply <em>a</em> by <em>b</em>, we get a*b as an answer. However, (a*b)/d is clearly a multiple of both <em>a</em> and <em>b</em>. We don&#8217;t need to multiply <em>a</em> by <em>d</em>, since <em>a</em> already HAS <em>d</em> as a factor. <em>d</em> is uncoincidentally the GCD of <em>a</em> and <em>b</em>, and clearly, GCD(a, b) * LCM(a, b) = a * b. An actual proof is left as an exercise to the reader.</p>
<h3>What makes a good hash // hash table?</h3>
<p>The short answer is that nobody knows. Hashes that work well for some kinds of inputs can produce intractable results for other kinds of input. For our purposes, we will say that a good hash function minimizes the odds of two different inputs ending up in the same congruence class (mod <em>n</em>). When two different inputs DO end up in the same index, this is called a <strong>collision</strong>, and is as undesirable in hash tables as it is while driving. Also bad is <strong>clustering</strong>, which is when collisions are much more likely to happen in certain indices than in other indices.</p>
<p>Ideally, we would like the hash function to be able to place elements at any index in the table. This makes it a <strong>generator</strong>, namely, it can generate any value in the table.</p>
<p>We will try to find a happy medium of all concerns through experimentation. I will define a few different hash functions in the upcoming articles, and then will show how to compare them. That will be where the &#8220;<a href="http://www.xkcd.com/store/try_science_shirt_300.png">Science</a>&#8221; part of Computer Science enters the picture <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Linear Hashes<br />
</strong></p>
<p>Linear hashes take in some number <em>x</em>, and place the object in the index <em>ax </em>+ <em>b </em>(mod <em>n</em>). To make the mathematics easier, we will just use <em>ax</em>(mod <em>n</em>), as it should be obvious that adding <em>b</em> produces the set in the same order, but with a different starting point. In order for us to consider <em>a</em> as a hash function, <em>a</em> must be a generator (mod <em>n</em>). How do we know that it does that? Let&#8217;s look at a few different values of <em>a</em> (mod 16).</p>
<p><em><img src='/blog/wp-content/plugins/latexrender/pictures/701067ed5d646af1c269d1bb85bd3e69_1.0pt.gif' title='2^1 = 2' alt='2^1 = 2'  style="vertical-align:-1.0pt;" > </em>: {2, 4, 6, 8, 10, 12, 14, 0, 2} (mod 16) (doesn&#8217;t generate the integers (mod 16))</p>
<p><em><img src='/blog/wp-content/plugins/latexrender/pictures/d7b213cee95b4b6b3ab6b90cadfed175_1.0pt.gif' title='3^1' alt='3^1'  style="vertical-align:-1.0pt;" > = 3</em>: {3, 6, 9, 12, 15, 2, 5, 8, 11, 14, 1, 4, 7, 9, 12, 15, 3} (mod 16) (generates the integers (mod 16))</p>
<p><em>2*3 = 6</em>: {6, 12, 2, 8, 14, 4, 10, 0, 6} (mod 16) (doesn&#8217;t generate the integers (mod 16)).</p>
<p>So what works? It works when gcd(<em>a</em>, <em>n</em>) = 1. This is known as being <strong>relatively prime</strong> or <strong>coprime</strong>, meaning they don&#8217;t share any common prime factors. <img src='/blog/wp-content/plugins/latexrender/pictures/d7b213cee95b4b6b3ab6b90cadfed175_1.0pt.gif' title='3^1' alt='3^1'  style="vertical-align:-1.0pt;" > and <img src='/blog/wp-content/plugins/latexrender/pictures/27eac782422adb62c41a6f3c2c99a5d1_1.0pt.gif' title='2^4' alt='2^4'  style="vertical-align:-1.0pt;" > obviously don&#8217;t share any prime factors, so 3 is a generator using addition (mod 16).</p>
<p>Why does that work?  The largest possible multiple of a that will give us 0 (mod n) is n, because a*n == a * 0 == 0 (mod n). We need to make the LCM of a and n equal to a * n, and since we know that LCM(a, n) = a * n / GCD(a, n), it follows that GCD(a, n) = 1.</p>
<p>Since most hash tables you make will have 2^n elements (this seems to be the standard, for addressing reasons), any odd number <em>a</em> will suffice to be a generator for linear hashes.</p>
<p><strong>Theoretically, which hash value should I use?</strong></p>
<p>Linear hashing is obviously a very simple hash function (the simplest one there is, I believe), and therefore, there is not a single hash fucntion that will work for every input set. In fact, this type of hash will have many input sets that will make it have very poor performance. However, if we have advanced knowledge of the kind of data that will be the input, we can stack the deck in our favor.</p>
<p>If your data is guaranteed to have no collisions (mapping unique integers less than the size of the hash table to some value), you can use any positive integer you want as your hash. I recommend 1 for ease of calculation <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If your data is sorted ascending, use hash values close to 1. If you can find the mode of the data in advance, you can yourself by setting the hash value larger than the mode. If the mode is large with respect to the size of the hash table or with respect to the size of the data set, you can make the hash value larger than the average number of repetitions for each input.</p>
<p>If your data is sorted descending, you want to do as above, except make your hash value close to n-1. The reasoning can be derived from the above paragraph.</p>
<p>If your data is either purely random, or of several different varieties, your hash function is not always going to work no matter how hard you try. We should avoid hashes close to <em>1</em> and <em>n-1, </em>but other than that, we will need to benchmark to see if there is a better value.</p>
<p><script type="text/javascript"><!--
  amazon_ad_tag = "jakvoyshom-20";  amazon_ad_width = "468";  amazon_ad_height = "60";
// --></script><br />
<script src="http://www.assoc-amazon.com/s/ads.js" type="text/javascript"></script></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=14&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Number Theory for Programmers, Part 1</title>
		<link>http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/</link>
		<comments>http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/#comments</comments>
		<pubDate>Sun, 16 Sep 2007 20:19:19 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Fermat's Method]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[Primality Testing]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/</guid>
		<description><![CDATA[What is Number Theory? Number theory is the study of numbers, their properties, and what can be inferred from their properties. For programmers, it is most practical to focus on the theory of positive integers. Who should use this guide? Those who did not know the answer to the above question. How do we use [...]]]></description>
			<content:encoded><![CDATA[<h3>What is Number Theory?</h3>
<p>Number theory is the study of numbers, their properties, and what can be inferred from their properties. For programmers, it is most practical to focus on the theory of positive integers.</p>
<h3>Who should use this guide?</h3>
<p>Those who did not know the answer to the above question.</p>
<h3>How do we use modulus?</h3>
<p>First, we should bridge the gap between a Programmer&#8217;s definition of modulus and a Mathematician&#8217;s.</p>
<p><strong>Programmer</strong>: <em>a % b</em> is the remainder of <em>a / b</em>. Essentially, the programmer uses the following equation:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/5c0bab0c3f7fbc8b463ff5d78236973c_1.83333pt.gif' title='a = b*c + r' alt='a = b*c + r'  style="vertical-align:-1.83333pt;" ></p>
<p>That is, the programmer says If we were finding 23 % 5, we would have:<br />
<img src='/blog/wp-content/plugins/latexrender/pictures/ef9bbd57cbb49f7fd02f8ae7e6b3ef03_1.83333pt.gif' title='23 = 5*4 + 3' alt='23 = 5*4 + 3'  style="vertical-align:-1.83333pt;" ><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/37693cfc748049e45d87b8c7d8b9aacd_1.0pt.gif' title='23' alt='23'  style="vertical-align:-1.0pt;" > % <img src='/blog/wp-content/plugins/latexrender/pictures/4c31ffa20fa4cbd5d1cea980f758157e_1.0pt.gif' title='5 = 3' alt='5 = 3'  style="vertical-align:-1.0pt;" >.</p>
<p><strong>Mathematician</strong>: Mathematicians rearrange the above equation into the following:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/a8abb65be73f763a005feeaa5ce2189d.gif' title='a &amp;#8211; r = bc' alt='a &amp;#8211; r = bc'  align=absmiddle>. They write this as:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/77b99e9bc51689a475314f0937b4128a_3.5pt.gif' title='a \equiv r ($mod$\  b)' alt='a \equiv r ($mod$\  b)'  style="vertical-align:-3.5pt;" ></p>
<p>All this means is that you can move from <em>a </em>to <em>r</em> just by adding and subtracting <em>b</em>.<br />
23 &#8211; 5 &#8211; 5 &#8211; 5 &#8211; 5 = 3, so <img src='/blog/wp-content/plugins/latexrender/pictures/e7aedcd748c0f4ae851ea5f6827deb17_3.5pt.gif' title='23 \equiv 3 (mod\ 5)' alt='23 \equiv 3 (mod\ 5)'  style="vertical-align:-3.5pt;" ></p>
<h3>Efficient Exponentiation (mod <em>n</em>)</h3>
<p>Let&#8217;s say that you need to find <img src='/blog/wp-content/plugins/latexrender/pictures/014b154b2ba42fb9e1c80d1b02804839_3.5pt.gif' title='a ^{x} (mod\ n)' alt='a ^{x} (mod\ n)'  style="vertical-align:-3.5pt;" >. The naive way of doing this is to perform the operation just as it&#8217;s written:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> powmod<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> pow<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> n<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and it has a few disadvantages.</p>
<ol>
<li>It has a very good chance of overflowing native data types</li>
<li>It has an algorithmic complexity of O(<em>n</em>) for the size of the exponent. For large integer types, this becomes O(<em>nm</em>), for integers of (on average) <em>m</em>words</li>
</ol>
<p>To show a more efficient way of doing it, we will use a method called &#8220;successive squaring&#8221;. I will explain it by using an example: Find <img src='/blog/wp-content/plugins/latexrender/pictures/7799c43c67f34958b77d6a3ab07cb9cd_3.5pt.gif' title='3 ^ {17} (mod\ 5)' alt='3 ^ {17} (mod\ 5)'  style="vertical-align:-3.5pt;" >:</p>
<p>We know that <img src='/blog/wp-content/plugins/latexrender/pictures/591e0b302dcbce1b6385784731609932_3.5pt.gif' title='3 ^ {17} (mod\ 5) \equiv 3^{16} * 3^{1} (mod\ 5)' alt='3 ^ {17} (mod\ 5) \equiv 3^{16} * 3^{1} (mod\ 5)'  style="vertical-align:-3.5pt;" >. We need to find <img src='/blog/wp-content/plugins/latexrender/pictures/d6ec409f0a2cdff55a22279c47a62839_3.5pt.gif' title='3^{16}(mod\ 5)' alt='3^{16}(mod\ 5)'  style="vertical-align:-3.5pt;" >:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/f14c04e5d758ab7d82fc6d382212a202_3.5pt.gif' title='3 \equiv 3 (mod\ 5)' alt='3 \equiv 3 (mod\ 5)'  style="vertical-align:-3.5pt;" >. This is given.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/662626b01c91a3c6caf8f96bbc845c6a_3.5pt.gif' title='3^{2} \equiv 9 \equiv 4 (mod\ 5)' alt='3^{2} \equiv 9 \equiv 4 (mod\ 5)'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/2677fb23d7eff3992f854c22b41a4e3a_3.5pt.gif' title='3^{4} \equiv (3^{2})^{2} \equiv 4^{2} \equiv 16 \equiv 1 (mod\ 5)' alt='3^{4} \equiv (3^{2})^{2} \equiv 4^{2} \equiv 16 \equiv 1 (mod\ 5)'  style="vertical-align:-3.5pt;" >.</p>
<p>This is where the leap of logic occurs. Since <img src='/blog/wp-content/plugins/latexrender/pictures/1ecc2e4b54b1677c15c57efaf3478cdf_3.5pt.gif' title='3^{2} \equiv 4 (mod\ 5)' alt='3^{2} \equiv 4 (mod\ 5)'  style="vertical-align:-3.5pt;" >, it follows that <img src='/blog/wp-content/plugins/latexrender/pictures/f047db04c4e0aee3a1af6e4054225db2_3.5pt.gif' title='3^{4} \equiv (3^{2})^{2}' alt='3^{4} \equiv (3^{2})^{2}'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/463eadc74dbf9aad216b55449c95e656_3.5pt.gif' title='3^{8} \equiv 1 (mod\ 5)' alt='3^{8} \equiv 1 (mod\ 5)'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/de98530786b1586bd9f2c4b8084b8bc0_3.5pt.gif' title='3^{16} \equiv 1 (mod\ 5)' alt='3^{16} \equiv 1 (mod\ 5)'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/d7434bcad4ce2e8d6b23330250ed4297_3.5pt.gif' title='3^{17} = 3^{16} * 3^{1} = 1 * 3 \equiv 3 (mod\ 5)' alt='3^{17} = 3^{16} * 3^{1} = 1 * 3 \equiv 3 (mod\ 5)'  style="vertical-align:-3.5pt;" ></p>
<p>So <img src='/blog/wp-content/plugins/latexrender/pictures/f281266801a868c3ea6bb494f86bf350_1.0pt.gif' title='3^{17}' alt='3^{17}'  style="vertical-align:-1.0pt;" > % 5 = 3, and I was able to do it all in my head! For small numbers, this is usually the case. But it should be obvious that this is a lot easier than ordinary exponentiation, with on the order of O(log<em>n</em>) multiplications.</p>
<p><strong>Code example</strong></p>
<p>The best code example I have found is from Bruce Schneier&#8217;s &#8220;Applied Cryptography&#8221;. The C version using native unsigned integers is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> powmod<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> base<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> exp<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mod<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> toret<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>exp <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>exp <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
            toret <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>toret <span style="color: #339933;">*</span> base<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> mod<span style="color: #339933;">;</span>
&nbsp;
        exp <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
        base<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>base<span style="color: #339933;">*</span>base<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> mod<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> toret<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;ll still overflow for the wrong values, but it is a quick and dirty example. If you have access to an infinite precision integer, it should be trivial to convert it.</p>
<h3>Fermat&#8217;s Little Theorem</h3>
<p>One of the many things that Fermat conjectured (and supposedly proved) is quite useful to the modern programmer. It says, for any prime number <em>p</em>, and for any integer <em>a</em></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/bda5a769e75fe05fb287502fa9558be7_3.5pt.gif' title='a^{p} \equiv a (mod\ p)' alt='a^{p} \equiv a (mod\ p)'  style="vertical-align:-3.5pt;" >.</p>
<p>Combined with the successive squaring method, this provides us a very powerful tool.</p>
<h3>Probabilistic Primality Testing</h3>
<p>For any number of applications, we need prime numbers. They are the crack-cocaine of modern mathematics. There are many simple ways to get prime numbers, such as the Sieve of Eratosthenes, but these methods fail when your application needs a 20-digit prime. There are newly developed (but complicated) tests that give a definite yes/no on a number in polynomial time, but they require Abstract Algebra, which is beyond the scope of this entry! For most developers, we don&#8217;t need to be 100% sure the numbers we are using are prime. We&#8217;re not using RSA in life-or-death (or multi-billion dollar banking) situations! All we want to do is tell whether or not an integer is most likely prime so that we can encrypt our <em>Dawson&#8217;s Creek</em> fan fiction and hide it from our father.</p>
<p>Fermat&#8217;s Little Theorem is always true if we know that the modulus is prime. The proof, however, doesn&#8217;t hold true in the opposite direction: if, for some number <em>a</em>, <img src='/blog/wp-content/plugins/latexrender/pictures/9ac82e8fab98ceb651fe26b7c6ccf38b_3.5pt.gif' title='a^{n} \equiv a (mod\ n)' alt='a^{n} \equiv a (mod\ n)'  style="vertical-align:-3.5pt;" >, we can&#8217;t say for sure that <em>n</em> is a prime. However, it is very frequently true, and often enough that we can form a probabilistic test, meaning that the numbers are probably prime. Mathematicians are noted for devastating understatement, so when we say &#8220;probably&#8221;, we mean &#8220;the chance is absurdly close to 100%&#8221;. According to <a href="http://www.it.pgp.net/pgp-faq/faq-appendix2.html#2.3">pgp.net</a>, PGP uses trial division for primes less than 8191, and the Fermat test for 2,3,5, and 7. I can&#8217;t find a reliable source covering the mathematics of why, but an <a href="http://en.wikipedia.org/wiki/Fermat_primality_test">unreliable source</a> gives the chance that a composite is picked as less than 1 in <img src='/blog/wp-content/plugins/latexrender/pictures/67d474f8b10467f44715eca2c9ac5770_1.0pt.gif' title='10^{50}' alt='10^{50}'  style="vertical-align:-1.0pt;" >. Yikes!</p>
<p><strong>The Test</strong></p>
<p>Ready?</p>
<p>For some number <em>n</em>, it is probably prime if:</p>
<ol>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/126cef28aa6e7c5dd284390a1d12af3e_3.5pt.gif' title='2^{n} \equiv 2 (mod\ n)' alt='2^{n} \equiv 2 (mod\ n)'  style="vertical-align:-3.5pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/f7d8dd0dd38aa6f49de7b3f30580c593_3.5pt.gif' title='3^{n} \equiv 3 (mod\ n)' alt='3^{n} \equiv 3 (mod\ n)'  style="vertical-align:-3.5pt;" ></li>
</ol>
<p>If this makes you uncomfortable by using the first two primes, you can randomly pick two numbers (instead of 2 and 3). The test works just the same. For the ultra paranoid, try it three or four times.</p>
<p><strong>The Carmichael Numbers</strong></p>
<p>There are numbers that cause this test to fail for all test values. They are called <a href="http://mathworld.wolfram.com/CarmichaelNumber.html">Carmichael numbers</a>, named after the first person to find an example. The first three are 561, 1105, and 1729. There are infinitely many Carmichael numbers, though they grow more scarce as the integers approach infinity. For fun, use the method of successive squaring to show that 561 is a Carmichael number.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=12&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
