|
-
Nov 16th, 2012, 03:13 PM
#1
Thread Starter
Lively Member
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
-
Nov 16th, 2012, 03:36 PM
#2
Re: Finding a palindromic number
if i3.Length = 5 Then there ain't no such thing as i3(5)!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Nov 16th, 2012, 03:58 PM
#3
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.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Nov 16th, 2012, 04:09 PM
#4
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
Last edited by dbasnett; Nov 16th, 2012 at 04:23 PM.
-
Nov 16th, 2012, 04:09 PM
#5
Thread Starter
Lively Member
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.
-
Nov 16th, 2012, 04:48 PM
#6
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|