Friday, 16 August 2013

How to stop adding numbers that occur twice in C#

How to stop adding numbers that occur twice in C#

So I am trying to find an answer to the question:
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.
I am using C# and have a pretty good idea of what to do, but my code keeps
counting the numbers that occur twice (e.g. 15, 30) and I would like to
know the quickest/easiest way to counteract that. everything I have found
so far has been in a different language so I am sorry if this seems
relatively easy to you. This is what I have so far:
static void Main(string[] args)
{
var result1 = 0;
var result2 = 0;
var result3 = 0;
var uniqueInts3 = new List<int>();
for (var i = 0; i < 100; i += 3)
{
uniqueInts3.Add(i);
result1 += i;
}
var uniqueInts5 = new List<int>();
for (var o = 0; o < 100; o += 5)
{
uniqueInts5.Add(o);
result2 += o;
}
result3 += result1 + result2;
Console.WriteLine(result3);
Console.ReadLine();
}
I would love if someone could explain to me what to do as I am not sure at
this point.

No comments:

Post a Comment