Computer combination of a number
import math
n = int(input("Enter the total number of objects: ")) r = int(input("Enter the number of objects taken at a time: ")) combination = math.comb(n, r) print("Combination of", n, "objects taken", r, "at a time is", combination) To compute the combination of a number, you can use the `math` module in Python, which provides a function called `comb(n, r)` to calculate the combination of `n` objects taken `r` at a time.Here's a Python program that uses the `comb()` function to compute the combination of a number:
In this program, the `input()` function is used to get the values of `n` (total number of objects) and `r` (number of objects taken at a time) from the user. The `math.comb()` function is then called with these values to compute the combination. Finally, the result is printed to the console.
Note: The `math.comb()` function is available in Python version 3.8 or later. If you are using an older version of Python, you can use the `scipy.special.comb()` function from the `scipy` module instead.
Comments
Post a Comment