Algorithm help!!!

BlueJay924

Well-known member
Joined
May 6, 2003
Messages
46
is there an algorithm for 2^n-1? im 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
 
You dont need a complex algorithm to calculate (2^n)-1.

Having said that, I conclude that I didnt 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 dont know any named algorithm for that.
Basically its 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.
 
your bit about it being 1/0s is right but let me go a litte more into what im 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.
 
You could have 4 loops, each embedded in another, using a if statement to only write them, if they are not the same.
 
No. That would limit the array size to 4.

Are you familiar with recursive algorithms ?

Code:
 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
 
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.
 
Back
Top