Need to calculate factorials in your application? This guide covers everything you need to know about factorial calculations via API, including properties, applications in combinatorics, and implementation examples.
What is Factorial?
The factorial of a non-negative integer n, denoted n!, is the product of all positive integers less than or equal to n. It represents the number of ways to arrange n distinct objects.
n! = n × (n-1) × (n-2) × ... × 2 × 1
Examples:
5! = 5 × 4 × 3 × 2 × 1 = 120
0! = 1 (by definition)
1! = 1
Factorial Properties
| n | n! |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 5 | 120 |
| 10 | 3,628,800 |
| 20 | 2,432,902,008,176,640,000 |
Using the Factorial API
TinyFn provides a simple endpoint for factorial calculations:
GET https://api.tinyfn.io/v1/math/factorial
Headers: X-API-Key: your-api-key
{
"n": 10,
"factorial": 3628800,
"scientific_notation": "3.6288e+6"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
n |
integer | Non-negative integer (0 to 170 for standard precision) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/factorial?n=10',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.factorial); // 3628800
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/factorial',
params={'n': 10},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['factorial']) # 3628800
cURL
curl "https://api.tinyfn.io/v1/math/factorial?n=10" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Permutations: Number of ways to arrange objects (n!)
- Combinations: Used in nCr = n! / (r!(n-r)!)
- Probability: Calculating probabilities in statistics
- Algorithms: Complexity analysis, tree structures
- Education: Math learning applications
Best Practices
- Validate input: Factorials are only defined for non-negative integers
- Handle large numbers: Use string representation for very large results
- Consider overflow: Standard numbers overflow around 170!
- Use logarithms: For very large n, use log(n!) = sum of log(k)
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-math": {
"url": "https://api.tinyfn.io/mcp/math/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all math tools available via MCP in our Math MCP Tools for AI Agents guide.
Try the Factorial Calculator API
Get your free API key and start calculating factorials in seconds.
Get Free API Key