Skip to the content of the web site.

Project K.10: Subsets

Write a function that prints all permutations of $n$ numbers from $1$ to $n$ taken $r$ at a time.

For example, all permutations of the numbers $\{1, 2, 3\}$ taken $2$ at a time are:

1,2
1,3
2,1
2,3
3,1
3,2

and taken $3$ at a time are:

1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1

All permutations of the numbers $\{1, 2, 3, 4, 5\}$ taken two at a time include:

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

while there would be three times this many if we were to consider this set taken three at a time.

In general, there are $n \cdot (n - 1) \cdots (n - r + 1)$ permutations of $n$ numbers taken $r$ at a time.

Your function should have the declaration

void print_permutations( unsigned int n, unsigned int r );