My car has a 5-digit odometer, which measures the miles since the car was built, and a 3-digit trip meter, which measures the miles since I last set it. Every so often, one or both of the readings is a palindrome. The meters reset to 000 after 999 and to 00000 after 99999.
The current readings are 123 and 12345. Assuming that I do not reset the trip meter, when is the next time both readings will be palindromes?
When was the most recent time both readings were palindromes?
Prove no matter what the mileage and trip meters read, they can eventually be made to both be palindromes without resetting the trip meter.
Note: A palindrome reads the same forwards and backwards, like 262 or 37173.
The palindrome function requires a number in string format and a desired length for the string. Visual Basic handles string kinda funny so I had to stick another loop in ther to strip the leading spaces off the front.
Function palindrome(inputstring As String, length As Integer) As Boolean
Dim i, IntStart, IntEnd As Integer
Dim newstring As String
IntEnd = Len(inputstring)
For i = 1 To IntEnd
If Mid$(inputstring, i, 1) >= "0" And Mid(inputstring, i, 1) <= "9" Then
newstring = newstring + Mid$(inputstring, i, 1)
End If
Next
inputstring = newstring
Do While Len(inputstring) < length
inputstring = "0" + inputstring
Loop
IntStart = 1
IntEnd = Len(inputstring)
Do While IntStart < IntEnd
If Mid$(inputstring, IntStart, 1) <> Mid$(inputstring, IntEnd, 1) Then
palindrome = False
Exit Function
End If
IntStart = IntStart + 1
IntEnd = IntEnd - 1
Loop
palindrome = True
End Function
The calling program follows. The input comes from two textboxes called text1 (for the trip odometer) and text2 (for the other odometer).
Private Sub Command1_Click()
Dim trip, odometer As Long
trip = Val(Text1.Text)
odometer = Val(Text2.Text)
trip = trip + 1
odometer = odometer + 1
Do While (Not (palindrome(Str(trip), 3) And palindrome(Str(odometer), 5)))
trip = (trip + 1) Mod 1000
odometer = (odometer + 1) Mod 100000
Text1.Text = Str(trip)
Text2.Text = Str(odometer)
Form1.Refresh
DoEvents
Loop
End Sub
I post these in case someone wants to find more palindromes themselves and doesn't want to type in all the code themselves.
|
Posted by Erik O.
on 2004-06-16 09:48:43 |