'Combinational' Code Required

1. Senior Member
Join Date
Dec 1969
Posts
172

## 'Combinational' Code Required

Hi!<BR><BR>In my current ASP project, I have a task of generating all the possible combinations of some supplied strings e.g If ABC is given, I must have<BR><BR>A<BR>B<BR>C<BR>AB<BR>BC<BR>AC<BR><BR>S ome help in code regarding this matter. I&#039;ll be supplying a much larger string actually e.g 30 digitd or 50 digits. But time is not a factor for me.<BR><BR>Any URLs, Any Help Articles or code samples are most welcome.<BR><BR>Thanx in anticipation.

2. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

## Heres one I prepared earlier...

Hope it works with letters...<BR><BR>&#060;% <BR>&#039; This array contains the numbers you wish to get the combinations of: <BR>NumAry = Array("A","B","C") <BR><BR>For i=1 To 2^(UBound(NumAry)+1)-1 <BR> For ix=0 To Ubound(NumAry) <BR> BinaryValue = 2^ix <BR> If i AND BinaryValue Then <BR> AddString = AddString & NumAry(ix) & "+" <BR> End If <BR> Next <BR> Response.write Left(AddString,Len(AddString)-1) & "&#060; br&#062;" <BR> AddString = "" <BR>Next <BR>%&#062;

3. Senior Member
Join Date
Dec 1969
Posts
1,843

## RE: 'Combinational' Code Required

1) This isn&#039;t easy<BR>2) You have to have a sound understanding of mathematics. You have to know, for instance, that 3 letters have 15 possible combinations, not the 6 you listed above. <BR><BR>3) Every time someone has needed help with this, their end-result could always be accomplished an easier way. This is hardly ever needed. *PLEASE* share what you&#039;re actually trying to accomplish. <BR><BR>Shane

4. Senior Member
Join Date
Dec 1969
Posts
1,843

## Nope

Your code returns:<BR><BR>A<BR>B<BR>A+B<BR>C<BR>A+C<BR>B+C<B R>A+B+C<BR><BR>The possible combinations for A, B, C are:<BR><BR>A<BR>AB<BR>ABC<BR>AC<BR>ACB<BR>B<BR>BA <BR>BAC<BR>BC<BR>BCA<BR>C<BR>CA<BR>CAB<BR>CB<BR>CB A

5. Senior Member
Join Date
Dec 1969
Posts
1,843

## But I can see..

that it does work for numbers...<BR><BR>1+2 is the same as 2+1 ... but you can&#039;t use that logic for letters.

6. Senior Member
Join Date
Dec 1969
Posts
172

## RE: Nope

Yes U R right actually I forgot to right etc etc etc at the end of the six combinations I made..<BR><BR><BR>:))

7. Senior Member
Join Date
Dec 1969
Posts
172

## RE: 'Combinational' Code Required

My application is a Mathematics based demo for children in which I want to explain how combinations are made.<BR><BR>About my maths skills don&#039;t worry, I am a Graduate.<BR><BR>

8. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

You&#039;d need (another!) loop outside the current ones which shifted all the characters in the array to the right on each iteration until each was done.<BR><BR>ary = ("a","b","c")<BR>&#039; loop existing code<BR><BR>ary = ("c","a","b")<BR>&#039; loop existing code<BR><BR>ary = ("b","c","a")<BR>&#039; loop existing code<BR><BR>Hmm, would that work ? need to think about it a bit more maybe.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
1,843

## You ****DONT**** Want to do this

If you are a "graduate" then you understand there must be a better way. <BR><BR>If you seriously want all the combinations of a 30 digit number, prepare to wait a long time for the results and understand that you&#039;ll probably crash the browser. <BR><BR>Even if you only want all the possible 30 digit combinations made from that number, you&#039;re looking for 30! Which is <BR><BR>2.6525285981219105863630848e+32<BR><BR>I don&#039;t think that would much help to the children. Plus, if you want to include combinations of lesser length (combinations between 1 and 29 letters long) then you&#039;re talking about millions more. <BR><BR>Like I said, you don&#039;t want to do this.

10. Senior Member
Join Date
Dec 1969
Posts
172