Tuesday, 9 March 2010

Problem 006

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.

Solution:
Function Prob006() As Long
Dim SumOfSquares As Long = 0
For k As Integer = 1 To 100
SumOfSquares += System.Math.Pow(k, 2)
Next
Dim Sum As Long = 0
For k As Integer = 1 To 100
Sum += k
Next
Dim SquareOfSum As Long = System.Math.Pow(Sum, 2)
Return SquareOfSum - SumOfSquares
End Function

Summary:

Monday, 8 March 2010

Problem 005

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?

Solution:
Function Prob005() As Integer
Dim FoundResult As Boolean = False
Dim k As Integer = 0
While FoundResult = False
k += 1
FoundResult = True
For f As Integer = 1 To 20
If k Mod f <> 0 Then
FoundResult = False
End If
Next
If FoundResult = True Then Return k
End While
End Function

Summary:
This was pretty easy, but it seems like the problems are building upon each other, so I’m sure the next few are going to be a major pain…

Problem 004

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.

Solution:
Function Prob004() As Integer
For k As Integer = 100 To 999
For f As Integer = 100 To 999
Dim intResult As Integer = k * f
Dim strResult As String = intResult.ToString
If strResult = StrReverse(strResult) AndAlso intResult > Prob004 Then
Prob004 = intResult
End If
Next
Next
End Function

Summary:
This one was pretty quick and fun.

Sunday, 7 March 2010

Problem 003

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Solution:
Function Prob003() As Long
Dim Src As Long = 600851475143
Dim PrimeCol As New Collection
For tmpVal As Long = 2 To Src
If tmpVal > System.Math.Sqrt(Src) Then Exit For
If IsPrime(tmpVal) = True Then
PrimeCol.Add(tmpVal)
End If
Next
For k As Integer = 1 To PrimeCol.Count
If Src Mod PrimeCol(k) = 0 Then
If PrimeCol(k) > Prob003 Then Prob003 = PrimeCol(k)
End If
Next
End Function

Summary:
This one was kind of a pain. I had forgotten a lot about what makes a prime and the various rules, tricks, etc. when dealing with them…

Thursday, 4 March 2010

Problem 002

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, …

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

Solution:
Function Prob002() As Long
Dim Term1 As Long = 1
Dim Term2 As Long = 2
Dim tmpTerm As Long = 0
While Term1 < = 4000000 If Term1 Mod 2 = 0 Then Prob002 += Term1 tmpTerm = Term1 + Term2 Term1 = Term2 Term2 = tmpTerm End While End Function

Summary:
Even though it'll never be as exciting as in the movie Pi, I do love working with Fibonacci sequences.