We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › combination finder - optimisations
Page Index Toggle Pages: 1
combination finder - optimisations? (Read 1383 times)
combination finder - optimisations?
Nov 24th, 2005, 11:49pm
 
I've written a snippet to find all the possible combinations of a given system. I wrote it so I could test out the Wolfram automata system with more than two colours. I needed a way to figure out what combinations I would need to apply rules for growth.

I figured out that the standard black and white rules were simply the progression of the binary number system. Therefore: I simply need to model a number system of a given base and a number of digits.

It works but I'm wondering if I could have done it simpler - I tend to miss these things and do it the hard way:
Code:

int digits = 3; // the amount of slots
int base = 3; // the quantity of numbers per slot
int [][] combo;
void setup(){
combo = combinations(digits, base);
for(int i = 0; i < combo.length; i++){
for(int j = 0; j < combo[i].length; j++)
print(combo[i][j]+":");
print("\n");
}
println("combinations:"+combo.length);
}
int [][] combinations(int digits, int base){
//returns all possible combinations
//format: [combination index][digit]
int [] digit = new int[digits];
for(int i = 0; i < digit.length; i++)
digit[i] = 0;
combo = new int[int(pow(base, digits))][digits];
for(int i = 0; i < combo.length; i++){
for(int j = 0; j < combo[i].length; j++){
combo[i][j] = digit[j];
}
digit[digit.length-1]++;
for(int k = digit.length-1; k > 0; k--)
if(digit[k] >= base){
digit[k] = 0;
digit[k-1]++;
}
}
return combo;
}
Page Index Toggle Pages: 1