Find all numbers of two or more digits in decimal, that are reversed when converted to hexadecimal.
These numbers have something in common. Explain why this is.
What is being asked is equivalent to saying that if one reverses the digits of a decimal number and treats the results as a hex number, is the number so obtained equal to the original number.
The numbers must be limited to 5 digits or less, as the smallest 6-digit hex number, 100000 hex = 16^5 = 1048576, is a 7-digit decimal number.
The program finds these, in addition to decimals with trailing zeros that lead to hex numbers with leading zeros:
dec hex
53 35
371 173
5141 1415
99481 18499
8520280 0820258
DefDbl A-Z
Dim crlf$
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
For i = 2 To 15
d = 10 ^ i: x = 16 ^ i
Text1.Text = Text1.Text & i & Str(d) & Str(x) & crlf
Next
Text1.Text = Text1.Text & crlf & " done" & crlf
For n = 10 To 9999999
ns$ = LTrim(Str(n))
If Left(ns, 1) >= Right(ns, 1) Then
hx = 0
For digpos = Len(ns) To 1 Step -1
hx = hx * 16 + Val(Mid(ns, digpos, 1))
Next
If n = hx Then
hxs$=""
for i=1 to len(ns)
hxs$=mid(ns,i,1)+hxs
next
Text1.Text = Text1.Text & n & " " & hxs & crlf
End If
End If
DoEvents
Next
Text1.Text = Text1.Text & crlf & " done" & crlf
End Sub
I would imagine that the 7-digit limit could be waived if allowing for leading zeros on the hexadecimal side resulting from trailing zeros in the decimal number, as in the last number found above.last number found above.
|
Posted by Charlie
on 2017-08-02 14:16:35 |