Project Euler With VBA

Tackling problems from project Euler using VBA

Project Euler With VBA

Project Euler is a website set up with a wide selection of math problems. While some, if not all of them, could be figured out manually over time you could be looking at taking weeks or months to find an answer.

The idea is that you use any form of coumputer programming you are happy with to create a programme that returns the answer. The challenge is knowing how to structure and write the programme to complete the task at hand. When done correctly a computer can return the answer instantly or in seconds.

The puzzles can all be found at https://projecteuler.net and if you create an account you can select a puzzle and check if you answer is correct.

My Solutions

The following are just my methods I used to clear some of the puzzles on the website and there are no doubt better, cleaner ways to get to the same outcome. I'm not posting the answers here but they are all tested and verified before uploading.


Problem 1

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.
Sub Problem1()

Dim Answer As Long

Answer = 0

For x = 1 To 999
    If ((x / 3) - Int((x / 3))) = 0 Or ((x / 5) - Int((x / 5))) = 0 Then
        Answer = Answer + x
    Else
        Answer = Answer
    End If
Next

MsgBox Answer

End Sub

Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Sub Problem2()

Dim Answer As Long
Dim x As Long
Dim y As Long
Dim z As Long

Answer = 0
x = 0
y = 1

Do While z < 4000000
    z = x + y
    If ((z / 2) - Int(z / 2)) = 0 Then
        Answer = Answer + z
    End If
    x = y
    y = z
Loop

MsgBox Answer

End Sub

Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

On this one I did create a way to calculate the answer but due to limitations with integer values in VBA running the answer for the actual problem generates an overflow error. As such when I knew that the solution I made worked I ran a quick python script to generate the correct answer (also below).

Dim Answer As Long
Dim Factors As Integer
Dim Number As Integer
Dim x As Long
Dim y As Long

Answer = 0
Factors = 0
Number = 13195

Do While Answer = 0
    For y = Number To y = 1 Step -1
        For x = 1 To Number
            If y Mod x = 0 Then
                Factors = Factors + 1
            End If
        Next x
        
        If Factors = 2 And Number Mod y = 0 Then
            Answer = y
            Exit For
        Else
            Factors = 0
        End If
        
    Next y
Loop

MsgBox Answer
Number = 600851475143
x = 2

while x * x < Number:
    while Number % x == 0:
        Number = Number / x
    x = x + 1
    
print (Number)

Problem 4

A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Sub Problem4()

Dim Answer As Long
Dim Working As Long
Dim Highest As Long
Dim x As Long
Dim y As Long
Dim LeftNum As String
Dim RightNum As String

Answer = 0
Working = 0
Highest = 0
x = 999
y = 999

Do While Answer = 0 And x > 1 And y > 1
    Working = x * y
    LeftNum = Left(Working, 3)
    RightNum = Right(Working, 3)
    If LeftNum = StrReverse(RightNum) Then
        If Working > Highest Then
            Highest = Working
            x = 999
            y = y - 1
        ElseIf Highest > Working Then
            Highest = Highest
            x = 999
            y = y - 1
        End If
    End If
    x = x - 1
Loop

Answer = Highest

MsgBox Answer

End Sub

Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Sub Problem5()

Dim Answer As Long
Dim x As Long
Dim y As Long

Answer = 0
x = 10
y = 1

Do While Answer = 0
    If ((x / y) - Int(x / y)) = 0 Then
        If y = 20 Then
            Answer = x
        Else
            y = y + 1
        End If
    Else
        x = x + 1
        y = 1
    End If
Loop

MsgBox Answer

End Sub

Problem 6

The sum of the squares of the first ten natural numbers is,
1 2 + 2 2 + ... + 10 2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10) 2 = 55 2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Sub Problem6()

Dim Answer As Long
Dim x As Long
Dim Working1 As Long
Dim Working2 As Long

Answer = 0
x = 1
Working1 = 0
Working2 = 0

For x = 1 To 100
    Working1 = Working1 + (x ^ 2)
    Working2 = Working2 + x
Next

Working2 = Working2 ^ 2

Answer = Working2 - Working1

MsgBox Answer

End Sub

Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
Sub Problem7()

Dim Answer As Long
Dim Number As Long
Dim Working As Long
Dim Factors As Long
Dim x As Long
Dim y As Long

Answer = 0
Number = 10001
Working = 0
Factors = 0
y = 2

Do While Working < Number
    For x = 1 To y
       If y Mod x = 0 Then
           Factors = Factors + 1
       End If
    Next x
    
    If Factors = 2 Then
        Answer = y
        Working = Working + 1
    End If
    
    y = y + 1
    Factors = 0
Loop

MsgBox Answer

End Sub

Problem 8

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Sub Problem8()

Dim Answer As String
Dim Working As String
Dim TestString As String
Dim AdjNum As Integer
Dim x As Integer
Dim y As Integer

Answer = 0
Working = 1
AdjNum = 13
TestString = "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823063465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

For y = 1 To (999 - AdjNum)
    For x = 0 To (AdjNum - 1)
        Working = Mid(TestString, (y + x), 1) * Working
    Next x
    
    If Working + 0 > Answer Then
        Answer = Working
    End If
    
    Working = 1
Next y

MsgBox Answer

Problem 9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a 2 + b 2 = c 2
For example, 3 2 + 4 2 = 9 + 16 = 25 = 5 2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
Sub Problem9()

Dim Answer As Long
Dim x As Long
Dim a As Long
Dim b As Long
Dim c As Long

For a = 1 To 1000
    For b = 1 To 1000
        c = (a ^ 2) + (b ^ 2)
        If (Sqr(c) - Int(Sqr(c))) = 0 Then
            x = a + b + Sqr(c)
            If x = 1000 Then
                Answer = a * b * Sqr(c)
                Exit For
            End If
        End If
    Next b
    
    If Answer > 0 Then
        Exit For
    End If

Next a

MsgBox Answer

End Sub

Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

This one could do with some level of rework before I would be happy with it as it took a little over 8 mins to run all around and I like to keep them under the minute mark as per the website guidelines. I mean it still works but there is obviously a cleaner way to do this.

Sub Problem10()

Dim Answer As String
Dim Factors As Long
Dim Number As Long
Dim x As Long
Dim y As Long

Answer = "0"
Factors = 0
Number = 2000000
y = 2

Do While y < Number
    For x = 1 To y
        If y Mod x = 0 Then
                Factors = Factors + 1
                If Factors = 3 Then
                    Exit For
                End If
        End If
    Next x
    
    If Factors = 2 Then
        Answer = (Answer + 0) + (x - 1)
    End If
        
    Factors = 0
    y = y + 1
Loop

MsgBox Answer

End Sub

Problem 11

Still working on it :)

Problem 12

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?

No doubt this one is a brute force effort and takes far to long to run repeatedly but it does give the correct soultion. Thinking again I could have it account for the square root of each triangle number and stop attempting to divide each number for factors when it crosses that point which should save time when working with such high numbers.

Sub Problem12()

Dim Answer As String
Dim Factors As Long
Dim x As Long
Dim y As Long
Dim z As Long

Answer = "0"
Factors = 0
y = 1
z = 1

Do While Answer = 0

    y = (z * (z + 1)) / 2
    
    For x = 1 To y
        If y Mod x = 0 Then
                Factors = Factors + 1
        End If
    Next x
    
    If Factors > 500 Then
        Answer = y
        Exit Do
    End If
        
    Factors = 0
    z = z + 1
    
Loop

MsgBox Answer

End Sub