<?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 &#187; Project Euler</title>
	<atom:link href="http://www.parallelcoding.com/tag/project-euler/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 11:55:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<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: [...]]]></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 [...]]]></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>
		<item>
		<title>Project Euler #2</title>
		<link>http://www.parallelcoding.com/2009/05/28/project-euler-2/</link>
		<comments>http://www.parallelcoding.com/2009/05/28/project-euler-2/#comments</comments>
		<pubDate>Thu, 28 May 2009 20:07:09 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=414</guid>
		<description><![CDATA[Problem #2 in the Project Euler series is: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230; Find the sum of all the even-valued terms in [...]]]></description>
			<content:encoded><![CDATA[<p>Problem #2 in the Project Euler series is:</p>
<blockquote><p><em>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</em></p>
<p><em>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</em></p>
<p><em>Find the sum of all the even-valued terms in the sequence which do not exceed four million.</em></p></blockquote>
<p>My solution:</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Problem2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> term1 <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> term2 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> term3 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> max <span style="color: #008000;">=</span> <span style="color: #FF0000;">4000000</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> sum <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>term1 <span style="color: #008000;">&lt;</span> max <span style="color: #008000;">&amp;&amp;</span> term2 <span style="color: #008000;">&lt;</span> max <span style="color: #008000;">&amp;&amp;</span> term3 <span style="color: #008000;">&lt;</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        term1 <span style="color: #008000;">=</span> term2 <span style="color: #008000;">+</span> term3<span style="color: #008000;">;</span>
        term2 <span style="color: #008000;">=</span> term1 <span style="color: #008000;">+</span> term3<span style="color: #008000;">;</span>
        term3 <span style="color: #008000;">=</span> term2 <span style="color: #008000;">+</span> term1<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term1 <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>
            sum <span style="color: #008000;">+=</span> term1<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term2 <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>
            sum <span style="color: #008000;">+=</span> term2<span style="color: #008000;">;</span>
         <span style="color: #000000;">&#125;</span>
         <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term3 <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>
            sum <span style="color: #008000;">+=</span> term3<span style="color: #008000;">;</span>
         <span style="color: #000000;">&#125;</span>
&nbsp;
    <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/05/28/project-euler-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #1</title>
		<link>http://www.parallelcoding.com/2009/03/25/project-euler-1/</link>
		<comments>http://www.parallelcoding.com/2009/03/25/project-euler-1/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 13:20:40 +0000</pubDate>
		<dc:creator>manatarms</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=288</guid>
		<description><![CDATA[Problem #1 in the Project Euler series is: Add all the natural numbers below one thousand that are multiples of 3 or 5. A few of the more interesting/elegant solutions I&#8217;ve seen are listed below. C# 1 2 List&#60;int&#62; Numbers = &#40;Enumerable.Range&#40;0, upperLimit&#41;&#41;.ToList&#40;&#41;; int Result = &#40;from n in Numbers where n % 3 == [...]]]></description>
			<content:encoded><![CDATA[<p>Problem #1 in the Project Euler series is:</p>
<blockquote><p>Add all the natural numbers below one thousand that are multiples of 3 or 5.</p></blockquote>
<p>A few of the more interesting/elegant solutions I&#8217;ve seen are listed below.</p>
<p>C#</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> Numbers <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, upperLimit<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> Result <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from n <span style="color: #0600FF;">in</span> Numbers where n <span style="color: #008000;">%</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">||</span> n <span style="color: #008000;">%</span> <span style="color: #FF0000;">5</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> select n<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></pre></td></tr></table></div>

<p>F#</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #066; font-weight: bold;">#light</span>
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> sum_mul xs <span style="color: #000080;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> xs <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #000080;">|</span> <span style="color: #000080;">&#91;</span><span style="color: #000080;">&#93;</span>    <span style="color: #000080;">-&gt;</span> <span style="color: #c6c;">0</span>
        <span style="color: #000080;">|</span> y<span style="color: #000080;">::</span>ys <span style="color: #06c; font-weight: bold;">when</span> y <span style="color: #000080;">%</span> <span style="color: #c6c;">3</span> <span style="color: #000080;">=</span> <span style="color: #c6c;">0</span> <span style="color: #000080;">||</span> y <span style="color: #000080;">%</span> <span style="color: #c6c;">5</span> <span style="color: #000080;">=</span> <span style="color: #c6c;">0</span> <span style="color: #000080;">-&gt;</span> y <span style="color: #000080;">+</span> sum_mul ys
        <span style="color: #000080;">|</span> y<span style="color: #000080;">::</span>ys <span style="color: #000080;">-&gt;</span> sum_mul ys
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> sum <span style="color: #000080;">=</span> sum_mul <span style="color: #000080;">&#91;</span><span style="color: #c6c;">1</span> <span style="color: #000080;">..</span> <span style="color: #c6c;">999</span><span style="color: #000080;">&#93;</span>
print_any sum</pre></td></tr></table></div>

<p>Ruby (from <a title="CSLife" href="http://cslife.wordpress.com/2009/02/09/some-project-euler-solutions-in-ruby-one-liners/">csLife</a>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&#40;</span>1...1000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">3</span> == <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">or</span> n <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">5</span> == <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>sum, n<span style="color:#006600; font-weight:bold;">|</span> sum <span style="color:#006600; font-weight:bold;">+</span> n <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>Haskell</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">sum</span> <span style="color: green;">&#91;</span>n <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>1000<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> n '<span style="font-weight: bold;">mod</span>' <span style="color: red;">5</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">||</span> n '<span style="font-weight: bold;">mod</span>' <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#93;</span></pre></td></tr></table></div>

<p>Erlang</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">-</span><span style="color: #06c; font-weight: bold;">module</span><span style="color: green;">&#40;</span>euler<span style="color: #339933; font-weight: bold;">_</span>1<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">.</span>
<span style="color: #339933; font-weight: bold;">-</span>export<span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>start<span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span>solve<span style="color: #339933; font-weight: bold;">_</span>euler<span style="color: #339933; font-weight: bold;">_</span>1<span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">1</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">.</span>
&nbsp;
start<span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> io:format<span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;~w~n&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#91;</span>solve<span style="color: #339933; font-weight: bold;">_</span>euler<span style="color: #339933; font-weight: bold;">_</span>1<span style="color: green;">&#40;</span><span style="color: red;">1000</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">.</span>
solve<span style="color: #339933; font-weight: bold;">_</span>euler<span style="color: #339933; font-weight: bold;">_</span>1<span style="color: green;">&#40;</span>N<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> lists:<span style="font-weight: bold;">sum</span><span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>X <span style="color: #339933; font-weight: bold;">||</span> X <span style="color: #339933; font-weight: bold;">&lt;-</span> lists:<span style="font-weight: bold;">seq</span><span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>N<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span>X <span style="font-weight: bold;">rem</span> <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="font-weight: bold;">or</span> <span style="color: green;">&#40;</span>X <span style="font-weight: bold;">rem</span> <span style="color: red;">5</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">.</span></pre></td></tr></table></div>

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