BlueJay924 Posted March 2, 2004 Posted March 2, 2004 is there an algorithm for 2^n-1? i'm trying to get all the possibilities from an array and store each combination into an arraylist. is there a name for this algorithm? help would be appreciated Quote
Heiko Posted March 2, 2004 Posted March 2, 2004 You don't need a complex algorithm to calculate (2^n)-1. Having said that, I conclude that I didn't fully understand your problem :) You have an array of length n. Each array element can either be 0 or 1 ? And you want to transform this into another array with 2^n elements, each element containing the unique (0/1) combination ? I don't know any named algorithm for that. Basically it's a bit-array to word conversion. So you could just fill your second array with all numbers from zero to n and interpret them as bitarrays. Quote .nerd
BlueJay924 Posted March 2, 2004 Author Posted March 2, 2004 your bit about it being 1/0s is right but let me go a litte more into what i'm trying to do supp i have my array a : 1, 2, 3, 4 i want to be able to get all the possible combinations (the array size may vary) result would be 1 12 13 14 123 124 134 1234 2 23 24 234 3 34 4 but is there a an algorithm that will give me this combination because if there is my brain just does not want to see it. Quote
Jay1b Posted March 2, 2004 Posted March 2, 2004 You could have 4 loops, each embedded in another, using a if statement to only write them, if they are not the same. Quote
Heiko Posted March 2, 2004 Posted March 2, 2004 No. That would limit the array size to 4. Are you familiar with recursive algorithms ? ' P S E U D O C O D E ' Sub Pick (ByVal pool as array, byval pickstring as string) If pool.length = 0 then Debug.writeline (pickstring) else for each elem in pool Pick (New array (old pool minus elem), pickstring & elem) next elem end if next Quote .nerd
Jay1b Posted March 2, 2004 Posted March 2, 2004 He asked for 4 loops :) Ok, have a loop held in a class, then within that loop have it call the class for x amount of times. Not at all complex. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.