For fun one day, I did the first 10 Project Euler problems in VB.NET. Some people make comments that dot net is too inefficient to do real number crunching and hard work, but for these problems I found it to work just fine. I’ve supplied the Visual Studio 2010 version.

The project has three parts; the main module this used to call the various methods of the Problems Class. Changing the method will change problem that is ran.

Module Module1

    Sub Main()

        Dim p As New Problems
        'Change this to target another solution - eg. p.Prob10()
        p.Prob1()

        'So we can see the output in the console
        System.Console.ReadKey()

    End Sub

End Module

The second piece of the project is the Problems class. The details of each solution are in this file – below is a small snippet.

Imports System.Math

Public Class Problems



    Public Function Prob1()

        '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.

        Dim i As Integer = 999
        Dim s As Integer
        Dim sf As Integer

        While i > 0

            If (i Mod 3 = 0) Or (i Mod 5 = 0) Then

                System.Console.WriteLine(i)
                s = i
                sf = sf + s

            End If

            i = i - 1

        End While

        System.Console.WriteLine(vbCrLf & "Sum: " & sf)

        Return 0

    End Function


The last file is a helper class called Primes that is used in various solutions to find the nth prime or generate a count of consecutive prime numbers.

Here is the Primes Class:

Public Class Primes



    Public Function generateByMax(ByVal numberOfPrimes As Long)

        'By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
        'Dim max As Double = Math.Sqrt(numberOfPrimes)
        Dim ListOfPrimes As List(Of Long) = New List(Of Long)
        Dim num As Integer = 0


        While num <= numberOfPrimes

            If (IsPrime(num)) Then

                ListOfPrimes.Add(num)

            End If

            num = num + 1

        End While

        Return ListOfPrimes

    End Function

    Public Function generateByCount(ByVal countOfPrimes As Integer)

        Dim ListOfPrimes As List(Of Long) = New List(Of Long)
        Dim num As Integer = 0

        While ListOfPrimes.Count <= countOfPrimes - 1

            If (isPrime(num)) Then

                ListOfPrimes.Add(num)

            End If

            num = num + 1

        End While

        Return ListOfPrimes

    End Function

    'This was adapted from the C fuction 
    Private Function isPrime(ByVal number As Long)

        Dim j As Integer = 1

        If number = 2 Or number = 3 Then
            Return True
        End If

        If (number Mod 2 = 0) Or (number Mod 3 = 0) Or number = 1 Then

            Return False

        End If

        While (6 * j + 1) <= Math.Sqrt(CType(number, Long)) Or (6 * j - 1) <= Math.Sqrt(CType(number, Long))

            If (number Mod (6 * j + 1) = 0) Or number Mod (6 * j - 1) = 0 Then

                Return False

            End If
            j = j + 1
        End While

        Return True

    End Function

End Class

If you want to download the entire project; you can download it from here - ProjectEuler-VS2010.