<?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>On technology and development &#187; Iterators</title>
	<atom:link href="http://blog.codewrench.net/tag/iterators/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codewrench.net</link>
	<description>My thoughs on stuff</description>
	<lastBuildDate>Wed, 18 Jan 2012 07:49:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Project Euler and Linq</title>
		<link>http://blog.codewrench.net/2009/03/12/project-euler-and-linq/</link>
		<comments>http://blog.codewrench.net/2009/03/12/project-euler-and-linq/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 19:22:44 +0000</pubDate>
		<dc:creator>Pål</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Euler]]></category>
		<category><![CDATA[Iterators]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://blog.paks.no/?p=69</guid>
		<description><![CDATA[First I&#8217;d like to thank Stefaan for introducing me to Project Euler. Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant &#8230; <a href="http://blog.codewrench.net/2009/03/12/project-euler-and-linq/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>First I&#8217;d like to thank <a href="http://stefaan.wordpress.com/">Stefaan</a> for introducing me to <a href="http://projecteuler.net/">Project Euler</a>. Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.</p>
<p>Most of the problems you can brute force in resonable time, but the challenge is more of a personal level in trying to solve them elegant or in as few lines as possible.</p>
<p>I quickly found that the magic of yield return and Linq was well suited to solve several of the problems I&#8217;ve encountered yet. So I decided to write a blog post about them and share my new-found knowlege of Linq.</p>
<p><strong>Problem 1: <span style="text-decoration: none;">Add all the natural numbers below one thousand that are multiples of 3 or 5</span></strong></p>
<p>The Enumerable pattern is quite extraordinary when used in Linq like this. So I decided to use the same pattern on other problems as well.</p>
<pre>[code lang="csharp"]
return Enumerable.Range(1, 999)
.Where(x => x % 5 == 0 || x % 3 == 0)
.Sum();
[/code]</pre>
<p><strong>Problem 2: Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million</strong></p>
<p>First I made a sequence generator for Fibonacci numbers:</p>
<pre>[code lang="csharp"]
internal static IEnumerable<long>FibonacciSequence()
{
  long last1 = 1;
  yield return 1;

  long last2 = 2;
  yield return 2;

  while (true)
  {
    long next = last1 + last2;
    last1 = last2;
    last2 = next;
    yield return next;
  }
}
[/code]</pre>
<p><strong>Problem 3: What is the largest prime factor of the number 600851475143 ?</strong></p>
<p>Again I start with creating a sequence generator. This one returns the sequence of factors for any number.</p>
<pre>[code lang="csharp"]
internal static IEnumerable<long> GetFactors(long x)
{
    for (long factor = 1; factor * factor <= x; factor++)
    {
        if (x % factor == 0)
        {
            yield return factor;
            if (factor * factor != x)
                yield return x / factor;
        }
    }
}
[/code]</pre>
<p>We also need a small helper function to check if a number if a prime number:</p>
<pre>[code lang="csharp"]
private static bool IsPrime(long num)
{
  long limit = (long)Math.Sqrt(num);

  for (long d = 2; d <= limit; d++)
  {
      if (num % d == 0)
          return false;
  }

  return true;
}
[/code]</pre>
<p>Finally the actual Linq query to find the largest prime factor of the number 600851475143 :</p>
<pre>[code lang="csharp"]
var primeFactors = from factor in GetFactors(600851475143)
                          where IsPrime(factor)
                          select factor;

return primeFactors.Max();
[/code]</pre>
<p>I also have use the same pattern on several of the other problems like nr 10: <strong>Find the sum of all prime numbers below 2 000 000:</strong></p>
<pre>[code lang="csharp"]
return PrimeNumbers()
         .TakeWhile(x => x < 2000000)
         .Sum();
[/code]</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.codewrench.net/2009/03/12/project-euler-and-linq/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

