<?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>Christian Schenk&#187; math</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 04 Dec 2011 23:43:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Hot math: twin and sexy prime numbers</title>
		<link>http://www.christianschenk.org/blog/math-twin-sexy-prime-numbers/</link>
		<comments>http://www.christianschenk.org/blog/math-twin-sexy-prime-numbers/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 05:12:06 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[primes]]></category>
		<category><![CDATA[sexy]]></category>
		<category><![CDATA[twin]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=722</guid>
		<description><![CDATA[Just recently I discovered that there&#8217;s something called sexy prime numbers. I read about twin primes but hadn&#8217;t heard the other term before. The concept is pretty simple: take a prime number and check whether the next prime minus the first one results in a certain number, e.g. 2 for twin primes or 6 for [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently I discovered that there&#8217;s something called <em>sexy</em> prime numbers. I read about twin primes but hadn&#8217;t heard the other term before. The concept is pretty simple: take a prime number and check whether the next prime minus the first one results in a certain number, e.g. 2 for twin primes or 6 for sexy primes. This post presents a simple solution to calculate arbitrary combinations of prime number pairs, triplets, etc.</p>
<p>The Eclipse project with the code for this post can be downloaded as <a href="http://data.christianschenk.org/math-twin-sexy-primes/PrimeGroups.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/math-twin-sexy-primes/PrimeGroups.zip">zip</a>. You can browse the code online <a href="http://data.christianschenk.org/math-twin-sexy-primes/xref/index.html">here</a>.</p>
<p><span id="more-722"></span></p>
<h2>Preliminary</h2>
<p>It&#8217;s likely that you already know how to calculate prime numbers but I&#8217;d like to show you a feasible approach anyway. The idea is based on the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">sieve of Eratosthenes</a>: starting with the number 2, use it as a prime number if it isn&#8217;t divisible by any other prime number otherwise discard it. The advantage: easy to implement, the disadvantage: not that fast.</p>
<p>If we&#8217;d like to calculate the prime numbers starting with 2, up to 500 we could write the following code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">500</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDivisibleByPrime<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
  primes.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This implementation is simple yet fast enough for my purposes. Note that using Java&#8217;s primitive types it&#8217;s not possible to find prime numbers of arbitrary length; more work is required here if you want to do that.</p>
<h2>Arbitrary groups of primes</h2>
<p>What I call a group here is a set of prime numbers differing by a particular number. One example would be triplets of prime number that differ by six, called the sexy prime triplets. You could think of many combinations, i.e. twins, triplets or quadruplets where the numbers differ by e.g. 2, 3 or 6.</p>
<p>The code that finds these sets of prime numbers is straight forward. It generates a list with prime numbers, searches for a given pattern &#8211; e.g. sexy prime triplets &#8211; and prints the results. By changing the parameters you can make up any combination you like but keep in mind that some will return an empty result.</p>
<p>Check out the file <code>PrimeGroups</code>, specifically the top of the <code>main</code> method. Try chaning the paramters <code>start</code>, <code>end</code>, <code>amount</code> and <code>distance</code>. Did you expect to get that many results for a certain pattern? It can be quite entertaining changing <code>amount</code> and <code>distance</code>, don&#8217;t you think?</p>
<h2>Conclusion</h2>
<p>It&#8217;s easy and interesting to search prime numbers that match certain criteria. Have a look at the code and you&#8217;ll see that the implementation is pretty easy and doesn&#8217;t require extra coding skills if you don&#8217;t want to search for particularly large primes. Finally, it&#8217;s a nice exercise &#8211; even as a math novice &#8211; thinking about numbers and prime numbers in particular.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/math-twin-sexy-prime-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

