0% found this document useful (0 votes)
5 views2 pages

Program 6

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Program 6

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 ( (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

Here’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)}.")

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.

You might also like