Count the ways to pick k items from n, first in order and then without order.
What to build
A program that prints P(n, k) and C(n, k).
Requirements
- Print
permutations P(ordered selections of k from n) andcombinations C(unordered selections).
Input
One line: n and k.
Output
Two lines.
Constraints
0 ≤ k ≤ n ≤ 20.
Example
Input:
4 2
Output:
permutations 12
combinations 6
Why it works
Each unordered selection of k items can be put in k! orders, so the permutations are exactly k! times the combinations.
Examples
Example 1 · Input
4 2
Expected output
permutations 12 combinations 6
Example 2 · Input
5 5
Expected output
permutations 120 combinations 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
math.perm(n, k) counts ordered selections; math.comb(n, k) unordered ones.
Hint 2
Both are 1 when k is 0.
Solution
Show one correct solution (try it yourself first)
from math import comb, perm
n, k = map(int, input().split())
print("permutations", perm(n, k))
print("combinations", comb(n, k))