Calkin-Wilf fraction index
3/2 is the 5th fraction in the Calkin-Wilf sequence, two levels below the root 1/1. Enter a reduced fraction with numerator and denominator from 1 to one million to get its exact position and depth. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- A
- 3
- B
- 2
- Fraction
- 3/2
- Index
- 5
- Depth
- 2
- Gcd
- 1
The Calkin-Wilf tree enumerates every positive rational number exactly once as a reduced fraction a over b. Students of discrete mathematics, enumerative combinatorics, and continued fractions often need the index of a given fraction in that sequence—the breadth-first label of the tree node that carries a/b. This capability accepts two positive integers a and b, verifies that the fraction is already in lowest terms, and returns the one-based Calkin-Wilf index together with the path depth from the root one-over-one.
What the Calkin-Wilf sequence assigns to each reduced fraction
The Calkin-Wilf tree is a rooted binary tree whose root is labeled one over one. The left child of a node labeled a over b is a over a plus b, and the right child is a plus b over b. Every positive rational appears at exactly one node when written in lowest terms, which is why the enumeration is a perfect listing of Q-plus rather than a multi-valued walk through equivalents like two-fourths.
Reading the tree in the natural heap order—root index one, left child of n at two n, right child at two n plus one—produces the Calkin-Wilf sequence: one over one, one over two, two over one, one over three, three over two, two over three, three over one, and so on.
Given a reduced pair a and b, this tool reconstructs the unique path from a over b back to the root by repeatedly applying the parent rule: if a is less than b the node was a left child and the parent is a over b minus a; if a is greater than b it was a right child and the parent is a minus b over b. Encoding that path as a binary index recovers the sequence position without generating the entire tree.
Depth is the number of edges from the root, useful when you care about height rather than the dense binary label.
What you can do with it
Classroom and homework checks
Let students submit reduced a and b and verify the one-based Calkin-Wilf index and path depth against textbook tree drawings.
Exam and LMS auto-grading
Mark Calkin-Wilf index items with a deterministic oracle that rejects non-reduced fractions instead of silently simplifying.
Research notebooks and bijection demos
Map positive rationals to natural numbers via the Calkin-Wilf labeling in scripts without embedding a full computer-algebra library.
FAQ
How do I get the Calkin-Wilf index of a fraction a/b?
Send positive integers a and b with gcd(a,b)=1.
What happens if the fraction is not in lowest terms?
Calkin-Wilf labels are unique for reduced positives; unreduced input is rejected and not billed. Reduce first, then retry.
Is the index zero-based or one-based?
One-based: 1/1 has index 1, 1/2 has index 2, 2/1 has index 3, matching the usual heap numbering of the tree.
How is this different from the Stern-Brocot tree?
Both trees enumerate positives, but child rules and order differ.
What does the depth of a fraction tell me?
It counts the steps from the root 1/1 down to the fraction. 3/2 has depth 2, because it is the right child of 1/2, which is the left child of the root.
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/numth/calkin-wilf-index \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"a":3,"b":2}'const res = await fetch("https://api.kit.forhosting.com/numth/calkin-wilf-index", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"a": 3,
"b": 2
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/calkin-wilf-index",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"a": 3,
"b": 2
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/calkin-wilf-index", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"a":3,"b":2}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"a":3,"b":2}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/calkin-wilf-index", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"a": 3,
"b": 2
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.calkin_wilf_index",
"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
Non-reduced inputs fail with invalid_input so that the bijection between positions and reduced positives stays exact.
What the Calkin-Wilf sequence assigns to each reduced fraction
Because the map is bijective on reduced positives, asking for the index of an unreduced fraction would break uniqueness; the API therefore rejects gcd greater than one instead of silently reducing and returning another label's position.
Input contract, response shape, and error behaviour
Example: a equals three and b equals two yields fraction three over two, index five, and depth two, because three over two is the right child of one over two, which is the left child of the root. POST a JSON body with two required positive integers: a, the numerator, and b, the denominator. Aliases numerator, num, and p are accepted for a; denominator, den, and q for b. Each field may arrive as a JSON number or as a whole decimal string; leading zeros, fractions, scientific notation, zero, and negatives fail with invalid_input naming the field. Values must lie between one and one million inclusive so walk cost stays bounded on the Worker. On success the body echoes a and b, a fraction string such as three slash two, the index as a JSON number when it fits the safe integer range or as a decimal string when it does not, the path depth from the root, gcd equal to one, and a short formula string restating the left and right child rules. Pair the response with your own tree drawing if you need a diagram; this endpoint returns the index and depth only and does not emit step lists of intermediate parents beyond what you can recompute from the same rules.
Where a Calkin-Wilf fraction index API earns its keep
Discrete math courses and contest-prep platforms use a Calkin-Wilf fraction index calculator so learners can check the position of arbitrary reduced rationals without drawing large trees by hand. Research notebooks that study hyperbinary representations, Minkowski question-mark related maps, or bijections between N and Q-plus need a deterministic oracle that refuses unreduced input rather than quietly simplifying and drifting from the textbook definition. Curriculum generators and LMS auto-graders can mark items that ask for the index of three-halves or five-eighths with a single API call at the published $0.002 base price, with invalid gcd cases free of charge.
Technical questions
What does the Calkin-Wilf fraction index calculator cost?
The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.