<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.NET at 9.400 ft above sea level : Project Euler</title><link>http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx</link><description>Tags: Project Euler</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>A cool way to find out whether a number is palindromic</title><link>http://weblogs.asp.net/esanchez/archive/2008/05/06/a-cool-way-to-find-out-whether-a-number-is-palindromic.aspx</link><pubDate>Tue, 06 May 2008 06:40:08 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6161587</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6161587</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/05/06/a-cool-way-to-find-out-whether-a-number-is-palindromic.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx" target="_blank"&gt;this blog entry I proposed a solution&lt;/a&gt; to &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4" target="_blank"&gt;Problem 4 at Project Euler&lt;/a&gt;, a crucial element of the problem is to find out whether a number is a palindrome (909 is, 809 isn't), a bit out of laziness and a bit in order to reuse existing methods, I decided to verify the palindrome by converting the number into a char array and then comparing this array with its mirror version, it works but it's not really that mathematical... &lt;a href="http://diditwith.net/2008/05/02/YAPESProblemFour.aspx" target="_blank"&gt;Dustin Campbell proposed a solution&lt;/a&gt; kind of similar to mine (alas, more elegant and, above that, in F#) and using the same trick of converting the number to chars, as he didn't like this part of the solution, in &lt;a href="http://diditwith.net/2008/05/06/YAPESProblemFourAlternateSolution.aspx" target="_blank"&gt;this new blog entry he proposes the detection of a palindrome by mirroring the number one digit at a time&lt;/a&gt;. A translation of his F# code to C# 3.0 could be:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; TailReverseNumber = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TailReverseNumber = (n, res) =&amp;gt; n == 0 ? res : TailReverseNumber(n / 10, 10 * res + n % 10);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPalindrome = n =&amp;gt; n == TailReverseNumber(n, 0);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;TailReverseNumber takes a number n and "mirrors" it, one digit at a time, for example: TailReverseNumber(237, 0) -&amp;gt; TailReverseNumber(23, 7) -&amp;gt; TailReverseNumber(2, 73) -&amp;gt; TailReverseNumber(0, 732), the big trick is that res works as an accumulator that is multiplied by 10, therefore moving its value to the left, and putting in the "hole" that appears at the right the least significant digit of n. As it names implies, TailReverseNumber() uses &lt;a href="http://en.wikipedia.org/wiki/Tail_recursion" target="_blank"&gt;tail recursion&lt;/a&gt;, so that no extra call stack space is used to save any intermediate results, which makes the process pretty efficient. Actually, in my PC it's four times faster than the initial solution. More efficient and more elegant, a smart guy Dustin.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6161587" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item><item><title>New version of F# just released</title><link>http://weblogs.asp.net/esanchez/archive/2008/05/03/new-version-of-f-just-released.aspx</link><pubDate>Sat, 03 May 2008 06:26:07 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6152555</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6152555</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/05/03/new-version-of-f-just-released.aspx#comments</comments><description>&lt;p&gt;In its way from research language to commercial language, &lt;a href="http://blogs.msdn.com/dsyme/archive/2008/05/02/f-1-9-4-now-available-making-f-simpler-and-more-consistent.aspx" target="_blank"&gt;Don Syme just announced&lt;/a&gt; that, silently, on May the 1st &lt;a href="http://research.microsoft.com/research/downloads/Details/7ac148a7-149b-4056-aa06-1e6754efd36f/Details.aspx" target="_blank"&gt;version 1.9.4.15 of F# was released&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/NewversionofFjustreleased_1422/FSharp19415_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="259" alt="F# 1.9.4.15" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/NewversionofFjustreleased_1422/FSharp19415_thumb.png" width="724" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This new version incorporates a number of specific enhancements (F# is now basically in stabilization mode, so we really shouldn't expect significative changes to the language). By the way, the example in the picture shows my idea for solving &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2" target="_blank"&gt;Problema 2 of Project&amp;#160; Euler&lt;/a&gt;: find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million. For an explanation of how it works, I suggest you this &lt;a href="http://diditwith.net/blog/Default.aspx#a8fc9ed17-94a5-440d-95be-c831f1e1a1de" target="_blank"&gt;Dustin Campbell blog&lt;/a&gt;, which proposes a solution similar to mine with a couple of elegant touches and, above all, with a detailed and engaging explanation.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6152555" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item><item><title>Which is the ten thousand first prime?</title><link>http://weblogs.asp.net/esanchez/archive/2008/05/02/which-is-the-ten-thousand-first-prime.aspx</link><pubDate>Fri, 02 May 2008 05:09:56 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6149321</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6149321</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/05/02/which-is-the-ten-thousand-first-prime.aspx#comments</comments><description>&lt;p&gt;Prime numbers have a good deal of practical applications (for example in cryptography) but let's face it, even if they would have none, they would still be the &lt;a title="Prime Obsession" href="http://olimu.com/Riemann/Riemann.htm" target="_blank"&gt;favorite toy of mathematicians&lt;/a&gt;. In &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7" target="_blank"&gt;Problem 7 of Project Euler&lt;/a&gt;, we are asked to find the 10001st element of the famous list, my approach was this:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Define the *infinite* sequence of the prime numbers &lt;/li&gt;    &lt;li&gt;From this sequence, throw away the first 10000 items and then take the first of the remaining &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Creating an infinite sequence in C# is easy (since version 2) thanks to IEnumerables and, above all, the yield statement:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; Primes()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; 2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; primesSoFar = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; primesSoFar.Add(2);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPrime = n =&amp;gt; primesSoFar.TakeWhile(p =&amp;gt; p &amp;lt;= (&lt;span style="color: blue"&gt;int&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sqrt(n)).FirstOrDefault(p =&amp;gt; n % p == 0) == 0;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 3; &lt;span style="color: blue"&gt;true&lt;/span&gt;; i += 2)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (IsPrime(i))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; i;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; primesSoFar.Add(i);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The yield at line 3 returns the first item of the sequence: the always excepcional 2 (the only even prime). Then at line 5 we create a list where we will be saving the generated primes as we progress in the sequence (this way we will gain speed at the cost of memory). The IsPrime(n) function defined at line 9, proposes a method -pretty crude actually- of verifying whether a number is prime: we take all primes generated so far which are lower or equal than the square root of n, and we look for the first among them that divides n evenly, if such a divisor exists then n is not a prime, that is: if none of the primes already generated is an exact divisor of n then FirstOrDefault() returns a 0, signaling the fact that n is indeed a prime. Finally at line 10 starts the loop that, every time the Prime() invoker asks for an item, it progresses thru 3, 5, 7, 9, 11, 13, ... stopping and returning, thru yield, when it finds a new prime.&lt;/p&gt;  &lt;p&gt;With this sequence in our hands, step 2 of my plan is utterly simple:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; Primes().Skip(nth - 1).First();&lt;/p&gt;   &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;We take the Primes() sequence, ignore the first nth - 1 (in our case nth = 100001) and then take the first of the remaining. This little code returns the answer in far less than a second.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6149321" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx">Math</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item><item><title>The square of the sum vs. the sum of the squares</title><link>http://weblogs.asp.net/esanchez/archive/2008/04/27/the-square-of-the-sum-vs-the-sum-of-the-squares.aspx</link><pubDate>Mon, 28 Apr 2008 02:56:32 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6137746</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6137746</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/04/27/the-square-of-the-sum-vs-the-sum-of-the-squares.aspx#comments</comments><description>&lt;p&gt;The &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=6" target="_blank"&gt;sixth Project Euler problem&lt;/a&gt; poses an exercise that, to me, offers no major hurdles:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;What is the difference between the sum of the squares and the square of the sums [of a sequence of natural numbers]?&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The functional C# solution is fairly easy to write and read:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; SquareSumsLessSumSquares(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; sequence)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sum = sequence.Sum();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sumSquares = sequence.Select(i =&amp;gt; i * i).Sum();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum * sum - sumSquares;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We get any sequence of integers (list, array, etc.) and we find the sum of its elements at line 3. The Select() in line 4 generates a new sequence with the square of each item from the original sequence and then we add them up. From there, getting the answer to Problem 6 is easy. Actually, the specific value that the problem asks for is to find out this difference for the first 100 natural numbers, so the corresponding call is:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; result006 = SquareSumsLessSumSquares(&lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 100));&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Now, given that the sequence we are focused on is 1, 2, 3, ..., 100, we can leverage a couple of math formulas:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a title="The sum of the first n natural numbers" href="http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%C2%B7_%C2%B7_%C2%B7" target="_blank"&gt;&lt;img src="http://upload.wikimedia.org/math/f/1/a/f1a147372db0aa2d5cafc82164ea518c.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;y&lt;/p&gt;  &lt;p&gt;&lt;a title="The sum of the *squares* of the first n natural numbers" href="http://en.wikipedia.org/wiki/Square_pyramidal_number" target="_blank"&gt;&lt;img src="http://upload.wikimedia.org/math/7/7/9/779324dc10f4e762a3eb95f1c4977a6b.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Isn't the &lt;a href="http://en.wikipedia.org" target="_blank"&gt;Wikipedia&lt;/a&gt; something? With this information in our hands we can re-write our solution this way:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; SquareSumsLessSumSquaresFirstNumbers(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sum = n * (n + 1) / 2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sumSquares = n * (n + 1) * (2 * n + 1) / 6;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum * sum - sumSquares;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Clearly this latter way of solving the problem uses far less memory and CPU time. My first solution follows what is called a &amp;quot;brute force&amp;quot; approach, contemption intended. It works, for sure, but there are more elegant and efficient ways of solving the problem. The moral of this story is that a little research can take us a long way towards fresh solutions, probably better than our first approach. How frequently have you seen brute force solutions in production environments?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6137746" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item><item><title>Recursive lambdas and sequence aggregations</title><link>http://weblogs.asp.net/esanchez/archive/2008/04/24/recursive-lambdas-and-sequence-aggregations.aspx</link><pubDate>Thu, 24 Apr 2008 05:22:45 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6125982</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6125982</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/04/24/recursive-lambdas-and-sequence-aggregations.aspx#comments</comments><description>&lt;p&gt;The &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=5" target="_blank"&gt;fifth problem at Project Euler&lt;/a&gt; proposes this nostalgic primary school exercise: find the smallest quantity that is evenly divisible by some numbers, the &lt;a href="http://en.wikipedia.org/wiki/Least_common_multiple" target="_blank"&gt;least common multiple&lt;/a&gt; of 1, 2, 3, ..., 20 to be precise. To begin with, let's remember the old arithmetic formula:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img src="http://upload.wikimedia.org/math/4/d/2/4d244548521249d1b5a71941506d5f41.png" /&gt; &lt;/p&gt;  &lt;p&gt;Where gcd is the &lt;a href="http://en.wikipedia.org/wiki/Greatest_common_divisor" target="_blank"&gt;greatest common divisor&lt;/a&gt; of a and b. It's also useful to remember that the lcm is associative, that is lcm(1,2,3) is the same as lcm(lcm(1,2), 3), in other words, we can calculate the lcm of 1 and 2, then the lcm of this result and 3, then the lcm of this result and 4, and so on. In functional C#, these ideas can be expressed like so:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; gcd = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gcd = (x, y) =&amp;gt; x % y == 0 ? y : gcd(y, x % y);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 20).Aggregate(1, (x,y) =&amp;gt; x * (y / gcd(x, y)));&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Line 2 finds the gcd of x and y like this: if the remainder of dividing x by y is zero, then y is the gcd, if not, we must find the gcd of y and such remainder. Note that gcd is defined recursively. The only interesting point here is that in order to define a recursive lambda expression, it is mandatory to first declare the expression variable (which we do in line 1), and only after this the compiler allows us to define a lambda expression in terms of itself. Odd, granted, but easy to get used to.&lt;/p&gt;  &lt;p&gt;Line 4 takes the sequence 1, 2, ..., 20 and accumulates the lcm as the sequence is traversed, that is: it starts calculating lcm(1, 1) (let's call this accum1), then it calculates lcm(accum1, 2) (let's call this accum2), then it calculates lcm(accum2, 3), ..., and finishes with lcm(accum19, 20) which is precisely lcm(1, 2, ..., 20). The Aggregate() function is frequently used for chores like adding each term of a sequence, or multiplying them all, in our case we are using Aggregate() to get the succesive lcm's, which answers the question of Project Euler in two and a half lines of code, cool, eh?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6125982" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx">Math</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item><item><title>Nested sequences and palindrome numbers</title><link>http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx</link><pubDate>Tue, 22 Apr 2008 06:41:44 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6120740</guid><dc:creator>Edgar Sánchez</dc:creator><author>Edgar Sánchez</author><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/esanchez/rsscomments.aspx?PostID=6120740</wfw:commentRss><comments>http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4" target="_blank"&gt;Problem 4 of Project Euler&lt;/a&gt; poses and impractical albeit intriguing problem: given all three digit numbers (100, 101, 102, ..., 998, 999), find the largest product of 2 of those numbers, where the &lt;strong&gt;product is a palindrome&lt;/strong&gt;. For example, 580.085 is the product of two three-digit numbers (995 x 583) but it isn't the largest of such products. One possible functional C# solution is:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPalindrome = n =&amp;gt; { &lt;span style="color: blue"&gt;var&lt;/span&gt; nchar = n.ToString().ToCharArray(); &lt;span style="color: blue"&gt;return&lt;/span&gt; nchar.SequenceEqual(nchar.Reverse()); };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(100, 900)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .SelectMany(i =&amp;gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(i, 900 - (i - 100)).Select(j =&amp;gt; i * j))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where (p =&amp;gt; IsPalindrome(p))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Max();&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;To begin with, let's focus on the interesting part: at line 3, Enumerable.Range() generates the sequence 100, 101, ..., 998, 999; SelectMany() at line 4 does several things, first &lt;strong&gt;for every number i from the previous line&lt;/strong&gt; it generates the sequence i, i + 1, i + 2, ..., 998, 999, and then the inner Select() generates the products {100 x 100, 100 x 101, ..., 100 x 999}, {101 x 101, 101 x 102, ..., 101 x 999 }, ... As you can see, there are 999 sequences generated (are all the same size?) finally SelectMany() kindly &amp;quot;flattens&amp;quot; those 999 sequences in one large sequence containing each and every product; line 5 then is easy, the Where() just keeps those products that are palindromes; the Max() in line 6 isn't worth any explanation.&lt;/p&gt;  &lt;p&gt;Back to line 1, I took the lazy path to check whether a number is a palindrome: I just converted it to a string and then to a char array, finally I checked whether that array is equal, item by item, to its reversed version (and all that work just to leverage IEnumerable.Reverse() ;-)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6120740" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx">Project Euler</category></item></channel></rss>