Click to See Complete Forum and Search --> : Number Combination


kmyadam
December 4th, 2005, 09:11 PM
Hi All,

I am facing problem in generating Number combinations. Can anyone tell me generating the combinations. Here is what i want.


First the algorithm shud produce single digit number combination, then 2 digit .... up to n digits.

For example N=3

First step
1
2
3

Second step(it shud take combinations of the above two steps, We shud be able to skip the repeated combinations)

1 2
1 3
2 3

Third step(we shud take the combinations and combine to generate 3 step)
1 2 3 ( We dont consider 112,113,212,213,223,312,313,323 because we dont want the numbers to be repeated)



Let me give combinations for N=5

First step( length of 1)
1
2
3
4
5

Second Step(length of 2)
1 2
1 3
1 4
1 5

2 3
2 4
2 5

3 4
3 5

4 5


1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5

2 3 4
2 3 5
2 4 5
3 4 5

Fourth Step(length of 4)
1 2 3 4
1 2 3 5
1 2 4 5
1 3 4 5
2 3 4 5

Fifth step(length of 5)
1 2 3 4 5

Hopefully it shud be clear. Let me know if i m not clear with my question.

Thanks a lot,
Kedar.

CBasicNet
December 4th, 2005, 11:31 PM
Take a look at this article,

http://www.codeguru.com/Cpp/Cpp/algorithms/combinations/article.php/c5117/

This article is due for an update, just that I haven't able to take time off to do iit. The updated article will contain 4 new algorithms.