Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to list all combinations.
Message
From
08/05/2008 03:50:11
 
 
To
07/05/2008 16:22:11
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01315740
Message ID:
01315792
Views:
16
This message has been marked as the solution to the initial question of the thread.
Hi John.

If you want unique combinations (so 1,4 is the same as 4,1 so only one should be listed) you can use something like the following code. Rather that use recursion it writes the code that you would write by hand. Just replace the array with your own data and set lnNumToSelect appropriately.
Clear

Local laNums[5], i

laNums[1] = 1
laNums[2] = 2
laNums[3] = 3
laNums[4] = 4
laNums[5] = 5

lnTotalEntries = alen(laNums)
lnNumToSelect = 2

lcCode = "lpara taNums"+ Chr(13)+Chr(13)+ "Local "
For i = 1 to lnNumToSelect
	lcCode = lcCode + Iif(i=1, "", ", ") + "lnLoop"+ Transform(i)
EndFor
lcCode = lcCode + Chr(13)+Chr(13)

For i = 1 to lnNumToSelect
	lcCode = lcCode + Replicate(Chr(9), i-1) + "For lnLoop"+ Transform(i) + ;
			" = "+ Iif(i == 1, "1", "lnLoop"+ Transform(i - 1) +" + 1") +" to "+ ;
			Transform(lnTotalEntries) +Chr(13)
EndFor

lcCode = lcCode + Replicate(Chr(9), lnNumToSelect) +"?"
For i = 1 to lnNumToSelect
	lcCode = lcCode + Iif(i = 1, "", ", ") + "taNums[lnLoop"+ Transform(i) +"]"
EndFor
lcCode = lcCode + Chr(13)

For i = 1 to lnNumToSelect
	lcCode = lcCode + Replicate(Chr(9), lnNumToSelect - i) + "EndFor"+ Chr(13)
EndFor

ExecScript(lcCode, @laNums)
Ian Simcock.

>I'm trying to write a formula to work out all the possible combinations of say 2 numbers from a group of 30 numbers ranging from (1 - 50). This formula would need to also work with combinations of 3 or 4 or even 5 numbers as well. The group of numbers may increase from 30 or decrease. I know how to calculate the actual number of combinations I just can't figure how to write the code to list all the combinations. Any help would be greatly appreciated.
>
>An example of the group of 30 numbers... 1,4,6,9,10,11,14,15,18,19,20,21,22,24,27,29,30,31,33,34,35,38,39,41,42,43,45,47,48,50
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform