## Archive for the ‘**General**’ Category

## Stackoverflow

I’ve recently started to post answers at Stackoverflow.

I think its a good way to contribute to the developer community and is a great medium to exchange ideas / solutions. I like their interface and is well done.

Its been a week since I started to post, I’ve got a score of 145. Check out my profile here.

## Project Euler #36

Problem: Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.

Answer: **872187**

Solution: I’m using String Reverse to figure out if its a palindrome. I’m not sure if there is a better way to do it.

var lQuery = from lNumber in Enumerable.Range(1, 1000000) let lBase10 = lNumber.ToString() let lBase10Rev = new string(lBase10.Reverse().ToArray()) let lBase2 = Convert.ToString(lNumber, 2) let lBase2Rev = new string(lBase2.Reverse().ToArray()) where lBase10 == lBase10Rev && lBase2 == lBase2Rev select lNumber; var lAnswer = lQuery.Sum();

Time: 6417 ms

## Project Euler #16

Problem: What is the sum of the digits of the number 2^{1000}?

Answer: **1366**

Solution: I’d to use F# BigInt and my ToDigits() extension method from the previous post.

var lNum = BigInt.Pow(new BigInt(2), BigInt.FromInt32(1000)); var lAnswer = lNum.ToDigits().Sum();

Time: 12 ms

## Project Euler #13

Problem : Find the first ten digits of the sum of one-hundred 50-digit numbers.

Answer: **5537376230**

Solution:

Thanks to this article, I got a good tip for reading file contents line by line, the Functional way. Since these were big numbers I used F# BigInt to solve the problem

var lContent = ReadLines("Problem13.txt") .Select(pArg => BigInt.Parse(pArg)); var lAnswer = lContent.Aggregate(BigInt.Zero, (pSeed, pId) => pSeed + pId).ToString().Substring(0, 10);

Time: 25 ms

## Project Euler #22

*Problem:* What is the total of all the name scores in the file of first names?

Answer: **871198282**

*Solution*: It was quite easy to implement with LINQ. This goes to show the power of LINQ and the readability of a Functional Program.

var lContent = ReadFile("Problem22.txt"); var lWords = lContent .Replace("\"", "") .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var lNumWords = lWords .OrderBy(pArg => pArg) .Select((pString, pIndex) => (pIndex + 1) * pString.Sum(pCharArg => pCharArg.ToOrdinal()) ); var lAnswer = lNumWords.Sum();

*Time*: 59 ms