.NET at 9.400 ft above sea levelProgramming in Quito, 2.860 m above sea levelThe square of the sum vs. the sum of the squares- April 28, 2008 The sixth Project Euler problem poses an exercise that, to me, offers no major hurdles: What is the difference between the sum of the squares and the square of the sums of a sequence of natural numbers The functional C solution is fairly easy to write and read: &160;&160;&160; 1&160;&160;&160;&160; public int SquareSumsLessSumSquares(IEnumerable<int> sequence) &160;&160;&160; 2&160;&160;&160;&160; &160;&160;&160; 3&160;&160;&160;&160;&160;&160;&160;&160; var sum =...http://weblogs.asp.net/esanchez/archive/2008/04/27/the-square-of-the-sum-vs-the-sum-of-the... Recursive lambdas and sequence aggregations- April 24, 2008 The fifth problem at Project Euler proposes this nostalgic primary school exercise: find the smallest quantity that is evenly divisible by some numbers, the least common multiple of 1, 2, 3, ..., 20 to be precise. To begin with, let's remember the old arithmetic formula: &160; Where gcd is the greatest common divisor 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,...http://weblogs.asp.net/esanchez/archive/2008/04/24/recursive-lambdas-and-sequence-aggregat... Nested sequences and palindrome numbers- April 22, 2008 Problem 4 of Project Euler 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 product is a palindrome. 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: &160;&160;&160; 1&160;&160;&160;&160; Func<int,bool> IsPalindrome = n => var nchar =...http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers... Finding the largest prime factor of a number- April 19, 2008 This is another classic problem at Project Euler that can be solved with the old trick of top down programming, like so: PrimeFactors(number).DefaultIfEmpty(number).Max(); It's a nice solution, supposing PrimeFactors() actually returns all prime factors of any given number. But before getting there, it's a good idea to discuss what DefaultIfEmpty() is doing there. The thing is that somebody could make this innocent call: PrimeFactors(1), which of course returns an...http://weblogs.asp.net/esanchez/archive/2008/04/19/finding-the-largest-prime-factor-of-a-n... Project Euler and infinite sequences in C- April 17, 2008 The second problem at Project Euler proposes: 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, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. One way of solving this problem is by defining an endless sequence in C. Something along these lines: &160;&160;&160; IEnumerable<int> FibonacciSequence() ...http://weblogs.asp.net/esanchez/archive/2008/04/17/project-euler-and-infinite-sequences-in... Project Euler and functional C- April 16, 2008 I think I already talked (a long time ago) about Project Euler: a set of problems, mainly math oriented, keen to be solved with a computer program. To be sure, it's not particularly deep math, but the problems posed go fairly quickly from really easy, to hmm, to challenging. In this sense, they are good programming calisthenics, and may also be a good way of learning a new language. For example, Chris Smith has solved some Project Euler problems using F, I find the idea intriguing, so I'll try..http://weblogs.asp.net/esanchez/archive/2008/04/16/project-euler-and-functional-c.aspx |