Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Algorithms


ghost's Avatar
0 0

i cant make out the algorithm for all possible solustions for 'n' different numbers (or whatever) in a row. can anyone give me any tip (or the whole algorithm)?

example: if n=3 then:

  1. 1 2 3
  2. 1 3 2
  3. 2 1 3
  4. 2 3 1
  5. 3 1 2
  6. 3 2 1

ghost's Avatar
0 0

you want the combo's or the amount?


ghost's Avatar
0 0

i dont want to know how many are the possibilities (in my example=6), but an algorithm to output all those rows…


ghost's Avatar
0 0

Erm, I can't quiet get it to work. but, here, n=3 so two begin with each one, so if it were n=4 then you have 3 begining with each number then 2 with the same number next if you understand what i mean. and then one. so n=5 wxyz wxy wx w
if you get what i mean w, x, y, z being parts.


ghost's Avatar
0 0

Hi, Have you learnt permutations and combinations in Math? well, like with 1,2,3 you can make 6 combos. with 4 you can make 24. Is that what you mean? You can find it by finding n! (n factorial). It means 1x2x3x4x…….xn.

A simple loop would be:

$fact=1; for($i=1;$i<=$n;$i++) $fact*=$i;

At the end of that $fact will have the factorial of n. Is that what you wanted?

ThomasB)


ghost's Avatar
0 0

no man this not what i want, but thanks for tring to help! what i want is all the compinations of n continuing numbers (or letters, or whatever…). just the algorithm.


ghost's Avatar
0 0

thousandandone, try reading ll the posts before replying :)


ghost's Avatar
0 0

That was thomasantony.


ghost's Avatar
0 0

There are a few ways that you could solve this. I'd recommend using recursion to get the job done. Off the top of my head with no testing, so bear with me.

pseudo code:

    if (!n--) return false; //controls the end of recursion
    for each piece of data { //loop through each piece of data (1,2,3 in your example)
        foo=x.data_piece;  //create the possible solution such as 11,12,13,111,112, etc
        generate(data,foo,n); //generate the next set
    }
}

generate(array(1,2,3,4),&#39;&#39;,7); //generate data until length=7

HTH

ps. sorry, the code parser appears to be non existant on here