Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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
Posted

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.

.nerd
Posted

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.

Posted

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


.nerd
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...