Python Program for lgamma() Function

In the previous article, we have discussed Python Program for Set max() Method
lgamma() Function in Python:

The math.lgamma() method returns a number’s natural logarithm gamma value.

Tip: We can also find the log gamma value by first finding the gamma value and then using the math.log() method to calculate the log of that value.

The gamma value is equal to the factorial (x-1).

Syntax:

math.lgamma(x)

Parameters:

x: This is required. It is a number for which the log gamma value should be calculated. If the number is a negative integer, a ValueError is returned. It throws a TypeError if it is not a number.

Return Value:

Returns a float value representing a number’s log gamma value.

  • The output of the lgamma function is returned if the number argument is a positive integer, positive or negative decimal.
  • The lgamma function returns ValueError if the number argument is a Negative integer.
  • If it is not a number, the lgamma Math function throws a TypeError.

Examples:

Example1:

Input:

Given first number = 8
Given second number = 4

Output:

The result after applying lgamma() function on above given first number 8  =  8.525161361065415
The result after applying lgamma() function on above given second number 4  =  1.7917594692280554

Example2:

Input:

Given first number = 5
Given second number = 10

Output:

The result after applying lgamma() function on above given first number 5  =  3.178053830347945
The result after applying lgamma() function on above given second number 10  =  12.801827480081467

Program for lgamma() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Apply math.lgamma() function to the given first number to get the natural logarithm gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.lgamma() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [2, 1, 3, 0.4]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 8
# Apply math.lgamma() function to the given first number to get the natural
# logarithm gamma value of a given number.
# Store it in another variable.
fnl_rslt = math.lgamma(gvn_numb1)
# Print the above result
print("The result after applying lgamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 4
print("The result after applying lgamma() function on above given second number",
      gvn_numb2, " = ", math.lgamma(gvn_numb2))
# Apply math.lgamma() function to the given list element and print it.
print(
    "The result after applying lgamma() function on given list element gvnlst[1] = ", math.lgamma(gvn_lst[1]))

Output:

The result after applying lgamma() function on above given first number 8  =  8.525161361065415
The result after applying lgamma() function on above given second number 4  =  1.7917594692280554
The result after applying lgamma() function on given list element gvnlst[1] =  0.0

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Apply math.lgamma() function to the given first number to get the natural logarithm gamma value of a given number.
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.lgamma() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.lgamma() function to the given first number to get the natural
# logarithm gamma value of a given number.
# Store it in another variable.
fnl_rslt = math.lgamma(gvn_numb1)
# Print the above result
print("The result after applying lgamma() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 =  float(input("Enter some random number = "))
print("The result after applying lgamma() function on above given second number",
      gvn_numb2, " = ", math.lgamma(gvn_numb2))
# Apply math.lgamma() function to the given list element and print it.
print(
    "The result after applying lgamma() function on given list element gvnlst[2] = ", math.lgamma(gvn_lst[2]))

Output:

Enter some random List Elements separated by spaces = 1 0 3 4 7
Enter some random number = 5
The result after applying lgamma() function on above given first number 5.0 = 3.178053830347945
Enter some random number = 10
The result after applying lgamma() function on above given second number 10.0 = 12.801827480081467
The result after applying lgamma() function on given list element gvnlst[2] = 0.693147180559945