Project Euler :: Problem #1

October 15th, 2009 § 0

Project Euler, for those that don”t know, is a pretty neat little website that presents some fun questions that take some math skills and the ability to use the math you know in a programming language. There is no requirement or suggestion for what language to use, or anything like that. Just that you get the right answer to the question.

I’ve been doing these on and off for about two weeks now, and I have only finished the first 13 (there are a total of 235). On to Problem 1…

Problem #1 says:

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.

So there are a few things we have to do to solve this problem. We need to iterate up to 1000 while determining if each stop along the way is divisible by either 3 or 5, and if a number is divisible by either one then we need to add it to a running total. Also, please note I like to assign the “variable” parts of these problems via input from the command-line.

using System;
class Program
{
  public static void Main(string[] Args)
  {
    if (Args.Length < 1)
      Args = new string[] { "1000" };

    int s = 0;
    for (int i = 1; i < Int32.Parse(Args[0]); i++)
    {
      if (i % 3 == 0 || i % 5 == 0)
        s += i;
    }

    Console.WriteLine(s.ToString());
  }
}

There we go, that’s all there is to it.

§ Leave a Reply

What's this?

You are currently reading Project Euler :: Problem #1 at SoggyByte.

meta