Home >Backend Development >C++ >C++ program to calculate log gamma of a given number

C++ program to calculate log gamma of a given number

WBOY
WBOYforward
2023-08-25 15:13:061740browse

C++ program to calculate log gamma of a given number

The gamma function is described as the expansion of the factorial of each given number math. On the other hand, factorial can only be defined for real numbers, so The gamma function goes beyond computing division Negative integer. It is represented by -

$$\mathrm{\Gamma \left ( x \right )=\left ( x-1 \right )!}$$

The gamma function grows rapidly for higher values; therefore, logarithmically apply Gamma will slow it down significantly. The natural logarithm gamma of a particular number is Another name for it.

In this article we will see how to calculate the logarithm of a given gamma function Enter the number x in C.

Use lgamma() function to logarithm Gamma

C cmath library has a lgamma() function, which accepts parameter x and then executes gamma(x) and apply the natural logarithm to the value. The syntax for using lgamma() is As follows -

grammar

#include < cmath >
lgamma( <number> )

algorithm

  • Read numbers x
  • res := Logarithmic gamma using lgamma( x )
  • Return results

Example

#include <iostream>
#include <cmath>
using namespace std;
float solve( float x ){
   float answer;
   answer = lgamma( x );
   return answer;
}
int main(){
   cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl;
   cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl;
   cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl;
   cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl;
}

Output

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Logarithm Gamma for x = -1.2 is: 1.57918
Logarithm Gamma for x = 3.1415 is: 0.827604

Use gamma() and log() functions

C also provides the tgamma() method for the gamma and log() functions. we can use They come to formulate lgamma(). Let's look at the algorithm to get a clear idea.

algorithm

  • Read numbers x
  • g := Use tgamma( x ) to calculate gamma
  • res := logarithmic gamma using log( g )
  • Return results

Example

#include <iostream>
#include <cmath>
using namespace std;
float solve( float x ){
   float answer;
   float g = tgamma( x );
   answer = log( g );
   return answer;
}
int main(){
   cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl;
   cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl;
   cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl;
   cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl;
}

Output

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Logarithm Gamma for x = -1.2 is: 1.57918
Logarithm Gamma for x = 3.1415 is: 0.827604

Use Factorial() and log() functions

In the previous example, we saw the use of tgamma() and log() methods. we can Define our factorial() function, but only accept positive numbers. let us see algorithm for better understanding.

algorithm

  • To define the factorial function, n

  • is required
  • If n is 1, then

    • Return n

  • otherwise

    • Return n * factorial ( n - 1 )

  • End if

  • In the main method, use the number x to find the log gamma of x

  • g := factorial (x - 1)

  • res := Use log( g ) to find the natural logarithm of g

  • Return results

Example

#include <iostream>
#include <cmath>
using namespace std;
long fact( int n ){
   if( n == 1 ) {
      return n;
   } else {
      return n * fact( n - 1);
   }
}
float solve( float x ){
   float answer;
   float g = fact( x - 1 );
   answer = log( g );
   return answer;
}
int main(){
   cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl;
   cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl;
   cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl;
}

Output

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Segmentation fault (core dumped)

in conclusion

The gamma method is sometimes called an extension of the factorial method. Since the gamma or factorial method grows so quickly, we can use logarithms for it. at this In the article we saw some techniques to perform log gamma on a given number X. Initially, we use the default function, lgamma() from the cmath library in C. The second way is to use tgamma() and log() and finally define our factorial method. However, the final method is limited to positive numbers. it doesn't work with negative numbers number. And it only performs well with integers.

The above is the detailed content of C++ program to calculate log gamma of a given number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete