Vasu Balakrishnan’s Blog

Archive for the ‘General’ Category

Worth a read

leave a comment »

Written by nbvasu

August 18, 2009 at 10:19 am

Posted in General

Stackoverflow

leave a comment »

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

Written by nbvasu

June 10, 2009 at 12:21 pm

Posted in General

Differences Between WPF and Silverlight

leave a comment »

Check this out here

Written by nbvasu

June 10, 2009 at 11:57 am

Posted in General

Project Euler #36

leave a comment »

 

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

Written by nbvasu

May 15, 2009 at 3:41 pm

Posted in C#, Euler, General

Project Euler #16

leave a comment »

Problem: What is the sum of the digits of the number 21000?

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

Written by nbvasu

May 15, 2009 at 3:20 pm

Posted in C#, Euler, General

Project Euler #13

leave a comment »

 

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

Written by nbvasu

May 15, 2009 at 3:14 pm

Posted in C#, Euler, General

Project Euler #22

leave a comment »

 

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

Written by nbvasu

May 15, 2009 at 2:40 pm

Posted in C#, Euler, General