5 Replies - 1778 Views - Last Post: 01 March 2010 - 01:49 PM Rate Topic: -----

#1 mickey11030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-February 10

Find a 5 digit number that reverses when divided by a single digit

Posted 25 February 2010 - 09:37 AM

A friend sent me this problem: Find a 5 digit number which reverses itself when divided by a single digit (division by 1 not allowed). I worked on it for a few days, and when I didn't have any luck, I decided to write VB code to search for the number. The code is below. Unfortunately, I didn't find any such number. I've check my code a thoroughly as I can, and I think it's correct, but I can't be sure if I didn't find the number because no such number exists or because of an error in my code. I'd appreciate any comments.PS I've just previewed my post, and I see all the indentation has been removed. I'm sorry I don't know how to post and keep the helpful indentation.
PS I tried to upload the form, which contains the code, but I got a message that I was not allowed to upload that kind of file, so I attached a text version of the code.

[code]
Private Sub Command1_Click()
Dim Num As Long '5 digit number to be tested
Dim DivL As Long 'Long version of divisior
Dim Quot As Long 'Quotient = Num / Div
Dim m As Long 'Work variable
Dim Div As Integer 'Divisior
Dim j As Integer 'Counter
Dim d(1 To 5) As Integer 'Array to hold digits of Num
Dim n(1 To 5) As Integer 'Array to hold digits of Quot

Dim Match As String 'Variable to signal solution was found
Dim Ans As String 'Dummy variable

For Div = 9 To 2 Step -1
DivL = Div 'We need a long version of Div for next stm.
'Start searching at 10,000 * Div. Smaller numbers give 4 digit quotient
'Go in steps of Div, because we want exact division
For Num = DivL * 10000 To 99999 Step Div
'Code to follow progress of calcualtion
If Num Mod 1000 = 0 Then
Debug.Print "Num is", Num
End If
'Store digits of Num in d(1 to 5)
m = Num
For j = 1 To 5
d(6 - j) = m Mod 10
m = m \ 10
Next j
'Divide Num by DivL
Quot = Num \ DivL
'Now store digits of Quot in n(1 to 5)
m = Quot
For j = 1 To 5
n(6 - j) = m Mod 10
m = m \ 10
Next j
'Check if Quot is the reverse of Num
Match = "Yes"
For j = 1 To 3
If d(j) <> n(j) Then
Match = "No"
Exit For
End If
Next j
If Match = "Yes" Then
Debug.Print "Houray", Num, Div
Exit For
End If
Next Num
If Match = "Yes" Then
Ans = InputBox("We Found Solution")
Exit For
End If
Debug.Print "We are finished with", Div
Next Div
End Sub
[\code]

Attached File(s)


This post has been edited by mickey11030: 25 February 2010 - 10:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Find a 5 digit number that reverses when divided by a single digit

#2 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Find a 5 digit number that reverses when divided by a single digit

Posted 25 February 2010 - 01:34 PM

it's simple
    For num = 10000 To 99999 Step 1
        For i = 1 To 9
            If num = StrReverse(CStr(num / i)) Then
               List1.AddItem num & " : " & i
            End If
        Next i
    Next


Was This Post Helpful? 1
  • +
  • -

#3 mickey11030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-February 10

Re: Find a 5 digit number that reverses when divided by a single digit

Posted 25 February 2010 - 02:05 PM

Thank you but when I incorporated your code into a new VB project, I got a "Sub or Function not defined" error message on StrReverse. I should say that I'm using VB 4, so maybe StrReverse was added in a later version of VB.

I should point out that division by 1 is not allowed. So the For statement for the divisor should run from 2 to 9.
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Find a 5 digit number that reverses when divided by a single digit

Posted 25 February 2010 - 03:37 PM

well then reverse the string on your own do you have Mid$() function. it will be something like so:
Private Sub Command1_Click()
    Debug.Print str_reverse("123")
End Sub

Public Function str_reverse(ByRef strNumber As String) As String
    Dim i As Integer
    For i = Len(strNumber) To 1 Step -1
        str_reverse = str_reverse & Mid$(strNumber, i, 1)
    Next i
End Function




good luck :)
Was This Post Helpful? 0
  • +
  • -

#5 mickey11030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-February 10

Re: Find a 5 digit number that reverses when divided by a single digit

Posted 26 February 2010 - 02:10 PM

I checked my code one last time, and found a subscript error. The program now generates two solutions:
87912, with a divisor of 4, and 98901, with a divisor of 9.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#6 mickey11030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-February 10

Re: Find a 5 digit number that reverses when divided by a single digit

Posted 01 March 2010 - 01:49 PM

After I got my code to work, I implemented your suggestions. I figured the problem involved arithmetic, so I isolated each digit of the number and of the quotient by successive divisions by 10. Then I had to compare the digits one by one. Your suggestion of using stings was much better way to go - it reduced the computational complexity by a factor of 10. And I learned a useful lesson for the future.

Thanks again.
Michael Trombetta
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1