Tuesday, November 25, 2025
HomeLanguagesPython math.gamma() Method

Python math.gamma() Method

Python in its language allows various mathematical operations, which has manifolds application in scientific domain. One such offering of Python is the inbuilt gamma() function, which numerically computes the gamma value of the number that is passed in the function.

Syntax : math.gamma(x)
Parameters :
x : The number whose gamma value needs to be computed.

Returns : The gamma value, which is numerically equal to “factorial(x-1)”.

Code #1 : Demonstrating the working of gamma()




# Python code to demonstrate
# working of gamma()
import math
  
# initializing argument
gamma_var = 6
  
# Printing the gamma value.
print ("The gamma value of the given argument is : "
                       + str(math.gamma(gamma_var)))


Output:

The gamma value of the given argument is : 120.0

 

factorial() vs gamma()

The gamma value can also be found using factorial(x-1), but the use case of gamma() is because, if we compare both the function to achieve the similar task, gamma() offers better performance.

Code #2 : Comparing factorial() and gamma()




# Python code to demonstrate
# factorial() vs gamma()
import math
import time 
  
# initializing argument
gamma_var = 6
  
# checking performance 
# gamma() vs factorial()
start_fact = time.time()
res_fact = math.factorial(gamma_var-1)
  
print ("The gamma value using factorial is : " 
                              + str(res_fact))
  
print ("The time taken to compute is : "
        + str(time.time() - start_fact))
  
print ('\n')
  
start_gamma = time.time()
res_gamma = math.gamma(gamma_var)
  
print ("The gamma value using gamma() is : "
                           + str(res_gamma))
  
print ("The time taken to compute is : " 
       + str(time.time() - start_gamma))


Output:

The gamma value using factorial is : 120
The time taken to compute is : 9.059906005859375e-06

The gamma value using gamma() is : 120.0
The time taken to compute is : 5.245208740234375e-06
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32412 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6790 POSTS0 COMMENTS
Nicole Veronica
11934 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6911 POSTS0 COMMENTS
Ted Musemwa
7169 POSTS0 COMMENTS
Thapelo Manthata
6868 POSTS0 COMMENTS
Umr Jansen
6856 POSTS0 COMMENTS