|
| 1 | +from typing import Callable, Optional |
| 2 | + |
| 3 | +from factorial import Factorial |
| 4 | + |
| 5 | + |
| 6 | +class PermutationsCombinations: |
| 7 | + @staticmethod |
| 8 | + def npr(n: int, r: int) -> int: |
| 9 | + if not isinstance(n, int) or not isinstance(r, int): |
| 10 | + raise ArithmeticError("Cannot calculate nPr for non-integers.") |
| 11 | + elif not n >= r >= 0: |
| 12 | + raise ArithmeticError("Please enter n ≥ r ≥ 0.") |
| 13 | + factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration |
| 14 | + return factorial(n) // factorial(n - r) |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def permutations(n: int, r: int) -> int: |
| 18 | + return PermutationsCombinations.npr(n, r) |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def ncr(n: int, r: int) -> int: |
| 22 | + if not isinstance(n, int) or not isinstance(r, int): |
| 23 | + raise ArithmeticError("Cannot calculate nCr for non-integers.") |
| 24 | + elif not n >= r >= 0: |
| 25 | + raise ArithmeticError("Please enter n ≥ r ≥ 0.") |
| 26 | + factorial: Callable[[int, Optional[bool]], int] = Factorial.factorial_iteration |
| 27 | + return factorial(n) // (factorial(r) * factorial(n - r)) |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def combinations(n: int, r: int) -> int: |
| 31 | + return PermutationsCombinations.ncr(n, r) |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == '__main__': |
| 35 | + input_n = int(input("Enter n: ")) |
| 36 | + input_r = int(input("Enter r: ")) |
| 37 | + print("Permutations:", PermutationsCombinations.permutations(n=input_n, r=input_r)) |
| 38 | + print("Combinations:", PermutationsCombinations.combinations(n=input_n, r=input_r)) |
0 commit comments