Finding a palindromic number
As part of Project Euler I have to find the largest palindrome made from the product of two 3-digit numbers.
I have created this code but when i3 gets to 909090 there is an error which states largest "Index was outside the bounds of the array." on the line
Code:
If i3(0) = i3(5) And i3(1) = i3(4) And i3(2) = i3(3) Then
Here is my full code, I'm not sure exactly what is wrong?
Also, i3 is a string because I couldn't work out how to go through an integer digit by digit?
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 999
Dim i2 As Integer = 999
Dim i3 As String
Dim found As Boolean = False
Do Until found = True
If i2 > 1 Then
i2 = i2 - 1
Else : i2 = 999
i = i - 1
End If
i3 = i * i2
If i3.Length = 5 Then
If i3(0) = i3(5) And i3(1) = i3(4) And i3(2) = i3(3) Then
found = True
End If
ElseIf i3.Length = 4 Then
If i3(0) = i3(4) And i3(1) = i3(3) Then
found = True
End If
End If
Loop
MsgBox(i3)
End Sub
End Class
Re: Finding a palindromic number
if i3.Length = 5 Then there ain't no such thing as i3(5)!
Re: Finding a palindromic number
Rather than going through and checking based on length you could use SequenceEqual.
Code:
productString.SequenceEqual(productString.Reverse)
I would also recommend using meaningful variable names. i, i2 & i3 are terrible names for variables (especially i3 which is a string). You'll thank yourself later when you come back to a project and can immediately tell what a variable is for.
Re: Finding a palindromic number
For reference this is Problem 4.
Solution based on this:
Code:
Dim palindrome As New List(Of String)
Dim stpw As New Stopwatch
stpw.Reset()
For num1 As Long = 999 To 110 Step -1
If num1 Mod 11 = 0 Then
For num2 As Long = 999 To 100 Step -1
Dim num3 As String = (num2 * num1).ToString
If num3.Length = 6 Then
Dim m As String = num3.Substring(0, 3)
Dim n As String
n = StrReverse(num3.Substring(3, 3))
If m = n Then palindrome.Add(num3 & "." & m.ToString)
End If
Next
End If
Next
stpw.Stop()
palindrome.Sort()
palindrome.Reverse()
Debug.WriteLine(palindrome(0))
Stop
Re: Finding a palindromic number
Thank you I will change the code to use that.
Also I know they are terrible variable names and I usually use self-identifying names but for programs this size I tend not to bother.
i3 was originally an integer but then I changed it to a string because as I said earlier, I couldn't work out how to go through an integer digit by digit and I didn't bother changing it from i3 after.
Re: Finding a palindromic number
refined:
Code:
Dim palindrome As New List(Of String)
Dim stpw As New Stopwatch
stpw.Reset()
For num1 As Long = 990 To 110 Step -11
If num1 Mod 11 = 0 Then
For num2 As Long = 999 To 101 Step -1
Dim num3 As Long = num2 * num1
If num3 >= 101101 Then
Dim s As String = num3.ToString
Dim m As String = s.Substring(0, 3)
Dim n As String = s.Substring(3, 3)
If m = StrReverse(n) Then palindrome.Add(num3 & "." & num1.ToString & "." & num2.ToString)
Else
Exit For
End If
Next
End If
Next
palindrome.Sort()
palindrome.Reverse()
stpw.Stop()
Debug.WriteLine(stpw.ElapsedTicks.ToString)
Debug.WriteLine(palindrome(0))
Stop