Vasu Balakrishnan’s Blog

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: