Monday, 8 March 2010

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.

Wednesday, 3 March 2010

Problem 001

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:
Function Prob001() As Integer
For k As Integer = 3 To 999
If k Mod 3 = 0 OrElse k Mod 5 = 0 Then Prob001 += k
Next
End Function

Summary:
Easy-peasy.

Friday, 26 February 2010

Getting To Know Eu…ler

One of the best ways to get to know a programmer is to see their code. You might not know their favorite hobbies, favorite foods, or favorite football team (trick question, programmers don’t like football. Hehe), but getting a glimpse at how someone goes about solving problems can really be quite helpful.

With that in mind, I’m going to be taking part in ProjectEuler.net. It’s all math-based to one degree or another. I am horrible at math, really. I know enough to get by, but a lot of it I’ve forgotten since being out of school. But, I must admit, I love a figuring out puzzles — especially by programming — so I’m anxious to get started.

If you’d like to follow along, feel free to set up an account on their site and try the stuff out yourself. It can get quite addictive :)

Since I’m going to post code-samples here, I might as well include one of my “helper” routines I ended up using in a couple of solutions. Since a few of the problems involved prime numbers, I made a generic function for allowing me to determine whether a specific number is a prime or not.

I’m sure there are more effective ways of figuring out if a number is a prime, but math was never a subject a particularly excelled at…

Function IsPrime(ByVal tmpVal As Long) As Boolean
Dim strVal As String = tmpVal.ToString

'If the number ends in 0,2,4,5,6,8 then it's NOT prime (might be a composite)
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "0" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "2" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "4" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "5" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "6" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "8" Then Return False

'Except for the number 2, if a number is even, it's always composite
If tmpVal <> 2 AndAlso tmpVal Mod 2 = 0 Then Return False

'If a number's digits add up to a number which is divisible by 3, it's composite
Dim tmpDigitSum As Integer = 0
If strVal.Length > 1 Then
For k As Integer = 0 To strVal.Length - 1
tmpDigitSum += strVal.Substring(k, 1)
Next
If tmpDigitSum = 3 Then Return False
End If

'If a number's square root is an integer, it's a composite
If System.Math.Sqrt(tmpVal) = CInt(System.Math.Sqrt(tmpVal)) Then Return False

'If it's not divisible by itself, it's not Prime
If tmpVal Mod tmpVal <> 0 Then Return False

Dim MaxVal As Long = System.Math.Sqrt(tmpVal)

For k As Long = 2 To MaxVal
If tmpVal Mod k = 0 AndAlso k <> 1 Then
Return False
End If
Next

Return True
End Function