Solved

# code to help with maths problem

Posted on 2006-05-18
168 Views
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.

0
Question by:ajdunne

LVL 44

Expert Comment

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

AW
0

Author Comment

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

LVL 18

Expert Comment

Option Explicit

'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

LVL 18

Expert Comment

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

LVL 18

Expert Comment

I feel my posting was a correct answer.
0

Author Comment

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

LVL 18

Expert Comment

sorry missed the comment about access
0

Accepted Solution

PAQed with points refunded (125)

GranMod
Community Support Moderator
0

## Featured Post

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…