Google Sheets GAMMA Function: How to Use It
In this article
Google Sheets does not have a built-in GAMMA function, so you cannot type =GAMMA(x) and get a result. The good news is you can get the same answer in a few reliable ways: use the built-in GAMMALN function and combine it with EXP, or use GAMMA.DIST for probability calculations. This guide shows you exactly how to do both, plus how to build a reusable custom function if you need the plain gamma value often.
What You'll Need
- A Google account with access to Google Sheets (free).
- A spreadsheet open in a browser or the Sheets mobile app.
- Basic familiarity with entering formulas (starting with
=). - For the custom function method: permission to use Apps Script in the spreadsheet (available on personal and most work accounts; some organisations restrict it).
Step-by-Step
Method 1: Use GAMMALN and EXP for the gamma value
- Click an empty cell where you want the result.
- Type
=EXP(GAMMALN(5))and press Enter. This returns 24, because gamma(5) = 4! = 24. - To use a cell reference instead of a hard-coded number, put your value in cell A1 and type
=EXP(GAMMALN(A1)). - Drag the formula down a column to calculate gamma for a list of values.
- If you need the natural log of gamma directly (common in statistics), just use
=GAMMALN(A1)without theEXPwrapper.
This works because GAMMALN(x) returns ln(Γ(x)), so raising e to that power gives Γ(x). For large inputs this method is more stable than trying to compute factorials directly, which overflow quickly in spreadsheets.
Method 2: Use GAMMA.DIST for probability work
- Select an empty cell.
- Type
=GAMMA.DIST(x, alpha, beta, cumulative). For example,=GAMMA.DIST(5, 2, 3, TRUE)returns the cumulative probability. - Set the fourth argument to
FALSEto get the probability density instead of the cumulative value. - Use
GAMMA.INV(probability, alpha, beta)when you need the inverse — the x value for a given cumulative probability.
Note that GAMMA.DIST is the gamma distribution, not the gamma function itself. If you specifically need Γ(x), use Method 1 or Method 3.
Method 3: Create a custom GAMMA function with Apps Script
- In your spreadsheet, open Extensions > Apps Script. Menu labels can differ slightly by version and region.
- In the script editor, delete any placeholder code and paste the following:
function GAMMA(x) { if (x <= 0 && x === Math.floor(x)) { throw new Error('Gamma is undefined for zero and negative integers'); } if (x < 0.5) { return Math.PI / (Math.sin(Math.PI * x) * GAMMA(1 - x)); } x -= 1; var g = 7; var c = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]; var a = c[0]; var t = x + g + 0.5; for (var i = 1; i < g + 2; i++) { a += c[i] / (x + i); } return Math.sqrt(2 * Math.PI) * Math.pow(t, x + 0.5) * Math.exp(-t) * a; } - Click the save icon and give the project a name when prompted.
- Return to your spreadsheet. Type
=GAMMA(5)in a cell and press Enter. The first time, Google may ask you to authorise the script — follow the on-screen prompts. - The custom function now works like any built-in formula and updates when referenced cells change.
This implementation uses the Lanczos approximation, which is accurate for most practical inputs. It will not match specialised statistical software for very large or very small arguments, but it is fine for everyday spreadsheet work.
Common Mistakes
- Typing
=GAMMA(x)without a custom function. Sheets returns#NAME?because no built-in GAMMA exists. UseEXP(GAMMALN(x))or add the script. - Confusing GAMMA.DIST with the gamma function.
GAMMA.DISTneeds three parameters (x, alpha, beta) and returns a probability, not Γ(x). - Using zero or negative integers. The gamma function is undefined at 0, -1, -2, and so on. Both
GAMMALNand the custom function will error. - Forgetting the
EXPwrapper.GAMMALN(5)returns about 3.178, which is ln(24), not 24. - Missing authorisation for the custom function. If the script does not run, check that you approved it when prompted.
Tips
- For integer inputs, gamma(n) = (n-1)!. So
=FACT(A1-1)is a quick alternative for whole numbers, though it overflows above roughly 170. - Use
GAMMALNdirectly when working with log-likelihoods or very large numbers, since it avoids overflow. - Name your input cell (for example, "x") so formulas read as
=EXP(GAMMALN(x)). - If you share the sheet, the custom function travels with it, but collaborators may need to authorise the script themselves.
- Round results with
ROUNDwhen displaying, because floating-point output can show long decimals.
Troubleshooting
- #NAME? error: The function name is misspelled or the custom script is not saved/authorised. Recheck spelling and open Extensions > Apps Script to confirm the code is there.
- #NUM! error: The input is zero or a negative integer, where gamma is undefined. Change the input.
- Custom function returns nothing or an error about permissions: Re-open the sheet, then try the formula again and complete the authorisation prompt. If your organisation blocks Apps Script, use Method 1 instead.
- Results look slightly off for very large inputs: Floating-point limits apply. Use
GAMMALNand work in log space. - Formula works on desktop but not mobile: Custom functions generally work in the Sheets mobile app, but authorisation prompts may require the desktop browser. Open the sheet on a computer once to approve the script.
Frequently Asked Questions
Does Google Sheets have a GAMMA function?
No. There is no built-in GAMMA function. Use =EXP(GAMMALN(x)) or add a custom Apps Script function.
What is the difference between GAMMALN and GAMMA?
GAMMALN(x) returns the natural logarithm of the gamma function, ln(Γ(x)). The gamma function itself is Γ(x), which you get by wrapping it in EXP.
Can I use GAMMA.DIST to get the gamma function value?
No. GAMMA.DIST returns probabilities from the gamma distribution and requires three parameters. It is a different calculation.
Why does GAMMALN return an error for 0?
The gamma function is undefined at zero and at negative integers, so the logarithm is also undefined there.
Will my custom GAMMA function work for other people?
Yes, the script is bound to the spreadsheet, so anyone with edit access can use it after authorising it in their own account.
Is the custom function accurate enough for statistics?
For typical spreadsheet use, yes. For high-precision or extreme values, use dedicated statistical software or work with GAMMALN in log space.