Members Online
Total Online: 39 Web Spiders: 17
Guests Online: 36
Members Online: 3
Registered Members: 70160 Newest Member: Furtif
|
View Thread
| Author |
Algorithms |
z0mb1e
Member
Posts: 19
Location:
Joined: 28.01.06 Rank: Moderate |
|
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
When there's no more room in Hell, the dead will walk the Earth... |
|
| Author |
RE: Algorithms |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
you want the combo's or the amount?
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

|
|
| Author |
RE: Algorithms |
z0mb1e
Member
Posts: 19
Location:
Joined: 28.01.06 Rank: Moderate |
|
i dont want to know how many are the possibilities (in my example=6), but an algorithm to output all those rows...
When there's no more room in Hell, the dead will walk the Earth...
Edited by z0mb1e on 31-01-06 22:14 |
|
| Author |
RE: Algorithms |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
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.
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

Edited by wolfmankurd on 31-01-06 22:24 |
|
| Author |
RE: Algorithms |
thomasantony
Member
Posts: 52
Location:
Joined: 19.11.05 Rank: Monster |
|
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?
Thomas |
|
| Author |
RE: Algorithms |
z0mb1e
Member
Posts: 19
Location:
Joined: 28.01.06 Rank: Moderate |
|
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.
When there's no more room in Hell, the dead will walk the Earth... |
|
| Author |
RE: Algorithms |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
thousandandone, try reading ll the posts before replying 
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

|
|
| Author |
RE: Algorithms |
knutrainer
Member

Posts: 243
Location:
Joined: 08.07.05 Rank: Elite |
|
That was thomasantony.

~PM me if you need help or instant message me.
|
|
| Author |
RE: Algorithms |
daemorhedron
Member
Posts: 36
Location:
Joined: 10.01.06 Rank: Mad User |
|
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:
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 daemorhedron on 02-02-06 18:28 |
|
|
|
|