Link to home
Start Free TrialLog in
Avatar of ajdunne
ajdunneFlag for Australia

asked on

code to help with maths problem

Hi experts,
I have a tricky problem that I don't know how to attack:

I need a routine that creates all the possible numbers from a set of individual digits.
For example, suppose you have the digits 1, 3 and 5.
Possible numbers are:
135
153
315
351
513
531
You can only use each digit once per number.

I'm not sure how to start - if you can point me in the right direction, I would be grateful.
I am comfortable using VB in MS Access - my preferred environment.

Thanks in advance.
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

will you always have 3 digits?  and will they always be different (Digit1 <> Digit2 <> Digit3)?

AW
Avatar of ajdunne

ASKER

Ideally, I want a solution that I can extend to 3, 4, 5 or 6 digits, but each case is treated seperately, i.e.
case 1 is always 3 digits
case 2 is always 4 digits, etc

In all cases, the digits are unique:  Digit1 <> Digit2 <> Digit3 [<> Digit4...]

Thanks
Option Explicit

Private Sub Form_Load()

'here 4 digits, increase array to how many digits you want

Dim digits(1 To 4) As String
digits(1) = "3"
digits(2) = "1"
digits(3) = "3"
digits(4) = "4"

Dim out() As String

digitizer digits, out

Dim i As Integer
For i = 1 To UBound(out)

    Debug.Print i, out(i)

Next



End Sub

Private Sub digitizer(digits() As String, numbers() As String)
   
    Dim digits2() As String
   
    Dim numbers2() As String
    Dim numbers3() As String
   
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim l As Long
   
    Dim ni As Long
   
    Dim sPivot As String
   
    Dim sNew As String
   
    If UBound(digits) = 1 Then
   
        ReDim numbers(1)
        numbers(1) = digits(1)
       
    Else
   
        ReDim digits2(1 To UBound(digits) - 1)
        ReDim numbers3(0 To 0)
   
        For i = 1 To UBound(digits)
            k = 0
            For j = 1 To UBound(digits)
                If i <> j Then
                    k = k + 1
                    digits2(k) = digits(j)
                End If
            Next
           
            sPivot = digits(i)
           
            digitizer digits2, numbers2
           
            For l = 1 To UBound(numbers2)
                For j = 0 To UBound(digits2)
                   
                    sNew = Left(numbers2(l), j) + sPivot + Mid(numbers2(l), j + 1)
                    ni = UBound(numbers3) + 1
                    ReDim Preserve numbers3(ni)
                    numbers3(ni) = sNew
                   
                Next
            Next
           
        Next
       
        Dim bSwap As Boolean
        Dim sSwap As String
       
        Do
            bSwap = False
            For j = 1 To UBound(numbers3) - 1
           
                If numbers3(j) > numbers3(j + 1) Then
               
                    sSwap = numbers3(j)
                    numbers3(j) = numbers3(j + 1)
                    numbers3(j + 1) = sSwap
                    bSwap = True
                End If
           
            Next
           
        Loop Until Not bSwap
        k = 0
        For j = 1 To UBound(numbers3)
            If numbers3(j) <> numbers3(j - 1) Then
                k = k + 1
                ReDim Preserve numbers(k)
                numbers(k) = numbers3(j)
            End If
        Next
       
       
    End If
       


End Sub
if you run it with the setup below, you should get 120 combinations, which is 6!/3!=720/6=120



Dim digits(1 To 6) As String
digits(1) = "1"
digits(2) = "3"
digits(3) = "5"
digits(4) = "8"
digits(5) = "8"
digits(6) = "8"



Dim out() As String

digitizer digits, out

Dim i As Integer
For i = 1 To UBound(out)

    Debug.Print i, out(i)

Next



I feel my posting was a correct answer.
Avatar of ajdunne

ASKER

Hi deighton,
Thanks for your feedback on this, but I could not make your code work.
Sorry, probably my fault.

I did finally find an excel spreadsheet that contained the vba code I needed.
I found it at
http://www.geocities.com/SiliconValley/7043/ue.html

Experts, may I request a refund of these points.

Thanks,
ajdunne
sorry missed the comment about access
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial