The Collatz conjecture is one of the most famous unsolved problems in mathematics. The Collatz Sequence API generates these "hailstone" sequences, showing how any positive integer eventually reaches 1 through simple rules.
What is the Collatz Sequence?
The Collatz sequence starts with any positive integer and follows two simple rules:
- If the number is even, divide it by 2
- If the number is odd, multiply by 3 and add 1
Repeat until you reach 1 (then it loops: 1, 4, 2, 1, 4, 2...).
Example for n=6: 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1
The Collatz Conjecture
The Collatz conjecture states that this sequence eventually reaches 1 for ALL positive integers. Despite being tested for enormous numbers and studied for decades, no proof exists. Famous mathematician Paul Erdos said "Mathematics is not yet ready for such problems."
Interesting Properties
- Sequences can grow very large before eventually decreasing to 1
- Some starting numbers produce very long sequences
- The number 27 takes 111 steps to reach 1, peaking at 9,232
Using the Collatz Sequence API
TinyFn provides a comprehensive endpoint for Collatz sequences:
GET https://api.tinyfn.io/v1/math/collatz?number=27
Headers: X-API-Key: your-api-key
{
"starting_number": 27,
"sequence": [27, 82, 41, 124, 62, 31, 94, 47, 142, 71, ...],
"steps": 111,
"max_value": 9232,
"max_value_step": 77,
"sequence_length": 112,
"odd_steps": 70,
"even_steps": 41
}
Parameters
| Parameter | Type | Description |
|---|---|---|
number |
integer | Starting number for sequence (required, 1 to 10^12) |
include_sequence |
boolean | Include full sequence in response (default: true) |
max_steps |
integer | Maximum steps before stopping (default: 10000) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/collatz?number=27',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`Steps to reach 1: ${data.steps}`);
console.log(`Maximum value reached: ${data.max_value}`);
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/collatz',
headers={'X-API-Key': 'your-api-key'},
params={'number': 27}
)
data = response.json()
print(f"Sequence: {' → '.join(map(str, data['sequence'][:10]))}...")
print(f"Total steps: {data['steps']}")
cURL
curl "https://api.tinyfn.io/v1/math/collatz?number=27" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Mathematical Visualization: Create graphs of Collatz sequences
- Educational Tools: Teach recursion and iterative algorithms
- Programming Exercises: Practice implementing the algorithm
- Pattern Research: Study sequence properties and statistics
- Competitions: Project Euler and similar math challenges
Best Practices
- Set reasonable limits: Some sequences are extremely long
- Stats only for large numbers: Disable full sequence for efficiency
- Track maximums: The peak value is often interesting
- Visualize results: Graphing sequences reveals beautiful patterns
- Compare statistics: Analyze how different starting values behave
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 Collatz Sequence API
Get your free API key and start exploring this fascinating conjecture.
Get Free API Key