Home |
Search |
Today's Posts |
#1
|
|||
|
|||
Number anagram
I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all
the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
#2
|
|||
|
|||
Hi
Have a look at PERMUT(). It might be part of the analysis toolpak add-in. If it is, and you don't have it loaded, you will get an error. To load it, go to Tools|Addins . . . and select it from the list. Hope this helps. -- Andy. "pav55" wrote in message ... I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
#3
|
|||
|
|||
Logically, look at it this way :
When you select the first number you have 9 possibilities When you select the second number you have 8 possibilities left and so on Therefore... =9*8*7*6 or 3024 Here is a link with the formula you need. The excel function is FACTORIAL(). http://www.andrews.edu/~calkins/math...rod02.htm#PERM -- Carlos "pav55" wrote in message ... I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
#4
|
|||
|
|||
I do this type of operation fairly regularily
enter 1 thr 9 in cells A1 to A9 copy these cells and paste to A1:A81 copy A1:A81 and paste to B1:B81 Sort Column A by itself copy A1:B81 and paste to A1:B729 Copy B1:B729 and paste to C1:C729 Sort Column B by itself Copy A1:C729 and paste to A1:C6561 Copy C1:C6561 and paste to D1:D6561 Sort Column C by itself in Cell E1 enter =if(or(A1=B1,A1=C1,A1=D1,B1=C1,B1=D1,C1=D1),1,0) Copy E1 and paste to E1:E6561 Select Columns A:E and sort by column E Delete all of the Rows with a 1 in column E. (I would check that My 9 to the powers numbers are correct) There are, of course many ways to do this. By the time you get to about 6 combos, You run out of rows if you dont delete between steps in that Case I would use a macro similer to sub ser4() r=1 for J = 1 to 9 for j = 1 to 9 if j=i then goto 53 for k = 1 to 9 if k = i or k = j then goto 52 for m = 1 to 9 if m = i or m = j or m = k then goto 51 cells(r,1)=i:cells(r,2)=j:Cells(r,3)=k:cells(r,4)= m:r=r+1 51 next m 52 next k 53 next j next i end sub Please note this is what I call a brute force sub. It is what I would do for doing something simple once. If you are going to do something a bunch or if others will be running a sub or if the sub is complex. it is best to "dim" everything and use option explicit etc and put in comments to explain what you are doing. "pav55" wrote: I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
#5
|
|||
|
|||
I thought you said it pretty clearly you already knew about combin, permut,
and fact, but maybe not. Anyway, here is code written by Myrna Larson which will do what you want: (hope you know how to use a macro) Option Explicit Dim vAllItems As Variant Dim Buffer() As String Dim BufferPtr As Long Dim Results As Worksheet ' ' Posted by Myrna Larson ' July 25, 2000 ' Microsoft.Public.Excel.Misc ' Subject: Combin ' ' 'Since you asked, here it is. It is generic, i.e. it isn't written specifically 'for a given population and set size, as yours it. It will do permutations or 'combinations. It uses a recursive routine to generate the subsets, one routine 'for combinations, a different one for permutations. 'To use it, you put the letter C or P (for combinations or permutations) in a 'cell. The cell below that contains the number of items in a subset. The cells 'below are a list of the items that make up the population. They could be 'numbers, letters and symbols, or words, etc. 'You select the top cell, or the entire range and run the sub. The subsets are 'written to a new sheet in the workbook. ' ' Sub ListPermutations() Dim Rng As Range Dim PopSize As Integer Dim SetSize As Integer Dim Which As String Dim N As Double Const BufferSize As Long = 4096 Set Rng = Selection.Columns(1).Cells If Rng.Cells.Count = 1 Then Set Rng = Range(Rng, Rng.End(xlDown)) End If PopSize = Rng.Cells.Count - 2 If PopSize < 2 Then GoTo DataError SetSize = Rng.Cells(2).Value If SetSize PopSize Then GoTo DataError Which = UCase$(Rng.Cells(1).Value) Select Case Which Case "C" N = Application.WorksheetFunction.Combin(PopSize, SetSize) Case "P" N = Application.WorksheetFunction.Permut(PopSize, SetSize) Case Else GoTo DataError End Select If N Cells.Count Then GoTo DataError Application.ScreenUpdating = False Set Results = Worksheets.Add vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value ReDim Buffer(1 To BufferSize) As String BufferPtr = 0 If Which = "C" Then AddCombination PopSize, SetSize Else AddPermutation PopSize, SetSize End If vAllItems = 0 Application.ScreenUpdating = True Exit Sub DataError: If N = 0 Then Which = "Enter your data in a vertical range of at least 4 cells. " _ & String$(2, 10) _ & "Top cell must contain the letter C or P, 2nd cell is the number " _ & "of items in a subset, the cells below are the values from which " _ & "the subset is to be chosen." Else Which = "This requires " & Format$(N, "#,##0") & _ " cells, more than are available on the worksheet!" End If MsgBox Which, vbOKOnly, "DATA ERROR" Exit Sub End Sub Private Sub AddPermutation(Optional PopSize As Integer = 0, _ Optional SetSize As Integer = 0, _ Optional NextMember As Integer = 0) Static iPopSize As Integer Static iSetSize As Integer Static SetMembers() As Integer Static Used() As Integer Dim i As Integer If PopSize < 0 Then iPopSize = PopSize iSetSize = SetSize ReDim SetMembers(1 To iSetSize) As Integer ReDim Used(1 To iPopSize) As Integer NextMember = 1 End If For i = 1 To iPopSize If Used(i) = 0 Then SetMembers(NextMember) = i If NextMember < iSetSize Then Used(i) = True AddPermutation , , NextMember + 1 Used(i) = False Else SavePermutation SetMembers() End If End If Next i If NextMember = 1 Then SavePermutation SetMembers(), True Erase SetMembers Erase Used End If End Sub 'AddPermutation Private Sub AddCombination(Optional PopSize As Integer = 0, _ Optional SetSize As Integer = 0, _ Optional NextMember As Integer = 0, _ Optional NextItem As Integer = 0) Static iPopSize As Integer Static iSetSize As Integer Static SetMembers() As Integer Dim i As Integer If PopSize < 0 Then iPopSize = PopSize iSetSize = SetSize ReDim SetMembers(1 To iSetSize) As Integer NextMember = 1 NextItem = 1 End If For i = NextItem To iPopSize SetMembers(NextMember) = i If NextMember < iSetSize Then AddCombination , , NextMember + 1, i + 1 Else SavePermutation SetMembers() End If Next i If NextMember = 1 Then SavePermutation SetMembers(), True Erase SetMembers End If End Sub 'AddCombination Private Sub SavePermutation(ItemsChosen() As Integer, _ Optional FlushBuffer As Boolean = False) Dim i As Integer, sValue As String Static RowNum As Long, ColNum As Long If RowNum = 0 Then RowNum = 1 If ColNum = 0 Then ColNum = 1 If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then If BufferPtr 0 Then If (RowNum + BufferPtr - 1) Rows.Count Then RowNum = 1 ColNum = ColNum + 1 If ColNum 256 Then Exit Sub End If Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _ = Application.WorksheetFunction.Transpose(Buffer()) RowNum = RowNum + BufferPtr End If BufferPtr = 0 If FlushBuffer = True Then Erase Buffer RowNum = 0 ColNum = 0 Exit Sub Else ReDim Buffer(1 To UBound(Buffer)) End If End If 'construct the next set For i = 1 To UBound(ItemsChosen) sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1) Next i 'and save it in the buffer BufferPtr = BufferPtr + 1 Buffer(BufferPtr) = Mid$(sValue, 3) End Sub 'SavePermutation -- Regards, Tom Ogilvy "pav55" wrote in message ... I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
#6
|
|||
|
|||
Thanks for the responses.
Perhaps, I should have been a bit more clear. I'm a student and only recently started to use Excel properly so know only basic ( i mean ReallY basic functions and macros!) So the macros above, as amazing as they look..I have no idea how to implement them. how do you implement them pls? thanks. "Tom Ogilvy" wrote: I thought you said it pretty clearly you already knew about combin, permut, and fact, but maybe not. Anyway, here is code written by Myrna Larson which will do what you want: (hope you know how to use a macro) Option Explicit Dim vAllItems As Variant Dim Buffer() As String Dim BufferPtr As Long Dim Results As Worksheet ' ' Posted by Myrna Larson ' July 25, 2000 ' Microsoft.Public.Excel.Misc ' Subject: Combin ' ' 'Since you asked, here it is. It is generic, i.e. it isn't written specifically 'for a given population and set size, as yours it. It will do permutations or 'combinations. It uses a recursive routine to generate the subsets, one routine 'for combinations, a different one for permutations. 'To use it, you put the letter C or P (for combinations or permutations) in a 'cell. The cell below that contains the number of items in a subset. The cells 'below are a list of the items that make up the population. They could be 'numbers, letters and symbols, or words, etc. 'You select the top cell, or the entire range and run the sub. The subsets are 'written to a new sheet in the workbook. ' ' Sub ListPermutations() Dim Rng As Range Dim PopSize As Integer Dim SetSize As Integer Dim Which As String Dim N As Double Const BufferSize As Long = 4096 Set Rng = Selection.Columns(1).Cells If Rng.Cells.Count = 1 Then Set Rng = Range(Rng, Rng.End(xlDown)) End If PopSize = Rng.Cells.Count - 2 If PopSize < 2 Then GoTo DataError SetSize = Rng.Cells(2).Value If SetSize PopSize Then GoTo DataError Which = UCase$(Rng.Cells(1).Value) Select Case Which Case "C" N = Application.WorksheetFunction.Combin(PopSize, SetSize) Case "P" N = Application.WorksheetFunction.Permut(PopSize, SetSize) Case Else GoTo DataError End Select If N Cells.Count Then GoTo DataError Application.ScreenUpdating = False Set Results = Worksheets.Add vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value ReDim Buffer(1 To BufferSize) As String BufferPtr = 0 If Which = "C" Then AddCombination PopSize, SetSize Else AddPermutation PopSize, SetSize End If vAllItems = 0 Application.ScreenUpdating = True Exit Sub DataError: If N = 0 Then Which = "Enter your data in a vertical range of at least 4 cells. " _ & String$(2, 10) _ & "Top cell must contain the letter C or P, 2nd cell is the number " _ & "of items in a subset, the cells below are the values from which " _ & "the subset is to be chosen." Else Which = "This requires " & Format$(N, "#,##0") & _ " cells, more than are available on the worksheet!" End If MsgBox Which, vbOKOnly, "DATA ERROR" Exit Sub End Sub Private Sub AddPermutation(Optional PopSize As Integer = 0, _ Optional SetSize As Integer = 0, _ Optional NextMember As Integer = 0) Static iPopSize As Integer Static iSetSize As Integer Static SetMembers() As Integer Static Used() As Integer Dim i As Integer If PopSize < 0 Then iPopSize = PopSize iSetSize = SetSize ReDim SetMembers(1 To iSetSize) As Integer ReDim Used(1 To iPopSize) As Integer NextMember = 1 End If For i = 1 To iPopSize If Used(i) = 0 Then SetMembers(NextMember) = i If NextMember < iSetSize Then Used(i) = True AddPermutation , , NextMember + 1 Used(i) = False Else SavePermutation SetMembers() End If End If Next i If NextMember = 1 Then SavePermutation SetMembers(), True Erase SetMembers Erase Used End If End Sub 'AddPermutation Private Sub AddCombination(Optional PopSize As Integer = 0, _ Optional SetSize As Integer = 0, _ Optional NextMember As Integer = 0, _ Optional NextItem As Integer = 0) Static iPopSize As Integer Static iSetSize As Integer Static SetMembers() As Integer Dim i As Integer If PopSize < 0 Then iPopSize = PopSize iSetSize = SetSize ReDim SetMembers(1 To iSetSize) As Integer NextMember = 1 NextItem = 1 End If For i = NextItem To iPopSize SetMembers(NextMember) = i If NextMember < iSetSize Then AddCombination , , NextMember + 1, i + 1 Else SavePermutation SetMembers() End If Next i If NextMember = 1 Then SavePermutation SetMembers(), True Erase SetMembers End If End Sub 'AddCombination Private Sub SavePermutation(ItemsChosen() As Integer, _ Optional FlushBuffer As Boolean = False) Dim i As Integer, sValue As String Static RowNum As Long, ColNum As Long If RowNum = 0 Then RowNum = 1 If ColNum = 0 Then ColNum = 1 If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then If BufferPtr 0 Then If (RowNum + BufferPtr - 1) Rows.Count Then RowNum = 1 ColNum = ColNum + 1 If ColNum 256 Then Exit Sub End If Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _ = Application.WorksheetFunction.Transpose(Buffer()) RowNum = RowNum + BufferPtr End If BufferPtr = 0 If FlushBuffer = True Then Erase Buffer RowNum = 0 ColNum = 0 Exit Sub Else ReDim Buffer(1 To UBound(Buffer)) End If End If 'construct the next set For i = 1 To UBound(ItemsChosen) sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1) Next i 'and save it in the buffer BufferPtr = BufferPtr + 1 Buffer(BufferPtr) = Mid$(sValue, 3) End Sub 'SavePermutation -- Regards, Tom Ogilvy "pav55" wrote in message ... I have theses numbers:- 1, 2, 3, 4, 5, 6, 7, 8, 9 and I need to work out all the combinations these numbers have in sets of 4. By this I mean for example, 1234, 1236 etc....using each number once in a set..(so there are no duplicates) i.e. Not 1124. Is there any worksheet function/formula I could make in excel to work out and hence display all the possible four set combinations there are? But not the total of combinations i.e. just one number..because I already know how to calculate the number of combinations there can be. Or is there any alternatives other than writing them all out by hand?! Thanks. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Seed numbers for random number generation, uniform distribution | Excel Discussion (Misc queries) | |||
How to format a number in Indian style in Excel? | Excel Discussion (Misc queries) | |||
Defining a number in a cell by text then subtracting it by the tex | Excel Worksheet Functions | |||
multiply by actual number in cell | Excel Worksheet Functions | |||
GET.CELL | Excel Worksheet Functions |