Project Euler 1 VB.net

NOTE: I made this for educational purposes and helping you if you’re stuck.  I recommend you trying this out for yourself before using this.

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.

Solution:

First, consider what types of math operations you’re going to require for this.  You will need to use modulus (5 mod 2 = 1).  Next, you will need to see that a For Loop and an If statement.  Only two variables should need to be declared.

Now you need to get programming.  I start by making my For Loop:

For x = 1 To 999

Next

Next, put the If statement inside the For Loop (this assumes that variables x and y are declared as integers):

If x Mod 3 = 0 Or x Mod 5 = 0 Then
     y += x
End If

Finally, put the whole thing together and you should get something looking like this:

Module Module1

   Sub Main()
       Dim x, y As Integer

       For x = 1 To 999
           If x Mod 3 = 0 Or x Mod 5 = 0 Then
               y += x
           End If
       Next

       Console.WriteLine(y.ToString)
       Console.ReadLine()
   End Sub

End Module

That’s all there is to it.  As you can see, this was pretty simple if you knew how to work with the modulus operator.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *