# Approximate the Gamma Function

Page 1 of 1

## 0 Replies - 1640 Views - Last Post: 16 November 2011 - 09:01 AM

### #1 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

# Approximate the Gamma Function

Posted 16 November 2011 - 09:01 AM

Description: See implementation in snippet.Approximate the Gamma Function using a derivative of Stirling's approximation. This method only works for values grater than zero.
```using System;

namespace Gamma
{
class Program
{
static void Main()
{
Console.WriteLine("Enter a value - ");

// Get user input

try
{
// Print Results
Console.WriteLine("Gamma({0}) = {1}", input, Gamma(input));
Console.WriteLine();
Console.WriteLine("LnGamma({0}) = {1}", input, LnGamma(input));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

}

// Appriximate the Natural Logarithm of the Gamma Function through
// a derivative of Stirling's Approximation
private static double LnGamma(double z)
{
// Throw exception if z is not greater than zero
if(z <= 0)
{
throw new ArgumentException("Argument 'z' must be greater than zero");
}

// Approimate LnGamma(z)
return 0.5 * (Math.Log(2 * Math.PI) - Math.Log(z))+ z * (Math.Log(z + 1 / (12 * z - 1 / (10 * z))) - 1);
}

// Approximate Gamma Function by raising e to the power of LnGamma(z),
// which cancels the logarithm to provide Gamma(z)
private static double Gamma(double z)
{
return Math.Exp(LnGamma(z));
}
}
}
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }