Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default recursive or loopless combinations

Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default recursive or loopless combinations

Try this code

Public InStrings(5)
Public Levels
Public combo
Public RowCount
Sub combinations()

InStrings(0) = Array(1, 2, 3)
InStrings(1) = Array(4, 5, 6, 7)
InStrings(2) = Array(8, 9, 10)
InStrings(3) = Array(11, 12, 13, 14)
InStrings(4) = Array(15, 16, 17, 18)

Levels = UBound(InStrings)
Level = 1
RowCount = 1
ReDim combo(UBound(InStrings))

Call recursive(Level)
End Sub
Sub recursive(ByVal Level As Integer)

Length = UBound(InStrings(Level - 1)) + 1

For i = 0 To (Length - 1)

combo(Level - 1) = InStrings(Level - 1)(i)
If Level = Levels Then
For j = 0 To (Levels - 1)
If j = 0 Then
ComboString = combo(j)
Else
ComboString = ComboString & "," & combo(j)
End If
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1)
End If

Next i
End Sub



"Mazant, K." wrote:

Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default recursive or loopless combinations

Joel wrote:
Try this code

Public InStrings(5)
Public Levels
Public combo
Public RowCount
Sub combinations()

InStrings(0) = Array(1, 2, 3)
InStrings(1) = Array(4, 5, 6, 7)
InStrings(2) = Array(8, 9, 10)
InStrings(3) = Array(11, 12, 13, 14)
InStrings(4) = Array(15, 16, 17, 18)

Levels = UBound(InStrings)
Level = 1
RowCount = 1
ReDim combo(UBound(InStrings))

Call recursive(Level)
End Sub
Sub recursive(ByVal Level As Integer)

Length = UBound(InStrings(Level - 1)) + 1

For i = 0 To (Length - 1)

combo(Level - 1) = InStrings(Level - 1)(i)
If Level = Levels Then
For j = 0 To (Levels - 1)
If j = 0 Then
ComboString = combo(j)
Else
ComboString = ComboString & "," & combo(j)
End If
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1)
End If

Next i
End Sub



"Mazant, K." wrote:

Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,




Thanks Joel.

Regards,
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a recursive problem? davegb Excel Programming 2 August 16th 06 07:53 PM
Recursive Code Myles[_68_] Excel Programming 1 August 13th 06 02:11 PM
Help with Recursive Call? mark Excel Programming 9 May 25th 06 08:44 PM
Recursive Functio help BigBobbo Excel Worksheet Functions 1 May 10th 06 07:23 PM
recursive sums Joe Excel Worksheet Functions 6 July 17th 05 09:45 AM


All times are GMT +1. The time now is 02:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"