Project Euler Palindrome problem

Spekk

Member
Joined
Jun 8, 2010
Messages
7
Programming Experience
Beginner
Hi, I've found a site that requires programmed solutions to mathematical problems and if possible would like a bit of feedback on my code to one of the problems.

Project Euler ... in case anyone's interested.

Problem 4 is:
"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."

code:-

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim x As Integer
        Dim y As Integer
        Dim result As Long
        Dim Pal_test As Long
        

        Dim str_rev As String
        Dim str_holder As String



       

        For x = 999 To 100 Step -1

            For y = 999 To 100 Step -1

                result = x * y ' product of two three digit numbers
                str_rev = Str(result) ' convert result to string
                str_holder = ReverseStringOrder(str_rev) 'reverse the number
                Pal_test = Val(str_holder) ' set it back to long

                If result = Pal_test And result > 100000 Then 'If the numbers match  and the number is very large add to the list box
                    Pal_list.Items.Add(result)
                End If
                'store the palindrome and when the next palindrome is found
                'test whether it is higher than the last one. ???


            Next y

        Next x
    End Sub

    Public Function ReverseStringOrder(ByVal x As String)




        Dim strReversedText As String

        Dim arrChar() As Char = x.ToCharArray()
        Array.Reverse(arrChar)
        strReversedText = arrChar


        Return strReversedText


    End Function


    


End Class

Sure you get the largest palindrome possible, but it displays all the other palindromes as it works thru the two 'for' loops. How is possible to re-write the code so that the output is a messagebox showing the highest possible palindrome?

Thx in advance.
 
I did this a long while back... I think I did it in as3 or maybe even pick basic... not sure.

Anyways here's a quick result I put together, kind of similar to yours:

VB.NET:
Public Class Form1
    Public Sub New()
        Console.WriteLine(LargestPalindrome(100, 999))
    End Sub

    Private Function LargestPalindrome(ByVal min As Integer, ByVal max As Integer) As Integer
        Dim result As Integer = 0
        Dim temp As Integer

        For i As Integer = min To max
            ''why start j at i? because all the numbers before have gone through their reversed order
            ''i.e. 100 * 999 == 999 * 100
            For j As Integer = i To max

                temp = i * j

                If temp > result AndAlso IsPalindrome(temp) Then
                    result = temp
                End If
            Next
        Next

        Return result
    End Function

    Private Function IsPalindrome(ByVal value As Integer)
        Dim str As String = value.ToString()
        Return (str = New String(str.Reverse().ToArray()))

    End Function
End Class


note my comment in mine though... might as well reduce the number of iterations, especially when considering it only takes 1 character of code changed to cut our number of loops by like 39%



[edit]

I will say, it is a very fun site... I don't do the problems as much as I'd hope. But whenever I'm kind of bored I do some random problem... they get really tough as you move forward.
 
Last edited:
You can for example use a SortedList(Of Integer, Point), where you keep the product as key and x/y values as Point instances. The largest product will be the last item in list. For example:
VB.NET:
Dim pals As New SortedList(Of Integer, Point)
For x = 999 To 100 Step -1
    For y = x To 100 Step -1
        Dim prod = x * y
        Dim s = prod.ToString
        If s = New String(s.Reverse.ToArray) Then
            pals(prod) = New Point(x, y)
        End If
    Next
Next
Dim p = pals.Last
MessageBox.Show(String.Format("largest palindrome is {0} by x {1} and y {2}", p.Key, p.Value.X, p.Value.Y))
edit: previous posters idea of only keeping the highest ranking palindrome (+ corresponding x/y) is better memorywise. Though if you want to list all by order of product this can be done.
 
Back
Top