Bill Blogs in C#

Bill Wagner discusses C#, LINQ, and other items of interest

Project Euler: Problem 1

I am starting a new series. A friend recently pointed me at the Project Euler problems. You can see the project here: http://projecteuler.net/

As I finish them, I'll post an explanation of the problem and the code here. I've also posted the code on MSDN Code Gallery. As I finish a week's worth of code, I'll post a new release of source code there.

A few ground rules:

  1. I'm not going for the 'best' or 'most efficient' solution. I'm using this to demonstrate significant C# 3.0 features. If you have a different preferred algorithm, post it yourself. In fact, you can register at Project Euler and post your own code.
  2. I will post blog entries more or less at the same time I update the code. You might see the code before I discuss it. If you don't want spoilers, don't do it.
  3. I will discuss the algorithms and the answers freely. If you want to figure out the problems yourself, don't read too far before you try it yourself. Or, once again, signup at Project Euler and try yourself before I post my answers.
  4. I will do the problems in order, so if you're interested in avoiding spoilers, you can work in order, and stay ahead of where I'm at. (The first three problems took me less than 2 hours from start to finish).

Ok, with that out of the way, here's the code for the first problem. (Really, it's not that hard. I'm not giving much away here). In the first problem, you need to find the sum of all natural numbers below 1000 that are multiple of 3 and 5.

It's one (or two) lines of code in C# 3.0:

var nums = from n in
Generators.NumberSequence(0, 1000)
where ((n % 3 == 0) || (n % 5 == 0))
select n;
var answer = nums.Sum();

I should also show you the code for NumberSequence. I figure I'll need sequences of numbers fairly often in this projecxt, so I pulled that into a library:

public static IEnumerable<int> NumberSequence(int from, int to)
{
do
{
yield return from++; } while (from < to); }

Yes, this method assumes that from is less than to. Seems like a reasonable assumption at this time.

You can download the code for the first three problems here: http://code.msdn.microsoft.com/EulerCSharp

Note that I said 3 problems. I uploaded the code for the first three problems on Project Euler, so you may want to wait if you want to avoid spoilers.

Published Tue, Mar 25 2008 9:36 PM by wwagner
Filed under: , , ,

Comments

# re: Project Euler: Problem 1@ Wednesday, March 26, 2008 7:14 AM

It looks like your "Generators.NumberSequence" is already covered by "Enumerable.Range" so you could switch to using that and save a few more lines of code.

by Wilka

# re: Project Euler: Problem 1@ Wednesday, March 26, 2008 8:08 AM

Instead of Generators.NumberSequence, you could use the built in Enumerable.Range(0, 1000)

# re: Project Euler: Problem 1@ Wednesday, March 26, 2008 3:45 PM

You're both right.  I wasn't thinking, and I'd had a method that generated every Nth number (e.g. 0.3.6.9.12.15 ...).  It was right in front of me and I just modified it.

by wwagner

# re: Project Euler: Problem 1@ Thursday, March 27, 2008 9:24 AM

It should be noted that calling Enumerable.Range(0, 1000) is not exactly the same as Generators.NumberSequence(0, 1000).

The Enumerable call would include 1000 in the result set while the NumberSequence would exclude it.

Since the problem is to find the results less that 1000, if you are going to use the Enumberable.Range, you would want to use

Enumerable.Range(0, 999)

by epotter

# re: Project Euler: Problem 1@ Thursday, March 27, 2008 12:04 PM

Minor quibble: your solution produces the sum of numbers divisible by 3 OR 5, not 3 AND 5, in the mathematical sense of OR and AND.  Your solution of course solves the problem that was posited.  It's just a typo in your description of the problem being solved.  Might mislead those who don't check the eulerproject website.

by george byrkit

# Project Euler in Ruby@ Sunday, April 06, 2008 2:06 PM

I&#39;m taking Bill Wagner up on his Project Euler challenge , but in Ruby. I&#39;ve secretly wanted

# links for 2008-09-09 &laquo; dstelow notes&#8230;@ Tuesday, September 09, 2008 7:39 PM

Pingback from  links for 2008-09-09 &laquo; dstelow notes&#8230;