Vasu Balakrishnan’s Blog

Project Euler #30

leave a comment »

Problem: Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Answer: 443839

Solution:

My first pass was to check all numbers between 2 and 1 million. Then I realized that I could reduce the range. The max value for a digit is 9^5 (59049) and the max number could be 59049 times number of digits. This is more of a brute force. I’m sure there is a better way to get down the max range. I’ve reused my ToDigits extension method.

var x =  Enumerable.Range(2, 295245)
          .Where(pArg => pArg.ToDigits().Sum(pArg1 => Math.Pow(pArg1, 5)) == pArg)
          .Select(pArg => pArg);
var lAnswer = x.Sum();

 

Time: 930 ms

Advertisements

Written by nbvasu

May 19, 2009 at 10:47 pm

Posted in C#, Euler

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: