C# Implementation of Problem 1

View source code here on GitHub!

class p0001
: Euler.IEuler
object Answer ()
 1/*
 2Project Euler Problem 1
 3
 4I know that this could be done faster with a traditional for loop, but I wanted
 5to see if iterators were reasonably possible in C, since it makes the prime
 6number infrastructure a lot easier to set up.
 7
 8Problem:
 9
10If we list all the natural numbers below 10 that are multiples of 3 or 5, we
11get 3, 5, 6 and 9. The sum of these multiples is 23.
12
13Find the sum of all the multiples of 3 or 5 below 1000.
14*/
15using System;
16
17namespace Euler
18{
19    public class p0001 : IEuler
20    {
21        public object Answer()
22        {
23            int answer = 0;
24            for (int i = 0; i < 1000; i += 3)
25                answer += i;
26
27            for (int i = 0; i < 1000; i += 5)
28                answer += i;
29
30            for (int i = 0; i < 1000; i += 15)
31                answer -= i;
32
33            return answer;
34        }
35    }
36}

Tags: divisibility