Collatz sequence calculator
Start from any positive whole number and watch the Collatz rule run to 1: from 6 the path is 6, 3, 10, 5, 16, 8, 4, 2, 1, taking 8 steps and peaking at 16. Halve the even ones, triple the odd ones and add one. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 6
- Max
- 16
- Length
- 9
The Collatz conjecture, also called the 3n+1 problem or hailstone sequence, asks a deceptively simple question: start from any positive integer, repeatedly replace even numbers with half their value and odd numbers with three times plus one, and you always seem to reach one. Teachers, researchers, coding interview prep, and curious learners use it to inspect trajectories without writing the loop themselves.
What the Collatz rule does to a number
The Collatz process is defined on positive integers only. From a starting value n, if n is even you replace it with n divided by two; if n is odd you replace it with three times n plus one. You keep applying that rule until the value becomes one.
The sequence of values produced along the way is often called the hailstone sequence because numbers rise and fall like hail in a storm cloud before they settle. Famous small examples include six, which travels through three, ten, five, sixteen, eight, four, two and finally one in eight steps, peaking at sixteen. Twenty-seven is a classic longer case: its path climbs into the thousands before descending.
The open mathematical question is whether every positive integer eventually reaches one; no counterexample is known, and the conjecture has been checked by computer for enormous ranges, but a general proof is still missing.
What you can do with it
Classroom demos and homework checks
Show students the path for 6, 27 or 97 with the full sequence, step count and peak value so solutions match a single reference implementation.
Interview and kata reference answers
Generate expected Collatz outputs for automated graders that score step counts or sequence length without embedding brittle local copies of the loop.
Visualization and research batches
Fetch structured hailstone paths for ranges of n to plot stopping times, record peaks, or animate number trajectories in notebooks and dashboards.
FAQ
What does this Collatz sequence calculator return?
For a positive integer n it returns the full hailstone sequence until 1, the number of steps taken, the maximum value reached, and the sequence length.
What happens if n is less than 1?
Only integers greater than or equal to 1 are accepted, matching the mathematical domain of the Collatz map.
Is the Collatz conjecture proven?
No. This tool computes the sequence for your n; it does not claim a general proof that every positive integer reaches 1.
Are results deterministic?
Yes. The same n always produces the same sequence, steps and max, with no randomness, network calls or clock involvement.
Why is it called the hailstone sequence?
Because the values rise and fall like hailstones tossed around in a storm cloud before they finally settle. Starting from 6, the path climbs to 16 and then drops through 8, 4 and 2 until it reaches 1.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/math/collatz \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":6}'const res = await fetch("https://api.kit.forhosting.com/math/collatz", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 6
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/collatz",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 6
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/collatz", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":6}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":6}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/collatz", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"n": 6
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.collatz",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
This Collatz sequence calculator turns that rule into a reliable API response: send n, get the complete path, how many steps it took, and the highest value the sequence ever reached.
What the Collatz rule does to a number
This endpoint does not try to prove the conjecture. It simply executes the deterministic rule for the n you provide, returns the full ordered list of values, counts the number of transformations required to reach one, and reports the maximum value observed anywhere in that path so you can compare peaks across different seeds without re-deriving the sequence yourself.
Input, output and error contract
On success the response includes the original n, the complete sequence array beginning at n and ending at one, steps (how many applications of the rule were needed), max (the highest value in the sequence), and length (how many terms the array contains, which is always steps plus one). Very large starting values can produce long paths or intermediate terms that leave the safe-integer range; those cases are rejected with a clear message rather than returning corrupted floating-point numbers. POST a JSON body with a single required field n: an integer greater than or equal to one. If n is missing, not an integer, or less than one, the task fails with invalid_input and is not billed.
Where a Collatz API earns its keep
Educators use hailstone sequences to introduce loops, branching, and the idea that a tiny rule can generate surprising structure: students can plot steps-versus-n or peak-versus-n without reimplementing off-by-one bugs in every language of the course. Data-visualization authors embed the sequence array in charts that animate the rise and fall of each seed. Interview candidates and competitive-programming practice sites often ask for step counts or longest chains under a bound; calling a shared endpoint keeps reference answers consistent across graders. Researchers exploring stopping-time statistics or record peaks for consecutive integers can batch the endpoint and store structured JSON rather than scraping ad-hoc calculators. If you only need a one-off check, paste n in the free browser tool above; when you need machine-readable sequence, steps and max in a pipeline, the API at $0.002 per item is the same computation behind a task_id and signed delivery.
Technical questions
What does the Collatz API cost?
Successful calls cost $0.002 each. Invalid input is not billed. You can also run the same logic free in your browser on this page.