Recently heard an interesting quiz and thought it will be fun to see the solution done programmatically. The quiz is as follows: A person bought an old car with over 170,000 miles on it. The odometer is a digital device with 6 digits. The odometer shows only full digits – no decimals. One day as the person was ready to leave home for work he noticed the odometer reading is a Palindrome (i.e. the digits are same when reading from front and back - like 175571). After driving about an hour (during this time he also stopped for a cup of coffee on the way) the person reached office. Surprisingly he noticed that the odometer reading is again a palindrome. The question is: How far is the home from the office?
I will admit I did not get the answer by programming – bur rather by trail and error and by intuition. I am trying to solve in Excel but have not finished. I have a good logic for COBOL – but do not have access to COBOL compiler. Will post the answer after hearing from expert readers. Thanks SbElectric
Software/Hardware used:
VB, Excel, C, COBOL
ASKED:
July 31, 2011 4:33 AM
UPDATED:
March 31, 2012 7:34 PM
Interesting
. Will surely look into it later.
This is very interesting, I’ve played with it too and it’s really above my logic, but I followed what I’ve seen here and was impressed. Very interesting. Thanks for the question and the logic.
Kudos to Tlsanders1 excellent analysis & programming logic.
Yes, the answer is 11.
I specially liked the way you set up the palindrome format – it is an interesting concept.
By the way, did you realize that the answer will be the same (11 miles) if the odometer is 5 digit or 4 digit? It will be 10 miles if it is a 3 digit device.
Here is sample of my pseudo code in COBOL.
01 Num Pic 9(6)
01 Digits redefines Num Pic X occurs 6 times.
Now I will set up a Do loop from I = 170000 to 99999 incrementing by 1.
Num = I
and then compare
If Digits (1) = Digits (6) and Digits (2) = Digits (5) and Digits (3) = Digits (4)
If True then we found first palindrome.
I will place this Do loop under another Loop (J=I + 1 to 99999 then compare the difference to be under 60..
Just like you have done – yours is more elegant!
I would like to see a solution via EXCEL. Any EXCEL experts taking up?
In Excel is it possible to refer to a subset of a number (like we do in COBOL with occurs clause) without VB programming?
Cordially SbElectric
Trivial solution in Excel (but ugly)
Fill one column (A) with values from 170 to 999 (e.g. A1 = 170, A2 = A1 + 1, etc.).
Fill next column (B) with the palindromic value (Tlsanders1′s expression works fine).
Fill next column (C) with B2-B1, B3-B2, etc. (fill down works good).
Filter on column C for values < 60.
You can also write the function in VisualBasic in Excel and return the first value found (or return a string containing the concatenated values found where the value is less than a specific threshold).
Of course, if the person lives in Germany and commutes on the Autobahn in their Bugatti Veyron (or similar supercar), any other starting value where the middle two digits are 99 will also yield a palindrome after only 110 miles… (grin)
I got a chuckle out of the autobahn additional solution.
Was trying to find a better way to solve it, but gave up after a few minutes.
Tlsanders1′s solution is probably the most efficient one.
Actually, the most efficient solution could take advantage of a little trick. If you think about the solution for a few minutes, you realize that only palindromes with “xy99yx” are possible starting solutions. Any other values will not propagate the change in the lower two digits to the upper two.
Therefore, the loop is really (vb pseudocode):
function Palindrome(byval i1 as int, byval i2 as int, byval i3 as int) as long
dim L as long
L = (i1*100000) + (i2*10000) + (i3*1000) + (i3*100) + (i2*10) + i1
Palindrome = L
end function
dim palindromestart as long
dim palindromeend as long
dim diff as long
For i = 17 to 98
palindromestart = palindrome(i / 10, i mod 10, 9)
palindromeend = palindrome((i+1)/10, (i+1) mod 10, 0)
diff = palindromeend – palindromestart
if diff < 120 — or some arbitrary value
print palindromestart, diff
end if
next i
i.e. you only need to evaluate 81 possible starting values.
I enjoyed Kccrosser’s both the analysis and comments. Yes, the 2nd minimum distance will be 110 miles and the 3rd minimum 1100.miles. So your comments on driving a Bugatti was very appropriate.
Did you hear about the “million dollar car crash” involving Bentley, Aston-Martin, Porsche 911, Mercedes S class, Ferrari? No… it was not on an autobahn but on an innocuous road in Monte Carlo!
Here is a link… enjoy
http://money.msn.com/saving-money-tips/post.aspx?post=64e05a5c-d295-4715-9a86-5046c25970a0
with best regards SbElectric
I assumed that the challenge was to find the next palindrome. This can be accomplished as follows:
Dim nextpalindrome, startnum, i As Long
Dim stringnum, firsthalf, lasthalf As String
Dim j As Integer
startnum = 175571
For i = startnum + 1 To startnum + 10000
stringnum = CStr(i)
j = Len(stringnum)
j = j / 2
If j + j < Len(stringnum) Then j = j + 1
firsthalf = Left(stringnum, j)
lasthalf = StrReverse(Right(stringnum, j))
If firsthalf = lasthalf Then Exit For
Next i
nextpalindrome = i – startnum
Debug.Print "nextpalindrome = "; nextpalindrome
The only problem: Since the answer is 1100 miles, how could this be the distance dexcribed in the question?
Modified code. Dim nextpalindrome, startnum, i As Long
Dim stringnum, firsthalf, lasthalf As String
Dim j As Integer
startnum = 12345
For i = startnum + 1 To startnum + 10000
stringnum = CStr(i)
j = Len(stringnum)
j = j / 2
If j + j < Len(stringnum) Then j = j + 1
firsthalf = Left(stringnum, j)
lasthalf = StrReverse(Right(stringnum, j))
If firsthalf = lasthalf Then Exit For
Next i
nextpalindrome = i
Debug.Print "nextpalindrome = "; nextpalindrome
Debug.Print "distance to next palindrome: = "; i – startnum
My previous solution gave the distance instead of the palindrome.