This will be the first of my analysis of the Project Euler problems. It will contain spoilers and code. If you don’t wish to spoil the fun then don’t read.
Introduction
“Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.” – Project Euler website
I’m attempting to go through these one at a time and offer my reasoning behind my answers as well as my code. I haven’t gone through all of them so this is a continuing process.
Problem
“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.” – Project Euler website
Solution
This sounds remarkably like the FizzBuzz problem. And accordingly the solution is fairly easy.
Basically, we are going to use the modulus operator to determine if the number is divisable by 3 or 5. If it is we add it to a list of numbers. Once we’re done we add up all the numbers in our list. This gives the answer of 233,168. It takes my machine about 15 milliseconds to run the program.
Code (VB 2008)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim stopwatch As New Stopwatch
stopwatch.Start()
Dim numbers As New List(Of Int64)
For counter As Int64 = 1 To 999
If counter Mod 3 = 0 Or counter Mod 5 = 0 Then
numbers.Add(counter)
End If
Next
Dim sum As Int64
For Each number As Int64 In numbers
sum += number
Next
Debug.WriteLine(sum)
stopwatch.Stop()
Debug.WriteLine("Took " & stopwatch.ElapsedMilliseconds & " milliseconds.")
stopwatch.Reset()
End Sub
End Class
Download
You can download the “Project Euler – Problem 1″ code here. You can use VB 2008 Express to open it.





