Author  Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
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


Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
you want the combo's or the amount?


Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
i dont want to know how many are the possibilities (in my example=6), but an algorithm to output all those rows...
Edited by on 310106 22:14 

Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
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.
Edited by on 310106 22:24 

Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
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) 

Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
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.


Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
thousandandone, try reading ll the posts before replying


Author  RE: Algorithms 
knutrainer Member
Posts: 243 Location:
Joined: 08.07.05 Rank: Apprentice  
That was thomasantony.
~PM me if you need help or instant message me.


Author  RE: Algorithms 
Member
Posts: Location:
Joined: 01.01.70 Rank: Guest  
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:
Code function generate(data,x='',n=0) {
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),'',7); //generate data until length=7
HTH
ps. sorry, the code parser appears to be non existant on here
Edited by on 020206 18:28 
