To calculate the factorial of a number, you can use Python’s built-in functions for real numbers, and for complex numbers, you can use the Gamma function (scipy.special.gamma). The factorial of a complex number is defined as ( \Gamma(n + 1) ), where ( \Gamma ) is the Gamma function.
Here’s how you can implement a program to compute the factorial of both real and complex numbers:
- For real numbers, you can use a simple iterative or recursive method.
- For complex numbers, you can use the Gamma function from
scipy.
First, make sure you have the scipy library installed:
pip install scipyHere’s the complete Python program:
import scipy.special
import cmath
def factorial_real(n):
"""Compute the factorial of a non-negative integer n."""
if n < 0:
return "Factorial is not defined for negative numbers."
if n == 0 or n == 1:
return 1
result = 1
for i in range(2, n + 1):
result *= i
return result
def factorial_complex(z):
"""Compute the factorial of a complex number z using the Gamma function."""
return scipy.special.gamma(z + 1)
# Input from the user
user_input = input("Enter a number (real or complex, e.g., 5 or 3+4j): ")
# Try to convert the input into a complex number
try:
number = complex(user_input)
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
if number.imag == 0: # Check if the number is real
number = int(number.real)
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"The factorial of {number} is {factorial_real(number)}.")
else:
print(f"The factorial of {number} is {factorial_complex(number)}.")- Input Handling: The program takes user input and tries to convert it into a complex number.
- Real Numbers:
- Uses a function
factorial_realto compute the factorial of non-negative integers. - For non-negative integers, it computes the factorial using a loop.
- Uses a function
- Complex Numbers:
- Uses the
scipy.special.gammafunction to compute the Gamma function, which is used to find the factorial of a complex number.
- Uses the
- Output: Depending on whether the input is a real or complex number, the program prints the factorial or informs the user that the factorial of a negative number is not defined.
- Real Number: Input
5and the output will beThe factorial of 5 is 120. - Complex Number: Input
3+4jand the output will be something likeThe factorial of (3+4j) is (9.557260855514974-32.48293407649355j).
This program effectively handles both real and complex numbers and uses appropriate methods to compute their factorials.