Program 6
Program 6
numbers, and for complex numbers, you can use the Gamma function
(scipy.special.gamma). The factorial of a complex number is defined as ( (n + 1) ),
where ( ) is the Gamma function.
Here’s how you can implement a program to compute the factorial of both real and
complex numbers:
1. For real numbers, you can use a simple iterative or recursive method.
2. For complex numbers, you can use the Gamma function from scipy.
First, make sure you have the scipy library installed:
pip install scipy
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)
How It Works:
1. Input Handling: The program takes user input and tries to convert it into a complex
number.
2. Real Numbers:
– Uses a function factorial_real to compute the factorial of non-negative
integers.
– For non-negative integers, it computes the factorial using a loop.
3. Complex Numbers:
– Uses the scipy.special.gamma function to compute the Gamma function,
which is used to find the factorial of a complex number.
4. 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.
Example Usage:
• Real Number: Input 5 and the output will be The factorial of 5 is 120.
• Complex Number: Input 3+4j and the output will be something like The
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.