Task

Write a program that generates all   permutations   of   n   different objects.   (Practically numerals!)



Related tasks
 
   Find the missing permutation
   Permutations/Derangements



 The number of samples of size k from n objects.


With   combinations and permutations   generation tasks. 






 Order Unimportant

 Order Important


 Without replacement








(


n
k


)




=

n



C

k


=



n
(
n
−
1
)
…
(
n
−
k
+
1
)


k
(
k
−
1
)
…
1





{\displaystyle {\binom {n}{k}}=^{n}\operatorname {C} _{k}={\frac {n(n-1)\ldots (n-k+1)}{k(k-1)\dots 1}}}










n



P

k


=
n
⋅
(
n
−
1
)
⋅
(
n
−
2
)
⋯
(
n
−
k
+
1
)


{\displaystyle ^{n}\operatorname {P} _{k}=n\cdot (n-1)\cdot (n-2)\cdots (n-k+1)}




Task: Combinations

Task: Permutations


 With replacement








(



n
+
k
−
1

k


)




=

n
+
k
−
1



C

k


=



(
n
+
k
−
1
)
!


(
n
−
1
)
!
k
!





{\displaystyle {\binom {n+k-1}{k}}=^{n+k-1}\operatorname {C} _{k}={(n+k-1)! \over (n-1)!k!}}








n

k




{\displaystyle n^{k}}




Task: Combinations with repetitions

Task: Permutations with repetitions



MODULE 	Permute;
 
FROM	Terminal
IMPORT	Read, Write, WriteLn;
 
FROM	Terminal2
IMPORT	WriteString;
 
CONST	MAXIDX = 6;
	MINIDX = 1;
 
TYPE	TInpCh = ['a'..'z'];
	TChr   = SET OF TInpCh;
 
VAR	n,
	nl:	INTEGER;
	ch:	CHAR;
	a:	ARRAY[MINIDX..MAXIDX] OF CHAR;
	kt:     TChr = TChr{'a'..'f'};
 
PROCEDURE output;
VAR	i:	INTEGER;
BEGIN
	FOR i := MINIDX TO n DO Write(a[i]) END;
	WriteString(" | ");
END output;
 
PROCEDURE exchange(VAR x, y : CHAR);
VAR	z:	CHAR;
BEGIN z := x; x := y; y := z
END exchange;
 
PROCEDURE permute(k: INTEGER);
VAR	i:	INTEGER;
BEGIN
	IF k = 1 THEN
		output;
		INC(nl);
		IF (nl MOD 8 = 1) THEN WriteLn END;
	ELSE
		permute(k-1);
		FOR i := MINIDX TO k-1 DO
			exchange(a[i], a[k]);
			permute(k-1);
			exchange(a[i], a[k]);
		END
	END
END permute;
 
BEGIN
	n := 0;	nl := 1; WriteString("Input {a,b,c,d,e,f} >");
	REPEAT
		Read(ch);
		IF ch IN kt THEN INC(n); a[n] := ch; Write(ch) END
	UNTIL (ch <= " ") OR (n > MAXIDX);
 
	WriteLn;
	IF n > 0 THEN permute(n) END;
	(*Wait*)
END Permute.