# Factorial Calculator

> Computes n! = 1 × 2 × … × n for integers from 0 to 170, together with the number of decimal digits, the number of trailing zeros and a scientific-notation form.

- Calculator id: `factorial` · Category: Math (`math`) · Tool name: `calculate_factorial`
- Canonical page: https://tttkmbb.com/math/factorial · This document: https://tttkmbb.com/math/factorial.md · JSON definition: https://tttkmbb.com/math/factorial.json

## Purpose

Computes n! = 1 × 2 × … × n for integers from 0 to 170, together with the number of decimal digits, the number of trailing zeros and a scientific-notation form.

**Use when:** You need the factorial of an integer, e.g. for counting arrangements or as a building block of permutations and combinations.

**Do not use when:** n exceeds 170 (the result overflows double precision; use Stirling's approximation or log-gamma) or you need C(n, k) / P(n, k) directly (use combinations-permutations).

## Input

| Parameter | Type | Unit | Required | Description |
| --- | --- | --- | --- | --- |
| `n` | integer |  | required | Non-negative integer up to 170. (min 0, max 170) |

## Output

| Field | Type | Unit | Description |
| --- | --- | --- | --- |
| `factorial` | number |  | The factorial. Exact up to 22!; above that correct to about 16 significant digits. |
| `digits` | integer |  | Decimal digits of n!: ⌊log10(n!)⌋ + 1. |
| `trailing_zeros` | integer |  | Zeros at the end of n! (Legendre's formula: ⌊n/5⌋ + ⌊n/25⌋ + …). |
| `scientific_notation` | string |  | n! with 5 significant digits, e.g. '2.4329 × 10^18'. |

## Formula

`n! = n × (n − 1) × … × 2 × 1, with 0! = 1; digits = ⌊Σ log10(i)⌋ + 1; trailing_zeros = Σ ⌊n / 5^k⌋`

## Data Sources

- Wikipedia – Factorial — https://en.wikipedia.org/wiki/Factorial (reference, retrieved 2026-09-23)
- NIST Digital Library of Mathematical Functions §5.2 – Gamma function: definitions (n! = Γ(n + 1)) — https://dlmf.nist.gov/5.2 (standard, retrieved 2026-09-23)
- Wolfram MathWorld – Factorial — https://mathworld.wolfram.com/Factorial.html (reference, retrieved 2026-09-23)

Data freshness: `static`. Deterministic formula with fixed constants; results never go stale. Inputs supplied by the caller determine the output.

## API

- `GET https://tttkmbb.com/api/v1/calculate/factorial?n=…`
- `POST https://tttkmbb.com/api/v1/calculate/factorial` with JSON body `{"inputs": {…}}`
- Response: unified envelope (`success`, `request`, `result.values`, `result.units`, `sources`, `freshness`, `timestamp`, `next_actions`, `links`); see https://tttkmbb.com/docs/response-format.md
- Schema: https://tttkmbb.com/api/v1/calculators/factorial · OpenAPI operationId `calculate_factorial` in https://tttkmbb.com/openapi.json
- Authentication: none. Rate limit: fair use, see https://tttkmbb.com/docs/rate-limits.md.

## MCP

- Server: `https://tttkmbb.com/mcp` (Streamable HTTP, JSON-RPC 2.0, no auth)
- Tool:  `run_calculator` with `{"calculator_id": "factorial", "inputs": {…}}`

## Example

- 5!: inputs `{"n":5}` → `{"factorial":120,"digits":3,"trailing_zeros":1,"scientific_notation":"1.2 × 10^2"}`
- 20!: inputs `{"n":20}` → `{"factorial":2432902008176640000,"digits":19,"trailing_zeros":4,"scientific_notation":"2.4329 × 10^18"}`

```
GET https://tttkmbb.com/api/v1/calculate/factorial?n=5
```

## Limitations

n exceeds 170 (the result overflows double precision; use Stirling's approximation or log-gamma) or you need C(n, k) / P(n, k) directly (use combinations-permutations). All values are computed from the formula above; no measurement or live data is involved.

## FAQ

**Why is 0! equal to 1?**

By definition (the empty product), which keeps the recurrence n! = n × (n − 1)! and the counting formulas consistent.

**Is the result exact?**

Up to 22! every digit is exact. From 23! on the value has more than 53 significant bits, so only the first ~16 significant digits are reliable; the digit and trailing-zero counts stay exact.

## Related

- [Combinations and Permutations Calculator](https://tttkmbb.com/math/combinations-permutations.md) — Counts of selections and arrangements built from factorials.
- [Exponent Calculator](https://tttkmbb.com/math/exponent.md) — Powers, the other fast-growing product.
