<?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>Parallelcoding.com</title>
	<atom:link href="http://www.parallelcoding.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description></description>
	<lastBuildDate>Thu, 04 Mar 2010 16:57:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>March Madness Multicore Style</title>
		<link>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/</link>
		<comments>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 16:57:34 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[HPC]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=536</guid>
		<description><![CDATA[An interesting article has been posted over at Linux Magazine. The article interests me mainly because it gets to the heart of an issue that I myself ponder over: As hardware overtakes software, how will software catch up? This is a very important question, especially in the HPC world as there is no single language [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting article has been posted over at Linux Magazine. The article interests me mainly because it gets to the heart of an issue that I myself ponder over: As hardware overtakes software, how will software catch up? This is a very important question, especially in the HPC world as there is no single language that elegantly translates across multiple architectures.<br />
<br/><br />
Read the rest of the article <a href="http://www.linux-mag.com/id/7722">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Central Force Optimization</title>
		<link>http://www.parallelcoding.com/2010/03/03/central-force-optimization/</link>
		<comments>http://www.parallelcoding.com/2010/03/03/central-force-optimization/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 14:32:52 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Central Force Optimization]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=528</guid>
		<description><![CDATA[Central Force Optimization (CFO) is a new and interesting optimization method that has been developed by Richard A. Formato. The most interesting aspect of CFO is that it is fully deterministic unlike PSO, GA, AIS, or any of the other PIS based techniques.
The papers that he has produced on the topic can be found at:

Are [...]]]></description>
			<content:encoded><![CDATA[<p>Central Force Optimization (CFO) is a new and interesting optimization method that has been developed by Richard A. Formato. The most interesting aspect of CFO is that it is fully deterministic unlike PSO, GA, AIS, or any of the other PIS based techniques.</p>
<p>The papers that he has produced on the topic can be found at:</p>
<ol>
<li><a href="http://arxiv.org/abs/0912.1394">Are Near Earth Objects the Key to Optimization Theory?</a></li>
<li><a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;ved=0CAYQFjAA&amp;url=http%3A%2F%2Fsci2s.ugr.es%2Feamhco%2Fpdfs%2Fformato07PIER.pdf&amp;ei=sHGOS6qhNJHk8QakxtGHDQ&amp;usg=AFQjCNHnOCCtHFooTSLtyBtJrQcMCAaCtg&amp;sig2=s9I4f7tH3qDRoq8EB_9a3A">Central Force Optimization: A New Metaheuristic with Applications in Applied Electromagnetics</a></li>
<li><a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;ved=0CBMQFjAC&amp;url=http%3A%2F%2Fwww.springerlink.com%2Findex%2Ft357063336g229g0.pdf&amp;ei=sHGOS6qhNJHk8QakxtGHDQ&amp;usg=AFQjCNFLaff2aAbN22RraGM8VIeUITQG7Q&amp;sig2=0H95M1qhSYTh6cjBFVs8Aw">Central Force Optimization: A New Nature Inspired Computational Framework for Multidimensional Search and Optimization</a></li>
<li><a href="http://www.springerlink.com/content/541400g38xt5451w/">Central Force Optimization: A New Deterministic Gradient -like Optimization  Metaheuristic</a></li>
<li><a href="http://arxiv.org/abs/1002.2798">Comparative Results: Group Search Optimizer and Central Force Optimization</a></li>
<li><a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=6&amp;ved=0CCUQFjAF&amp;url=http%3A%2F%2Farxiv.org%2Fpdf%2F1001.0317&amp;ei=s3COS4HlDNLL8Qai_cDpDA&amp;usg=AFQjCNEe64O5QI6FCBL1gZQKjyUYGsPTUw&amp;sig2=G7355osxdccvC74ovG9wHw">Pseudorandomness in Central Force Optimization</a></li>
<li><a href="http://arxiv.org/abs/1003.0221">Central Force Optimization Applied to the PBM Suite of Antenna Benchmarks</a></li>
</ol>
<p>And, for all of those interested, <a href="http://www.parallelcoding.com/wp-content/uploads/2010/03/CFO.cpp">click here</a> to download my C++ implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/03/03/central-force-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Parallel Power Law</title>
		<link>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/</link>
		<comments>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:14:14 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=526</guid>
		<description><![CDATA[This article is very interesting. A very hot topic in parallel computing these days is power consumption and this is the first that I&#8217;ve heard of the parallel power law. I&#8217;m intrigued.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://perilsofparallel.blogspot.com/2010/02/parallel-power-law.html">This</a> article is very interesting. A very hot topic in parallel computing these days is power consumption and this is the first that I&#8217;ve heard of the parallel power law. I&#8217;m intrigued.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Particle Swarm Optimization</title>
		<link>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/</link>
		<comments>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 15:20:54 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Heuristics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=518</guid>
		<description><![CDATA[In pursuing some of my research as of late, I needed to use Particle Swarm Optimization. All I wanted was some simple code for both Real and Binary PSO &#8211; but I couldn&#8217;t find anything I like! So, I&#8217;m providing a file containing both along with the RNG that I used. Please be aware that [...]]]></description>
			<content:encoded><![CDATA[<p>In pursuing some of my research as of late, I needed to use Particle Swarm Optimization. All I wanted was some simple code for both Real and Binary PSO &#8211; but I couldn&#8217;t find anything I like! So, I&#8217;m providing a file containing both along with the RNG that I used. Please be aware that this code has not been refined. It&#8217;s clear, but rather ugly.<br/><br/></p>
<p><a href="http://www.parallelcoding.com/wp-content/uploads/2010/02/main.cpp">Particle Swarm Optimization Code</a></p>
<p><a href="http://www.parallelcoding.com/wp-content/uploads/2010/02/MersenneTwister.h">Mersenne Twister</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So Where Have I Been?</title>
		<link>http://www.parallelcoding.com/2010/01/05/so-where-have-i-been/</link>
		<comments>http://www.parallelcoding.com/2010/01/05/so-where-have-i-been/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 13:01:06 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=503</guid>
		<description><![CDATA[To my very own surprise I have not written a post since November! How crazy is that! The main reason of this has been the utter business of pursuing my PhD. What a crazy semester it was! So what have I accomplished in this first semester? It can be divided into 3 sections: Classes, Projects, [...]]]></description>
			<content:encoded><![CDATA[<p>To my very own surprise I have not written a post since November! How crazy is that! The main reason of this has been the utter business of pursuing my PhD. What a crazy semester it was! So what have I accomplished in this first semester? It can be divided into 3 sections: Classes, Projects, and Papers. <br/><br/></p>
<p>As for classes, I completed two: Artificial Intelligence and The Smart Grid. Both were highly interesting courses and the hard work paid off as well &#8211; I received an A- and an A in the courses! The Artificial Intelligence course was very interesting and covered a vast amount of AI topics. The class was taught by <a href="http://www.eecs.utoledo.edu/~serpen/">Dr. Gursel Serpen</a>. The Smart Grid class was taught by my own advisor, <a href="http://www.eng.utoledo.edu/eecs/people/wang_profile.html">Dr. Lingfeng Wang</a>, and was a very solid and thorough introduction to alternative energy systems and the smart grid.<br/><br/></p>
<p>Projects and Papers have a bit of an overlap. I have posted my AI paper <a href="http://www.parallelcoding.com/cv/">here</a> &#8211; &#8220;<a href="http://www.parallelcoding.com/wp-content/uploads/2010/01/Full-Paper.pdf">Survey of the Applications of Articial Intelligence Techniques to the Sudoku Puzzle</a>&#8221; &#8211; but I will not be posting my other 3 papers until I am aware of their submission status.<br/><br/></p>
<p>Through the entire semester I learned a lot. During the coming semester I will be working with biologically inspired computing techniques, optimization, the smart grid, and high performance computing. I&#8217;ll keep you all updated!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/01/05/so-where-have-i-been/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A New Start</title>
		<link>http://www.parallelcoding.com/2009/08/11/a-new-start/</link>
		<comments>http://www.parallelcoding.com/2009/08/11/a-new-start/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 15:08:27 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[PhD]]></category>
		<category><![CDATA[PHEV]]></category>
		<category><![CDATA[Simluation]]></category>
		<category><![CDATA[Toledo]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=482</guid>
		<description><![CDATA[So it&#8217;s been quite a while since I&#8217;ve last posted. That&#8217;s happened for really one big reason: I got married! So, as of August 1, I am a married man and I am wed to a wonderful woman. Now that all that planning and such is complete and we&#8217;re back from the honeymoon I plan [...]]]></description>
			<content:encoded><![CDATA[<p>So it&#8217;s been quite a while since I&#8217;ve last posted. That&#8217;s happened for really one big reason: I got married! So, as of August 1, I am a married man and I am wed to a wonderful woman. Now that all that planning and such is complete and we&#8217;re back from the honeymoon I plan to post a bit more often.</p>
<p><br/></p>
<p>On a more professional note, at the end of this month I will be starting my PhD studies at the <a href="http://www.utoledo.edu">University of Toledo</a> under<a href="http://www.eng.utoledo.edu/eecs/people/wang_profile.html"> Dr. Lingfeng Wang</a>. My area of research will be modeling and simulation concerning the reliability of large fleets of PHEVs (Plug In Hybrid Electric Vehicles) on the new Smart Grid. I plan to update this site often with my research and progress.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/08/11/a-new-start/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Intel Threading Challenge #1</title>
		<link>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/</link>
		<comments>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:16:57 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[Sorting]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=466</guid>
		<description><![CDATA[While I never participated in the Intel Threading Challenge, I still find the problems really intriguing. Why? Because they are problems designed to test threaded development which is not only cool but is also going to play a large part in the future of computing. I would call all this problem for the most part [...]]]></description>
			<content:encoded><![CDATA[<p>While I never participated in the <a href="http://software.intel.com/en-us/contests/Threading-Challenge-2009/codecontest.php">Intel Threading Challenge</a>, I still find the problems really intriguing. Why? Because they are problems designed to test threaded development which is not only cool but is also going to play a large part in the future of computing. I would call all this problem for the most part concurrent, not parallel. Why? Read <a href="http://www.linux-mag.com/id/7411">this</a> and you&#8217;ll understand completely. Now, on to the challenge!</p>
<p>Problem # 1 states:</p>
<blockquote>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Problem description: Given a set of unsorted items with keys that can be considered as a binary representation of an integer, the bits within the key can be used to sort the set of items. This method of sorting is known as Radix Sort.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Write a program that includes a threaded version of a Radix Sort algorithm that sorts the keys read from an input file, then output the sorted keys to another file. The input and output file names shall be the first and second arguments on the command line of the application execution.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>The first line of the input text file is the total number of keys (N) to be sorted; this is followed by N keys, one per line, in the file.  A key will be a seven-character string made up of printable characters not including the space character (ASCII 0&#215;20). The number of keys within the file is less than 2^31 &#8211; 1.  Sorted output must be stored in a text file, one key per line.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Timing: If you put timing code into your application to time the sorting process and report the elapsed time, this time will be used for scoring.  If no timing code is added, the entire execution time (including time for input and output) will be used for scoring.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em><br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Example Input file:<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />8<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />H@skell<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />surVEYs<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />sysTEMS<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />HASKELL<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Surveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />1234567<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />SURveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />systEMS</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Example Output file:<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />1234567<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />H@skell<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />HASKELL<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />SURveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Surveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />surVEYs<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />sysTEMS<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />systEMS</em></p>
</blockquote>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;">My solution (both serial and parallel):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> RadixSort <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Program <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            StreamReader sr<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> length<span style="color: #008000;">;</span>
            TimeSpan serial, parallel<span style="color: #008000;">;</span>
            sr <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">OpenText</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\Documents and Settings\rgreen\Desktop\Threading\Threading\rsTestK100.dat&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            length <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>length<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> newValues <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>length<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;</span> length<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                values<span style="color: #000000;">&#91;</span>x<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            Stopwatch sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stopwatch<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//</span>
            <span style="color: #008080; font-style: italic;">// Serial</span>
            <span style="color: #008080; font-style: italic;">//</span>
            sw.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            RadixSort<span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CopyTo</span><span style="color: #000000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            serial <span style="color: #008000;">=</span> sw.<span style="color: #0000FF;">Elapsed</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//</span>
            <span style="color: #008080; font-style: italic;">// Parallel</span>
            <span style="color: #008080; font-style: italic;">//</span>
            sw.<span style="color: #0000FF;">Reset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ParallelRadixSort<span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CopyTo</span><span style="color: #000000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            parallel <span style="color: #008000;">=</span> sw.<span style="color: #0000FF;">Elapsed</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Serial Time: &quot;</span> <span style="color: #008000;">+</span> serial<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Parallel Time: &quot;</span> <span style="color: #008000;">+</span> parallel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ParallelRadixSort<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> length <span style="color: #008000;">=</span> array.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            Parallel.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> curRadix<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> curRadix<span style="color: #008000;">;</span>
                array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MergeSort<span style="color: #000000;">&#40;</span>array, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> index<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> RadixSort<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #FF0000;">int</span> length <span style="color: #008000;">=</span> array.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> curRadix <span style="color: #008000;">=</span> array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> curRadix <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> curRadix<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MergeSort<span style="color: #000000;">&#40;</span>array, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> curRadix<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And there you have it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toughest Developer Puzzle Ever</title>
		<link>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/</link>
		<comments>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 18:34:20 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=463</guid>
		<description><![CDATA[Jeff Blankenburg has put together an incredible puzzle over at http://www.toughestdeveloperpuzzleever.com/tdpe/. Absolutely a ton of fun. It took me a few hours to get through it myself!
]]></description>
			<content:encoded><![CDATA[<p>Jeff Blankenburg has put together an incredible puzzle over at <a href="http://www.toughestdeveloperpuzzleever.com/tdpe/">http://www.toughestdeveloperpuzzleever.com/tdpe/</a>. Absolutely a ton of fun. It took me a few hours to get through it myself!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #6</title>
		<link>http://www.parallelcoding.com/2009/07/01/project-euler-6/</link>
		<comments>http://www.parallelcoding.com/2009/07/01/project-euler-6/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 13:52:40 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=451</guid>
		<description><![CDATA[Project Euler problem #6 is
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: with a [...]]]></description>
			<content:encoded><![CDATA[<p>Project Euler problem #6 is</p>
<blockquote><p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p></blockquote>
<p>When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: with a sequential algorithm. The algorithm here is rather trivial: Loop over the natural numbers from 1 to 100. In order to sum the squares I will sum the square of each number. In order to square the sum I will sum the numbers and then square them. The difference is the answer. So, first, I came up with two functions: SumOfSquares and SquareOfSums.</p>
<p><br/></p>
<p>My first attempt at these functions ended up something like this:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> SumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> min<span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;=</span> max<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        result <span style="color: #008000;">+=</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>x, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> SquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> min<span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;=</span> max<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        result <span style="color: #008000;">+=</span> x<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>result, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Both of those functions are very straight forward. My first thought after writing these was, &#8220;Hey, why not use some LINQ?&#8221; So I did. Here is how the functions change:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> LinqSumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>           
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> LinqSquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> d<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>    
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Wow! Talk about incredible, shrinking functions! I love finding a way to make code more succinct, readable, and elegant and these changes seem to have hit the nail on the head! Anyways, that is basically all the pieces for the sequential algorithm. All you have to do is call each of those functions and take the difference. Simple, huh? But the real question is how can we parallelize these bad boys? I have a few thoughts.</p>
<ol>
<li>Call each function in parallel. In other words let the sumOfSquares and SquareOfSum function run at the same time in different threads.</li>
<li>Parallelize each function invidvidually. In other words  leverage the loops inside of each function in order to parallelize them.</li>
</ol>
<p>So let&#8217;s take a look at each method using the Parallel Extensions for .NET. The first method is calling each function in parallel. My first thought here was to use Parallel.Invoke in order to call each function at the same time. A little further research quickly revealed that Parallel.Invoke cannot return any values. My initial response to that: &#8220;Well that sucks.&#8221; Luckily there&#8217;s another class in the Parallel library called Futures. What&#8217;s a future? From <a href="http://www.devx.com/dotnet/Article/39204/1763/page/5">DevX</a> &#8211;<br />
<blockquote>&#8220;In TPL terms, a Future is basically a task that returns a value. It&#8217;s like a deferred function. You start it running and then use its value later. If the Future hasn&#8217;t finished calculating its value by the time you need it, it makes you wait while it finishes.&#8221; </p></blockquote>
<p>Sounds good to me. So how do we use futures? Like this:
</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">long</span> Problem6Futures<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    Future<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">double</span><span style="color: #008000;">&gt;</span> fSumOfSquares <span style="color: #008000;">=</span> Future.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SumOfSquares<span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Future<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">double</span><span style="color: #008000;">&gt;</span> fSquareOfSums <span style="color: #008000;">=</span> Future.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SquareOfSums<span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> fSquareOfSums.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">-</span> fSumOfSquares.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Easy, huh? All you have to remember is that Futures are deferred functions that return values. The result of  Future operation gets stored in an object of type Future<Type> and the value is stored in Object.Value. So how about the second method of parallelizing this algorithm? Well, it&#8217;s even easier because of PLINQ &#8211; or Parallel LINQ. Let&#8217;s see what it looks like.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> ParallelSumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> ParallelSquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Hah! Even easier! All that I did was add .AsParrallel() to our data. That tells LINQ to do the processing in parallel!</p>
<p>
So, there&#8217;s Project Euler Problem #6 for you. Was it a hard problem? Not really. Are you going to see major performance results through the parallelization of this algorithm? No. But you gotta&#8217; start somewhere when you&#8217;re learning how to parallelize algorithms using a new library. Today I used Futures and PLINQ and that sound&#8217;s like a pretty solid start to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/01/project-euler-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #3</title>
		<link>http://www.parallelcoding.com/2009/06/30/project-euler-3/</link>
		<comments>http://www.parallelcoding.com/2009/06/30/project-euler-3/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 12:09:06 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=418</guid>
		<description><![CDATA[Project Euler #3 is :
Find the largest prime factor of a composite number.
My first attempt at this whipped up some typical code that simply brute forced my way to the solution. My code looked like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
static void Problem3&#40;&#41; &#123;
    long n = 600851475143;
    int factor = 2;
   [...]]]></description>
			<content:encoded><![CDATA[<p>Project Euler #3 is :</p>
<blockquote><p>Find the largest prime factor of a composite number.</p></blockquote>
<p>My first attempt at this whipped up some typical code that simply brute forced my way to the solution. My code looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Problem3<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">600851475143</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
         lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #FF0000;">double</span> maxFactor <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;&amp;</span> factor <span style="color: #008000;">&lt;=</span> maxFactor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
              n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> factor<span style="color: #008000;">;</span>
              lastFactor <span style="color: #008000;">=</span> factor<span style="color: #008000;">;</span>
              <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                  n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> factor<span style="color: #008000;">;</span>
              <span style="color: #000000;">&#125;</span>
              maxFactor <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #000000;">&#125;</span>
          factor <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>lastFactor.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
          Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>n.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
Not very elegant, but it works. So I set out to find something a bit prettier and I came across I LINQ solution <a href="http://srtsolutions.com/blogs/billwagner/archive/2008/03/28/notes-on-euler-problem-3.aspx">here</a>. All I did was tack on the .AsParallel() in order to give it a little speed boost. The code looks like:<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> LinqProblem3<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">long</span> largeNumber <span style="color: #008000;">=</span> <span style="color: #FF0000;">600851475143</span><span style="color: #008000;">;</span>
    var allPrimeFactors <span style="color: #008000;">=</span> from p <span style="color: #0600FF;">in</span> Primes.<span style="color: #0000FF;">PrimeFactors</span><span style="color: #000000;">&#40;</span>largeNumber<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                                 orderby p descending
                                 select p<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var f <span style="color: #0600FF;">in</span> allPrimeFactors<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Primes <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Find all prime factors.</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> PrimeFactors<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> number<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Start by removing the lowest prime (2)</span>
        <span style="color: #0600FF;">return</span> MorePrimeFactors<span style="color: #000000;">&#40;</span>number, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// This recursive method finds all prime factors.</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> MorePrimeFactors<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> number, <span style="color: #FF0000;">int</span> factor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Find the next prime factor</span>
	<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>number <span style="color: #008000;">%</span> factor <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
	    factor<span style="color: #008000;">++;</span>
	<span style="color: #008080; font-style: italic;">// Return it.</span>
	yield <span style="color: #0600FF;">return</span> factor<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// look again...</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>number <span style="color: #008000;">&gt;</span> factor<span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">// recursively look for this factor again, using Num/factor</span>
		<span style="color: #008080; font-style: italic;">// as the new big number</span>
		<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> factors <span style="color: #0600FF;">in</span> MorePrimeFactors<span style="color: #000000;">&#40;</span>number <span style="color: #008000;">/</span> factor, factor<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			yield <span style="color: #0600FF;">return</span> factors<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/30/project-euler-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
