
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Logarithm Gamma of a Given Number in C++
The gamma function is described as an extension of the factorial of every given number in mathematics. The factorial, on the other hand, can only be defined for real numbers, hence the gamma function exceeds to calculate the factorial on all complex values other than negative integers. It is represented by ?
$$\mathrm{\Gamma \left ( x \right )=\left ( x-1 \right )!}$$
The gamma function grows quickly for higher values; hence, applying logarithm to the gamma will greatly slow it down. 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 the gamma function for a given input number x in C++.
Logarithm Gamma with lgamma() function
C++ cmath library has a lgamma() function which takes an argument x, then perform gamma(x) and applies natural logarithm on that value. The syntax for using lgamma() is shown below ?
Syntax
#include < cmath > lgamma( <number> )
Algorithm
- read a number x
- res := Logarithm gamma using lgamma( x )
- return res
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
Using gamma() and log() functions
C++ also provides tgamma() method for gamma and log() functions as well. We can use them to formulate the lgamma(). Let us see the algorithm for a clear idea.
Algorithm
- read a number x
- g := calculate gamma using tgamma( x )
- res := Logarithm gamma using log( g )
- return res
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
Using factorial() and log() functions
In the last example, we have seen the use of the tgamma() and log() methods. We can define our factorial() function, but this will only accept positive numbers. Let us see the algorithm for a better understanding.
Algorithm
define the factorial function, this will take n
-
if n is 1, then
return n
-
otherwise
return n * factorial ( n - 1 )
end if
In the main method, take a number x to find the log gamma for x
g := factorial( x - 1)
res := use log( g ) to find the natural logarithm of g
return res
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)
Conclusion
The gamma methods are sometimes referred to as an extension of the factorial method. Since gamma or factorial methods increase so fast, we can use a logarithm on it. In this article, we have seen a few techniques to perform the logarithm gamma on a given number x. Initially, we used the default function, which is lgamma() from the cmath library in C++. The second method is using tgamma() and log() and finally defines our factorial method. However, the final method is limited to positive numbers only. It will not work for negative numbers. And it will only perform well for integer numbers.