Permutation of a number
def factorial(n):
if n == 0 or n == 1: return 1 else: return n * factorial(n-1) def permutations(n): num_str = str(n) num_digits = len(num_str) perm = factorial(num_digits) digit_count = [num_str.count(d) for d in num_str] for count in digit_count: perm //= factorial(count) return perm num = int(input("Enter a number: ")) permutations_count = permutations(num) print("Number of permutations:", permutations_count) Explanation:1. The `factorial` function is defined to calculate the factorial of a number using recursion.
2. The `permutations` function takes a number as input and calculates the permutations count for that number as follows:
- Convert the number to a string and determine the number of digits.
- Calculate the factorial of the number of digits.
- Calculate the count of each digit in the number and divide the factorial by the factorial of each count.
- Return the final permutations count.
3. The user is prompted to enter a number.
4. The `permutations` function is called with the input number as the argument to calculate the number of permutations.
5. The result is printed as the number of permutations.
Note: This program assumes that the input number is a positive integer.
Comments
Post a Comment