Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Project Euler Problem #1

, 10 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
CodeProjectAlright, here we go on Project Euler. Time for the first problem:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.That doesn't sound quite that hard. Righ

Alright, here we go on Project Euler. Time for the first problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
<br />
<br />Find the sum of all the multiples of 3 or 5 below 1000.

That doesn't sound quite that hard. Right? The first problem couldn't possibly be hard.

Well let's break it down. All the numbers that are multiples of 3 or 5 under 10 are 3, 5, 6 and 9. Or in other words, 3, 5, 6 and 9 are the only numbers (under 10) that are divisible by 3 or 5 and leave no remainder behind.

So now we have to find all the numbers under 1000 that satisfy the same requirement. And then sum those numbers.

At first thought I suppose it would be easy enough to create a loop that goes through each possibility and adds the number to a list if it passes the divisible test.

Something like this:


 List<int> multiples = new List<int>();

 for (int i = 1; i < 1000; i++)
 {
     if ((i % 3 == 0) || (i % 5 == 0))
     {
         multiples.Add(i);
     }
 }

 return multiples.Sum();

Simple enough right? Well yes and for this simple type of problem, it won't really have any affect on performance. However, I wanted the least amount of lines as possible. I had recently discovered the wonderful world of lambda expressions and I feel that I should use them as often as I can. (for better or for worse)

So here is my solution:

 return Enumerable.Range(1, 999).Where(i => (i % 3).Equals(0) || (i % 5).Equals(0)).Sum();

Ah. Enumerables. By using the 'Range' extension I can retrieve an enumerable list of ints to select from. The 'Where' extension method will ensure that I only retrieve the numbers that are multiples of 3 or 5. And finally, the 'Sum' extension will return the sum of all the numbers left.

Or if we want to get really fancy, we could create a lambda function delegate to make things a bit more readable:


 Func<int, int, bool> IsMultiple = (int i, int j) => (i % j).Equals(0);
 return Enumerable.Range(1, 999).Where(i => IsMultiple(i, 3) || IsMultiple(i, 5)).Sum();

However, the previous solution will do in this case.

Simple and concise. KISS principle in action.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jared Wallace
Business Analyst
United States United States
Visit my blog, Aimless Technical Babble

Comments and Discussions

 
QuestionLazy not simple PinmemberZebedee Mason15-Aug-11 21:35 
Try
 
def Euler(n):
    return n * (n+1) / 2
 
def Summation(n):
    return 3 * Euler(n/3) + 5 * Euler(n/5) - 15 * Euler(n/15)
 
instead and notice the speed difference between constant time and linear time.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 Aug 2011
Article Copyright 2011 by Jared Wallace
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid