Number Utilities

Collatz Sequence

Generates the complete Collatz sequence (3n+1 problem) for any positive integer until reaching 1. Access via MCP in Cursor or Windsurf, or REST at `/v1/number/collatz`. For example, input 7 produces [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]. Returns deterministic sequences every time.

API Endpoint

GET /v1/number/collatz

Code Examples

curl "https://tinyfn.io/v1/number/collatz" \
  -H "X-API-Key: YOUR_API_KEY"
const response = await fetch('https://tinyfn.io/v1/number/collatz', {
  headers: { 'X-API-Key': 'YOUR_API_KEY' }
});
const data = await response.json();
console.log(data);
import requests

response = requests.get('https://tinyfn.io/v1/number/collatz',
    headers={'X-API-Key': 'YOUR_API_KEY'})
data = response.json()
print(data)

Use via MCP

Add to your AI agent

Connect your AI agent (Claude, Cursor, Windsurf, etc.) to TinyFn's number tools:

{
  "mcpServers": {
    "tinyfn-number": {
      "url": "https://tinyfn.io/mcp/number",
      "headers": {
        "X-API-Key": "YOUR_API_KEY"
      }
    }
  }
}

Learn more about MCP setup →

FAQ

What is the Collatz sequence and how does it work?

The Collatz sequence follows simple rules: if n is even, divide by 2; if odd, multiply by 3 and add 1. Continue until reaching 1. Despite its simplicity, it's an unsolved mathematical conjecture.

How do I generate a Collatz sequence using MCP in Cursor?

Use the collatz tool with any positive integer. The MCP tool returns the complete sequence as an array, making it easy for AI agents to analyze mathematical patterns or verify calculations.

What happens with very large numbers in Collatz sequences?

Large numbers can produce very long sequences before reaching 1. The tool handles arbitrary-sized integers but be aware that some numbers generate sequences with hundreds or thousands of steps.

Can I get just the sequence length instead of all values?

The tool returns the complete sequence array. To get just the length, count the returned array elements or use the sequence for further mathematical analysis in your code.

Are Collatz sequences different from other mathematical sequences?

Unlike arithmetic or geometric sequences with predictable patterns, Collatz sequences are chaotic and unpredictable. No formula exists to determine sequence length without computing each step.

Try Collatz Sequence Now

Get your free API key and start using Collatz Sequence in seconds.

Get Free API Key