[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

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.
0
ajdunne
Asked:
ajdunne
1 Solution
 
Arthur_WoodCommented:
will you always have 3 digits?  and will they always be different (Digit1 <> Digit2 <> Digit3)?

AW
0
 
ajdunneAuthor Commented:
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
0
 
deightonCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
deightonCommented:
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



0
 
deightonCommented:
I feel my posting was a correct answer.
0
 
ajdunneAuthor Commented:
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
0
 
deightonCommented:
sorry missed the comment about access
0
 
GranModCommented:
PAQed with points refunded (125)

GranMod
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now