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:-
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.
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.