Table of Contents
Use lgamma() function to logarithm Gamma
grammar
algorithm
Example
Output
Use gamma() and log() functions
Use Factorial() and log() functions
in conclusion
Home Backend Development C++ C++ program to calculate log gamma of a given number

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

Aug 25, 2023 pm 03:13 PM
c program calculate Log gamma

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> )
Copy after login

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;
}
Copy after login

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
Copy after login
Copy after login

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;
}
Copy after login

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
Copy after login
Copy after login

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;
}
Copy after login

Output

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

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CUDA's universal matrix multiplication: from entry to proficiency! CUDA's universal matrix multiplication: from entry to proficiency! Mar 25, 2024 pm 12:30 PM

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

How to calculate addition, subtraction, multiplication and division in word document How to calculate addition, subtraction, multiplication and division in word document Mar 19, 2024 pm 08:13 PM

WORD is a powerful word processor. We can use word to edit various texts. In Excel tables, we have mastered the calculation methods of addition, subtraction and multipliers. So if we need to calculate the addition of numerical values ​​in Word tables, How to subtract the multiplier? Can I only use a calculator to calculate it? The answer is of course no, WORD can also do it. Today I will teach you how to use formulas to calculate basic operations such as addition, subtraction, multiplication and division in tables in Word documents. Let's learn together. So, today let me demonstrate in detail how to calculate addition, subtraction, multiplication and division in a WORD document? Step 1: Open a WORD, click [Table] under [Insert] on the toolbar, and insert a table in the drop-down menu.

How to count the number of elements in a list using Python's count() function How to count the number of elements in a list using Python's count() function Nov 18, 2023 pm 02:53 PM

How to use Python's count() function to calculate the number of an element in a list requires specific code examples. As a powerful and easy-to-learn programming language, Python provides many built-in functions to handle different data structures. One of them is the count() function, which can be used to count the number of elements in a list. In this article, we will explain how to use the count() function in detail and provide specific code examples. The count() function is a built-in function of Python, used to calculate a certain

Count the number of occurrences of a substring recursively in Java Count the number of occurrences of a substring recursively in Java Sep 17, 2023 pm 07:49 PM

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

How to use the Math.Pow function in C# to calculate the power of a specified number How to use the Math.Pow function in C# to calculate the power of a specified number Nov 18, 2023 am 11:32 AM

In C#, there is a Math class library, which contains many mathematical functions. These include the function Math.Pow, which calculates powers, which can help us calculate the power of a specified number. The usage of the Math.Pow function is very simple, you only need to specify the base and exponent. The syntax is as follows: Math.Pow(base,exponent); where base represents the base and exponent represents the exponent. This function returns a double type result, that is, the power calculation result. Let's

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

C program uses rename() function to change file name C program uses rename() function to change file name Sep 21, 2023 pm 10:01 PM

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

C++ program to print dictionary C++ program to print dictionary Sep 11, 2023 am 10:33 AM

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define

See all articles