<?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>Politics &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/politics/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Determine if a Number is Prime</title>
		<link>/2012/05/08/computer-algorithms-determine-if-a-number-is-prime/</link>
		<comments>/2012/05/08/computer-algorithms-determine-if-a-number-is-prime/#comments</comments>
		<pubDate>Tue, 08 May 2012 20:42:40 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Eratosthenes]]></category>
		<category><![CDATA[ineffective algorithm]]></category>
		<category><![CDATA[Integer factorization algorithms]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[Primality tests]]></category>
		<category><![CDATA[Prime number]]></category>
		<category><![CDATA[Quadratic sieve]]></category>
		<category><![CDATA[Sieve of Atkin]]></category>
		<category><![CDATA[Sieve of Eratosthenes]]></category>

		<guid isPermaLink="false">/?p=3100</guid>
		<description><![CDATA[Introduction Each natural number that is divisible only by 1 and itself is prime. Prime numbers appear to be more interesting to humans than other numbers. Why is that and why prime numbers are more important than the numbers that are divisible by 2, for instance? Perhaps the answer is that prime numbers are largely &#8230; <a href="/2012/05/08/computer-algorithms-determine-if-a-number-is-prime/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Determine if a Number is Prime</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/" rel="bookmark" title="Computer Algorithms: How to Determine the Day of the Week">Computer Algorithms: How to Determine the Day of the Week </a></li>
<li><a href="/2011/12/12/computer-algorithms-jump-search/" rel="bookmark" title="Computer Algorithms: Jump Search">Computer Algorithms: Jump Search </a></li>
<li><a href="/2011/11/24/computer-algorithms-sequential-search/" rel="bookmark" title="Computer Algorithms: Sequential Search">Computer Algorithms: Sequential Search </a></li>
<li><a href="/2011/12/26/computer-algorithms-binary-search/" rel="bookmark" title="Computer Algorithms: Binary Search">Computer Algorithms: Binary Search </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Each natural number that is divisible only by 1 and itself is prime. Prime numbers appear to be more interesting to humans than other numbers. Why is that and why prime numbers are more important than the numbers that are divisible by 2, for instance? Perhaps the answer is that prime numbers are largely used in cryptography, although they were interesting for the ancient Egyptians and Greeks (Euclid has proved that the prime numbers are infinite circa 300 BC). The problem is that there is not a formula that can tell us which is the next prime number, although there are algorithms that check whether a given natural number is prime. It&#8217;s very important these algorithms to be very effective, especially for big numbers.</p>
<h2>Overview</h2>
<p>As I said each natural number that is divisible only by 1 and itself is prime. That means that 2 is the first prime number and 1 is not considered prime. It’s easy to say that 2, 3, 5 and 7 are prime numbers, but what about 983? Well, yes 983 is prime, but how do we check that? If we want to know whether <strong>n</strong> is prime the very basic approach is to check every single number between 2 and n. It’s kind of a brute force.</p>
<h2>Implementation</h2>
<p>The basic implementation in PHP for the very basic (brute force) approach is as follows.</p>
<p><script src="https://gist.github.com/stoimen/640e6c0492d50904f3d6.js?file=prime_v1.php"></script></p>
<p>Unfortunately this is one very ineffective algorithm. We don’t have to check every single number between 1 and n, it’s enough to check only the numbers between 1 and n/2-1. If we find such a divisor that will be enough to say that <strong>n</strong> isn’t prime.</p>
<p><script src="https://gist.github.com/stoimen/640e6c0492d50904f3d6.js?file=prime_v2.php"></script></p>
<p>Although that code above optimizes a lot our first prime checker, it’s clear that for large numbers it won&#8217;t be very effective. Indeed checking against the interval [2, n/2 -1] isn’t the optimal solution. A better approach is to check against [2, sqrt(n)]. This is correct, because if <strong>n</strong> isn’t prime it can be represented as p*q = n. Of course if p > sqrt(n), which we assume can&#8217;t be true, that will mean that q &lt; sqrt(n).</p>
<p><script src="https://gist.github.com/stoimen/640e6c0492d50904f3d6.js?file=prime_v3.php"></script></p>
<p>Beside that these implementations shows how we can find prime number, they are a very good example of how an algorithm can be optimized a lot with some small changes.</p>
<h3>Sieve of Eratosthenes</h3>
<p>Although the sieve of Eratosthenes isn’t the exact same approach (to check whether a number is prime) it can give us a list of prime numbers quite easily. To remove numbers that aren’t prime, we start with 2 and we remove every single item from the list that is divisible by two. Then we check for the rest items of the list, as shown on the picture below.</p>
<p><img src="/wp-content/uploads/2012/05/SieveofEratosthenes.png" alt="/wp-content/uploads/2012/05/SieveofEratosthenes.png" /></p>
<p>The PHP implementation of the Eratosthenes sieve isn&#8217;t difficult.</p>
<p><script src="https://gist.github.com/stoimen/640e6c0492d50904f3d6.js?file=eratosthenes_sieve.php"></script></p>
<h2>Application</h2>
<p>As I said prime numbers are widely used in cryptography, so they are always of a greater interest in computer science. In fact every number can be represented by the product of two prime numbers and that fact is used in cryptography as well. That&#8217;s because if we know that number, which is usually very very big, it is still very difficult to find out what are its prime multipliers. Unfortunately the algorithms in this article are very basic and can be handy only if we work with small numbers or if our machines are tremendously powerful. Fortunately in practice there are more complex algorithms for finding prime numbers. Such are the sieves of Euler, Atkin and Sundaram.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/" rel="bookmark" title="Computer Algorithms: How to Determine the Day of the Week">Computer Algorithms: How to Determine the Day of the Week </a></li>
<li><a href="/2011/12/12/computer-algorithms-jump-search/" rel="bookmark" title="Computer Algorithms: Jump Search">Computer Algorithms: Jump Search </a></li>
<li><a href="/2011/11/24/computer-algorithms-sequential-search/" rel="bookmark" title="Computer Algorithms: Sequential Search">Computer Algorithms: Sequential Search </a></li>
<li><a href="/2011/12/26/computer-algorithms-binary-search/" rel="bookmark" title="Computer Algorithms: Binary Search">Computer Algorithms: Binary Search </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/05/08/computer-algorithms-determine-if-a-number-is-prime/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
