0

I need to print all possible combinations of five numbers selected from $\{1, 2, 3, \ldots, 10\}$. You can think of it like a lottery. You have $5$ numbers and each number can go from $1$ to $10$ and no number can be repeated.

How can I print all possible combinations?

My attempt was to add $1$ to the last number until it reaches the end ($10$), then do the same with the $4$th number until it reaches the end, and the same with $3$rd, $2$nd and $1$st.

1 2 3 4 5

1 2 3 4 6

.........etc

............10

1 2 3 5 10

1 2 3 6 10

..........etc

..........10 10

But as you can see, I repeat numbers and don't get all combinations.

N. F. Taussig
  • 76,571
bau8312
  • 171
  • After you reach $1\ 2\ 3\ 9\ 10$, switch to $1\ 2\ 4\ 5\ 6$, then continue as before. There are a total of $\binom{10}{5} = 252$ such combinations. – N. F. Taussig Mar 19 '16 at 10:38

2 Answers2

1

For combinations:

    N=10;
    for(a=1; a <= N-4; ++a)
     for(b=a+1; b <= N-3; ++b)
      for(c=b+1; c <= N-2; ++c)
       for(d=c+1; d <= N-1; ++d)
        for(e=d+1; e <= N; ++e)
         print a,b,c,d,e

http://js.do/daveyp225/comb

For permutations:

    N=10;
    for(a=1; a <= N; ++a)
     for(b=1; b <= N; ++b)
      if(b different from a) 
       for(c=1; c <= N; ++c)
       if(c different from a,b) 
        for(d=1; d <= N; ++d)
        if(d different from a,b,c) 
         for(e=1; e <= N; ++e)
         if(e different from a,b,c,d) 
           print a,b,c,d,e

http://js.do/daveyp225/perm

David P
  • 12,320
1

If you just need them listed, here they are:

(1, 2, 3, 4, 5)
(1, 2, 3, 4, 6)
(1, 2, 3, 4, 7)
(1, 2, 3, 4, 8)
(1, 2, 3, 4, 9)
(1, 2, 3, 4, 10)
(1, 2, 3, 5, 6)
(1, 2, 3, 5, 7)
(1, 2, 3, 5, 8)
(1, 2, 3, 5, 9)
(1, 2, 3, 5, 10)
(1, 2, 3, 6, 7)
(1, 2, 3, 6, 8)
(1, 2, 3, 6, 9)
(1, 2, 3, 6, 10)
(1, 2, 3, 7, 8)
(1, 2, 3, 7, 9)
(1, 2, 3, 7, 10)
(1, 2, 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6, 8, 10)
(3, 4, 6, 9, 10)
(3, 4, 7, 8, 9)
(3, 4, 7, 8, 10)
(3, 4, 7, 9, 10)
(3, 4, 8, 9, 10)
(3, 5, 6, 7, 8)
(3, 5, 6, 7, 9)
(3, 5, 6, 7, 10)
(3, 5, 6, 8, 9)
(3, 5, 6, 8, 10)
(3, 5, 6, 9, 10)
(3, 5, 7, 8, 9)
(3, 5, 7, 8, 10)
(3, 5, 7, 9, 10)
(3, 5, 8, 9, 10)
(3, 6, 7, 8, 9)
(3, 6, 7, 8, 10)
(3, 6, 7, 9, 10)
(3, 6, 8, 9, 10)
(3, 7, 8, 9, 10)
(4, 5, 6, 7, 8)
(4, 5, 6, 7, 9)
(4, 5, 6, 7, 10)
(4, 5, 6, 8, 9)
(4, 5, 6, 8, 10)
(4, 5, 6, 9, 10)
(4, 5, 7, 8, 9)
(4, 5, 7, 8, 10)
(4, 5, 7, 9, 10)
(4, 5, 8, 9, 10)
(4, 6, 7, 8, 9)
(4, 6, 7, 8, 10)
(4, 6, 7, 9, 10)
(4, 6, 8, 9, 10)
(4, 7, 8, 9, 10)
(5, 6, 7, 8, 9)
(5, 6, 7, 8, 10)
(5, 6, 7, 9, 10)
(5, 6, 8, 9, 10)
(5, 7, 8, 9, 10)
(6, 7, 8, 9, 10)

The Python code I wrote:

import itertools

numbers = range(1, 11)
for combination in itertools.combinations(numbers, 5):
    print(combination)
Lynn
  • 3,338