<?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>AmirWatad.com &#187; Regular Expressions</title>
	<atom:link href="http://www.amirwatad.com/blog/archives/category/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amirwatad.com/blog</link>
	<description>Remember the name ;)</description>
	<lastBuildDate>Wed, 22 Sep 2010 22:04:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Regular Expression To Check For Prime Numbers</title>
		<link>http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/</link>
		<comments>http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 23:09:15 +0000</pubDate>
		<dc:creator>Amir Watad</dc:creator>
				<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[Tricks]]></category>

		<guid isPermaLink="false">http://www.amirwatad.com/blog/?p=592</guid>
		<description><![CDATA[The code is in python, but the concept is the same for any language with regular expression support (perl, sed, awk, vim, etc..) 123import re def is_prime&#40;n&#41;: &#160; &#160; &#160; &#160; return re.match&#40;r'^1?$&#124;^(11+?)\1+$', &#34;1&#34; * n&#41; == None; That&#8217;s it using a regular expression we can check if a number is prime or not. Here [...]<p>---------
Note:
Some RSS readers might display this post with incorrect format.
View original post at <a href="http://www.amirwatad.com/blog">AmirWatad.com </a><br/><br/><a href="http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/">Regular Expression To Check For Prime Numbers</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The code is in python, but the concept is the same for any language with regular expression support (perl, sed, awk, vim, etc..)</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span><br />
<span style="color: #ff7700;font-weight:bold;">def</span> is_prime<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^1?$|^(11+?)<span style="color: #000099; font-weight: bold;">\1</span>+$'</span>, <span style="color: #483d8b;">&quot;1&quot;</span> <span style="color: #66cc66;">*</span> n<span style="color: black;">&#41;</span> == <span style="color: #008000;">None</span><span style="color: #66cc66;">;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s it <img src='http://www.amirwatad.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Regular Expression To Check For Prime Numbers" />   using a regular expression we can check if a number is prime or not.</p>
<p>Here is a short explanation of why it works (though it will ruin the magic part):<br />
<span id="more-592"></span></p>
<p>The trick is first to &#8220;flatten&#8221; the number. i.e., for 7, we&#8217;ll pass (&#8220;1&#8243; * 7) to the regexp, that is: &#8220;1111111&#8243;.<br />
The first part of the regexp &#8220;^1?$&#8221; matches a string with an optional &#8220;1&#8243;, that is, it matches the empty string, and the string &#8220;1&#8243;, which are the &#8220;flat&#8221; representations of 0 and 1 respectively.</p>
<p>The second part is more tricky:<br />
It tries to match a string of &#8220;start of line&#8221;, then two 1&#8242;s or more ^(11+?), then it tries to find one or more times the previous match (\1+). then end of line $.<br />
If you imaging &#8220;sticks&#8221; instead of the 1&#8242;s, then what we did is: represent 7 (for example) with 7 sticks, then try to divide them into two or more groups of two, or two or more groups of three, etc.. in general: try to divide the sticks into two or more groups of &#8220;two or more&#8221;. Which is exactly the definition of a non-prime number (a.k.a composite number).<br />
So, if the regexp succeeds to match the pattern, the number is not prime. If it fails, the number is prime.</p>
<p>So easy, so elegant <img src='http://www.amirwatad.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' title="Regular Expression To Check For Prime Numbers" /> </p>
<p>Note about the ^(11+?):  This is almost the same as ^(11+) but, with one difference: using (+?) instead of (+) means &#8220;match minimally&#8221;, as opposed to &#8220;match greedily&#8221;.<br />
For example, consider the string &#8220;aaaa&#8221; and the regexp &#8216;(a+)\1+&#8217;  the regexp will match the string, the value of \1 will be &#8220;aa&#8221;, since the a+ pattern matched the longest string which allows the regexp to match the string.<br />
The regexp &#8216;(a+?)\1+&#8217; will also match the string, but the value of \1 will be &#8220;a&#8221;, because this time (a+?) looked for the minimal match which will allow the regexp to match the string.<br />
In out case, the use of (+?) instead of (+) does not affect correctness (that is, we could use (+) and the regexp will still work correctly). The only difference is related to performance, because our case tries small groups first, while the other case tries big groups first.</p>
<p>More information and explanation in the <a href="http://montreal.pm.org/tech/neil_kandalgaonkar.shtml">original article</a>.</p>
<p>---------
Note:
Some RSS readers might display this post with incorrect format.
View original post at <a href="http://www.amirwatad.com/blog">AmirWatad.com </a><br/><br/><a href="http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/">Regular Expression To Check For Prime Numbers</a></p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=Regular+Expression+To+Check+For+Prime+Numbers+http://bit.ly/bKz0Ns" title="Post to Twitter"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-twitter-big3.png" alt="Post to Twitter" style="margin:0;" title="Regular Expression To Check For Prime Numbers" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/&amp;title=Regular+Expression+To+Check+For+Prime+Numbers" title="Post to Delicious"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-delicious-big3.png" alt="Post to Delicious" style="margin:0 0 0 2px;" title="Regular Expression To Check For Prime Numbers" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/&amp;title=Regular+Expression+To+Check+For+Prime+Numbers" title="Post to Digg"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-digg-big3.png" alt="Post to Digg" style="margin:0 0 0 2px;" title="Regular Expression To Check For Prime Numbers" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/&amp;t=Regular+Expression+To+Check+For+Prime+Numbers" title="Post to Facebook"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-facebook-big3.png" alt="Post to Facebook" style="margin:0 0 0 2px;" title="Regular Expression To Check For Prime Numbers" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/&amp;title=Regular+Expression+To+Check+For+Prime+Numbers" title="Post to Reddit"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-reddit-big3.png" alt="Post to Reddit" style="margin:0 0 0 2px;" title="Regular Expression To Check For Prime Numbers" /></a> <a target="_blank" class="tt" href="http://stumbleupon.com/submit?url=http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/&amp;title=Regular+Expression+To+Check+For+Prime+Numbers" title="Post to StumbleUpon"><img class="nothumb" src="http://www.amirwatad.com/blog/wp-content/plugins/tweet-this/icons/tt-su-big3.png" alt="Post to StumbleUpon" style="margin:0 0 0 2px;" title="Regular Expression To Check For Prime Numbers" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.amirwatad.com/blog/archives/2010/07/24/regular-expression-to-check-for-prime-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.662 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-09 23:54:14 -->

