<?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>Martin Wolf&#039;s weblog &#187; prime factors</title>
	<atom:link href="http://mwolf.net/archive/tag/prime-factors/feed/" rel="self" type="application/rss+xml" />
	<link>http://mwolf.net</link>
	<description>Software development and assorted geekery</description>
	<lastBuildDate>Sun, 08 Aug 2010 18:15:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Golfing with prime factors</title>
		<link>http://mwolf.net/archive/golfing-with-prime-factors/</link>
		<comments>http://mwolf.net/archive/golfing-with-prime-factors/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 13:57:53 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[me]]></category>
		<category><![CDATA[code golf]]></category>
		<category><![CDATA[factoring]]></category>
		<category><![CDATA[hanoi]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl golf]]></category>
		<category><![CDATA[prime factors]]></category>
		<category><![CDATA[prime numbers]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[towers of hanoi]]></category>

		<guid isPermaLink="false">http://mwolf.net/?p=55</guid>
		<description><![CDATA[Dirk-Jan reminded me of the Perl Golf and Code Golf contests, both of which have the aim of solving a simple programming task in as few characters of source code as possible. See his post for a stunning example.
One of the open challenges is to work out the prime factors of a given number. To [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.djcbsoftware.nl/ChangeLog/2009/05/perl-golf.html">Dirk-Jan</a> reminded me of the <a href="http://perlgolf.sourceforge.net/">Perl Golf</a> and <a href="http://codegolf.com/">Code Golf</a> contests, both of which have the aim of solving a simple programming task in as few characters of source code as possible. See his post for a stunning example.</p>
<p>One of the <a href="http://codegolf.com/competition/browse">open challenges</a> is to <a href="http://codegolf.com/prime-factors">work out the prime factors</a> of a given number. To make things a little more difficult, the output must be printed in a specific format:</p>
<blockquote><p><span style="font-family: Courier New;"> 7000: 2^3 5^3 7<br />
123456789: 3^2 3607 3803</span></p></blockquote>
<p><span id="more-55"></span>Here is one of my attempts at a Ruby implementation, which is 97 bytes after removing the final trailing newline:</p>
<blockquote><p><span style="font-family: Courier New;"> print&#8221;#{n=gets.to_i}: &#8221;<br />
(2..n).each{|x|i=0;n,i=n/x,i+1while n%x&lt;1<br />
print x,i&lt;2?&#8217; &#8216;:&#8221;^#{i} &#8220;if i&gt;0}</span></p></blockquote>
<p>Then we switched to Perl, and together Dirk-Jan and I managed to come up with a version which is just 82 bytes after removing all the newlines (just pipe it through &#8220;<span style="font-family: Courier New;">perl -pe chomp</span>&#8220;).</p>
<blockquote><p><span style="font-family: Courier New;"> print$n=pop,&#8217;:';for(2..$n){<br />
$i=0;$n/=$_,$i++until$n%$_;<br />
print&#8221; $_&#8221;.&#8221;^$i&#8221;x($i&gt;1)if$i}</span></p></blockquote>
<p>(Be aware that the Ruby version takes its input from <span style="font-family: Courier New;">stdin</span>, while the Perl program looks at its first command-line argument.)</p>
<p>That&#8217;s pretty compact, right? Algorithmically, the code is actually fairly straightforward; probably the most evil trick we use is to employ the &#8216;<span style="font-family: Courier New;">x</span>&#8216; operator, which does string concatenation a given number of times, in combination with the fact that any Boolean expression can be treated as a numeric value of either 0 or 1, as in C (but not in Ruby).</p>
<p>Unfortunately, both of the versions given above are horrendously slow. They have a running time of <a href="http://en.wikipedia.org/wiki/Big_O_notation">O(<em>n</em>)</a>, while any self-respecting factorization algorithm should be at most proportional with the largest prime factor of <em>n</em>. It took my Pentium-4 system almost 45 minutes to calculate the factors of 2,000,000,000, which a reasonably intelligent high school student could probably have done in half a minute or so. Here is a version which can do it in a fraction of a second, but at the cost of being a whole 10 bytes larger:</p>
<blockquote><p><span style="font-family: Courier New;"> print$n=pop,&#8217;:';<br />
for($x=2;$n&gt;1;$x++){<br />
$i=0;$n/=$x,$i++until$n%$x;<br />
print&#8221; $x&#8221;.&#8221;^$i&#8221;x($i&gt;1)if$i}</span></p></blockquote>
<p>So, how are we doing in the contest? Well, we&#8217;re not even competing. The <a href="http://codegolf.com/leaderboard/competition/prime-factors/">leader</a> is currently at an astonishing 76 bytes! I have no idea how they do that. The winning programs are all in Perl, which tends to do very well in this type of contest despite the fact that all variable names are at least two characters. The best Ruby program is 82 bytes, with Python coming in at 100 and PHP at 122.</p>
<p>Another <a href="http://codegolf.com/tower-of-hanoi">task</a> in Code Golf is to write a program which can solve the famous <a href="http://en.wikipedia.org/wiki/Towers_of_hanoi">Towers of Hanoi</a> puzzle, given a random starting position with up to nine disks. The input consists of three lines of text, each giving the sequence of disks for one of the three pegs. For example:</p>
<blockquote><p><span style="font-family: Courier New;"> 975<br />
864<br />
321</span></p></blockquote>
<p>The goal is to print the series of moves needed to get all disks together on peg C, the third one, following the usual rule that at no time may a larger disk be on top of (to the right of) a smaller one. Here is a very simple example run with only three disks:</p>
<blockquote><p><span style="font-family: Courier New;"> $ cat simple.txt<br />
31</span></p>
<p>2</p>
<p><span style="font-family: Courier New;"> $ ruby hanoi.rb simple.txt<br />
2 to B<br />
1 to B<br />
3 to C<br />
1 to A<br />
2 to C<br />
1 to C</span></p></blockquote>
<p><span style="font-family: sans-serif;">We haven&#8217;t spent as much time yet on this one as on the prime factorization problem. Here is my best effort so far:</span></p>
<blockquote><p><span style="font-family: Courier New;"> T,D=[],[:A,:B,:C]<br />
D.each{|t|gets.chomp.each_byte{|x|T[x-48]=t}}<br />
def m s,t<br />
if s&gt;0<br />
m s-1,(D-[t,T[s]])[0]<br />
puts&#8221;#{s} to #{t}&#8221;<br />
m s-1,T[s]=t<br />
end<br />
end<br />
m T.size-1,:C</span></p></blockquote>
<p><span style="font-family: sans-serif;">This is an embarassing 157 bytes. In the actual <a href="http://codegolf.com/leaderboard/competition/tower-of-hanoi/">contest participants</a>, Ruby is leading the pack this time, with 104 bytes, while the best Perl entry so far is a whole six bytes larger. Go Ruby!</span></p>
<p><span style="font-family: sans-serif;">The Code Golf contest is open to participants using <a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29">Ruby</a>, <a href="http://en.wikipedia.org/wiki/Perl">Perl</a>, <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> and <a href="http://en.wikipedia.org/wiki/PHP">PHP</a>. Nonetheless, <a href="http://blog.leenarts.net/">Jeroen</a>, <a href="http://blog.hendricksen.eu">Jeroen</a> and <a href="http://blogs.infosupport.com/blogs/raimondb/">Raimond</a>, I look forward to you trying to beat the above programs in Java or C#..Â  <img src='http://mwolf.net/wordpress/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://mwolf.net/archive/golfing-with-prime-factors/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
