Soldato
- Joined
- 15 Feb 2011
- Posts
- 10,234
- Location
- Slough
Im not bothered about running time, but I want it to be generic so that I can pass the algorithm four parameters.
- the minimum number
- the maximum number
- set min length
- set max length
ok, its really not pretty, but heres a modification of my idea. i hope you're familiar with C because it was easier to write it in a style similar to C
Code:
MinSetSize //the smallenst number of numbers in your combinations, 5 in your example
MaxSetSize //as above, but maximum, 10 in your example
CurrSetSize //the set size the program is looking for at the time
MinNumber //the lowest number allowed in the set, 0 in your example
MaxNumber //as above, but highest, 49 in your example
for(CurrSetSize = MinSetSize; CurrSetSize <=MaxSetSize; CurrSetSiz++)
{
for(a = MinNumber; a <= MaxNumber; a++)
{
if(CurrSetSize = 1)
{
if(set is valid) //see earlier post
{
store set
}
}
if(CurrSetSize > 1)
{
for(b = MinNumber; b <= MaxNumber; b++)
{
if(CurrSetSize = 2)
{
if(set is valid) //see earlier post
{
store set
}
}
if(CurrSetSize > 2)
{
etc, etc
}
}
}
}
}
the limit of the biggest set you can search for will be the number of
nested for loops you've put in. because i havent copied and pasted
much you;d only get sets of 1 or 2 from my program, but theoretically
you could copy paste this as much as you like and get massive sets 9
and kill your computer)
*edit*
what i've done is got the program to iterate through all the combinations for the minimum set size, then the minimum+1 set size, then minimum+2, all the way up to the maximum with the first for loop, and the if statements make sure it stops iterating through variables that arent in the set (eg, they stop the program for iterating through the 11th number combinations when you want to look through 10 number combinations). when it reaches the last number the if statements will store the combination if it is valid
there must be a way of doing this with recursive functions that wont involve 982736459623948562345 nested for loops, but i'm only a beginner at C/C++/C# so i wouldnt be sure of how to do it
i hope this helps though
Last edited: